Solved Get cardinal direction of player

Discussion in 'Spigot Plugin Development' started by minecraftguy719, Feb 18, 2020.

  1. Hello all I am trying to find the cardinal direction of the player however it all seems to be returning the same value of: 'N'?

    Code:
    Code (Text):

    public String getPlayerDirection(Player player){
            double yaw = playerSelf.getLocation().getYaw();
           
            if (yaw >= 0 && yaw < 90) {
                // South
                return "S";
            } else if (yaw >= 90 && yaw < 180) {
                // West
                return "W";
            } else if (yaw >= 80 && yaw < 90) {
                // East
                return "E";
            } else {
                // North
                return "N";
            }
       }
     
    Thanks!
     


  2. first things first, in:
    Code (Text):

    public String getPlayerDirection(Player player){
            double yaw = playerSelf.getLocation().getYaw();
         
            if (yaw >= 0 && yaw < 90) {
                // South
                return "S";
            } else if (yaw >= 90 && yaw < 180) {
                // West
                return "W";
            } else if (yaw >= 80 && yaw < 90) {
                // East
                return "E";
            } else {
                // North
                return "N";
            }
       }
     
    you have player as the given variable and playerSelf as the variable you are messing with, but no where does it set playerSelf to player.
    go ahead and try changing it to:

    Code (Text):

    public String getPlayerDirection(Player player){
            double yaw = player.getLocation().getYaw();
         
            if (yaw >= 0 && yaw < 90) {
                // South
                return "S";
            } else if (yaw >= 90 && yaw < 180) {
                // West
                return "W";
            } else if (yaw >= 80 && yaw < 90) {
                // East
                return "E";
            } else {
                // North
                return "N";
            }
       }
     
    and see if that helps.
     
  3. Sorry haha I forgot to change the variable while posting.

    Thanks
     
  4. Did that fix the problem?
     
  5. No sir, the code wouldn't compile if that was the case.
     
  6. Here is what I have that seems to work for me.

    Code (Java):
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args){

            if(sender instanceof Player){
                Player p = (Player) sender;

                double y = p.getLocation().getYaw();
                String direction = new String();

                if(y >= 135 || y < -135){
                    direction = "North";
                }
                if(y >= -135 && y < -45){
                    direction = "East";
                }
                if(y >= -45 && y < 45){
                    direction = "South";
                }
                if(y >= 45 && y < 135){
                    direction = "West";
                }

                p.sendMessage(String.valueOf(p.getLocation().getYaw()));
                p.sendMessage(direction);

                return true;

            }
    return false;
        }
    https://imgur.com/a/NrMU8T6
     
  7. A few things here...

    1. Whats the compile error? Its a pain to debug an error without the error.

    2. What are you using this for? There is probably a better method, avoid the xy problem.
     
  8. the yaw of player goes from 0 to 360, being 0 and 360 the south