3

I tried to get the direction, like North, South, West, East, of a Players Facing. I created this code, but sometimes it doesn't work...

package me.Nightfighter001.GlobalSystem.WorldEditor;

import org.bukkit.entity.Player;

public enum Yaw {
    NORTH, SOUTH, EAST, WEST;

    public static Yaw getYaw(Player p) {
        float yaw = p.getLocation().getYaw();
        if (yaw > 135 || yaw < -135) {
            return Yaw.NORTH;
        } else if (yaw < -45) {
            return Yaw.EAST;
        } else if (yaw > 45) {
            return Yaw.WEST;
        } else {
            return Yaw.SOUTH;
        }
    }
}

Can someone help me, please? Sorry for my bad English and thank you :)

LeWimbes
  • 517
  • 1
  • 10
  • 25

6 Answers6

2

The yaw values can range from -360 to 360, so for values greater than 135 or smaller than -135, your code currently doesn't display the correct cardinal direction. In your case, as far as I can tell, you can add 360 to any negative yaw values to shorten the code (less checks). The range from 0 - 45 degrees and 315 to 360 degrees is then south, 45 - 135 degrees is west, 135 - 225 degrees is north and 225 - 315 degrees is east. The code would look something like this, I tested this for a little while and it seemed to match the values of the f3 debug menu/screen:

    public static CardinalDirection get(Player player) {
    float yaw = player.getLocation().getYaw();
    if (yaw < 0) {
        yaw += 360;
    }
    if (yaw >= 315 || yaw < 45) {
        return CardinalDirection.SOUTH;
    } else if (yaw < 135) {
        return CardinalDirection.WEST;
    } else if (yaw < 225) {
        return CardinalDirection.NORTH;
    } else if (yaw < 315) {
        return CardinalDirection.EAST;
    }
    return CardinalDirection.NORTH;
}
Adrian Sohn
  • 1,271
  • 8
  • 10
2

The answer that was marked as correct works, but in theory the yaw can also have values above 360 degrees (noted in the documentation), so just handling negative values might cause bugs at some point.

The code here is your exact code, but it uses a true modulo implementation to get the angle in the 0-360 degree range.

public static Yaw getYaw(Player p) {
    float yaw = p.getLocation().getYaw();
    yaw = (yaw % 360 + 360) % 360; // true modulo, as javas modulo is weird for negative values
    if (yaw > 135 || yaw < -135) {
        return Yaw.NORTH;
    } else if (yaw < -45) {
        return Yaw.EAST;
    } else if (yaw > 45) {
        return Yaw.WEST;
    } else {
        return Yaw.SOUTH;
    }
}
DownloadPizza
  • 3,307
  • 1
  • 12
  • 27
0

You might want to try this:

private String getDirection(Player player) {
    double rot = (player.getLocation().getYaw() - 90) % 360;
    if (rot < 0) {
        rot += 360.0;
    }
    if (0 <= rot && rot < 67.5) {
        return "N";
    } else if (67.5 <= rot && rot < 157.5) {
        return "E";
    } else if (157.5 <= rot && rot <247.5) {
        return "S";
    } else if (247.5 <= rot && rot < 360.0) {
        return "W";
    } else {
        return null;
    }
}
NonameSL
  • 1,405
  • 14
  • 27
  • First thanks for your fast answer! I had to replace "N" by "W", "E" by "N", "S" by "E" and "W" by "S". But with your code I also found the Problem that the direction doesn't change at 45degree, but circa at 70degree. So if I move my Head from North to West, the code displays West, while it is still North... – LeWimbes Mar 06 '16 at 20:09
0

You can do this:

getPlayer().getEyeLocation().getDirection()

Viktoracri
  • 61
  • 1
  • 7
0

There is a player.getFacing() function available, which returns BlockFace. I don't know when this was added.

LorenzoP
  • 19
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '23 at 05:34
0

There is a getting BlockFace of player looking:

public static BlockFace getBlockFacing(Player p) {
    float yaw = p.getLocation().getYaw();
    float pitch = p.getLocation().getPitch();
    if (pitch >= 45) {
        return BlockFace.DOWN;
    } else if (pitch <= -45) {
        return BlockFace.UP;
    } else if (yaw > 45 && yaw < 135) {
        return BlockFace.WEST;
    } else if (yaw >= 135 || yaw < -135) {
        return BlockFace.NORTH;
    } else if (yaw >= -135 && yaw < -45) {
        return BlockFace.EAST;
    } else {
        return BlockFace.SOUTH;
    }
}
OneKa
  • 1