# Solved Player Direction?

Discussion in 'Spigot Plugin Development' started by Eliminator, May 17, 2016.

1. ### Eliminator

I am trying to paste a schematic from world edit, but it needs to paste in front of the player.

I was wondering how to get the direction the player is looking, and make the schematic paste facing that direction. (Hopefully the schematics front is set when you copy it.)

2. ### Mr.Midnight

Erm you can do Player#getLocation#getDirection

3. ### Eliminator

Yes, but the direction is a vector.
I was hoping some had code or knew how to calculate north, east ect.

4. ### darkkir3

Check the player yaw against certain bounds. (basically every 90 degrees if you just want the cardinals)
Also: "I was hoping some had code" doesnt work here.

6. ### Eliminator

It's just god dang math, I had hopes someone made a class so I didn't have to spend hours, guess not...

To the work factory. ):

• Agree x 1
7. ### Eliminator

I found some stuff/made some stuff, and since I am nice I will share it in case anyone else wants this.
Code (Text):

public static final BlockFace[] axis = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
public static final BlockFace[] radial = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST };

private static String getFacing(float yaw){
BlockFace bf = yawToFace(yaw);
if(bf.equals(BlockFace.NORTH)){
return "SOUTH";
}else if(bf.equals(BlockFace.EAST)){
return "WEST";
}else if(bf.equals(BlockFace.SOUTH)){
return "NORTH";
}else
return "EAST";
}

public static BlockFace yawToFace(float yaw) {
return yawToFace(yaw, false);
}

public static BlockFace yawToFace(float yaw, boolean useSubCardinalDirections) {
if (useSubCardinalDirections) {
return radial[Math.round(yaw / 45f) & 0x7];
} else {
return axis[Math.round(yaw / 90f) & 0x3];
}
}

public static float getLookAtYaw(Vector motion) {
double dx = motion.getX();
double dz = motion.getZ();
double yaw = 0;
// Set yaw
if (dx != 0) {
// Set yaw start value based on dx
if (dx < 0) {
yaw = 1.5 * Math.PI;
} else {
yaw = 0.5 * Math.PI;
}
yaw -= Math.atan(dz / dx);
} else if (dz < 0) {
yaw = Math.PI;
}
return (float) (-yaw * 180 / Math.PI - 90);
}

8. ### Ichbinjoe

Wow, you are saying you are going to have to work to get your work done? Woahhhhhhh

Sorry, SpigotMC isn't your personal code factory.

10. ### finnbon

Neither is google. You have to do this yourself. Also, hours of work? This can be done in 10 minutes.

11. ### Inkzzz Resource Staff

You can get the players' location using this method:
Code (Text):
private String translateDirection(Player player)
{
double r = ( player.getLocation().getY() - 90 ) % 360;
if(r <= 0)
{
r += 360D;
}
if (0 <= r && r < 22.5)
{
return "N";
}
if (22.5 <= r && r < 67.5) {
return "NE";
}
if (67.5 <= r && r < 112.5) {
return "E";
}
if (112.5 <= r && r < 157.5) {
return "SE";
}
if (157.5 <= r && r < 202.5) {
return "S";
}
if (202.5 <= r && r < 247.5) {
return "SW";
}
if (247.5 <= r && r < 292.5) {
return "W";
}
if (292.5 <= r && r < 337.5) {
return "NW";
}
if (337.5 <= r && r < 360.0) {
return "N";
}
return null;
}

• Optimistic x 1

13. ### darkkir3

Nice spoonfeed

14. ### Inkzzz Resource Staff

Thanks. Time is a precious virtue and is a waste if someone takes' hours of their time to figure out something which takes under 60 seconds.

• Agree x 1
• Optimistic x 1
15. ### Eliminator

I said doing it myself could take hours, calculating all he maths ect. There are usually methods already made for that, which there was!

16. ### darkkir3

wow.... thats so much math checking a value against certain bounds... wow... especially since the javadocs already explain what the values returned are:
You cant call yourself a developer when you didnt even read the docs

17. ### PickNChew

At least provide decent code if you are going to spoonfeed.

• Agree x 1
• Funny x 1
18. ### Eliminator

Anyway guys, I solved way frken up there before anyone posted code.

Sometimes I feel lazy, and its OK.

Just don't post if you don't like it.

19. ### Inkzzz Resource Staff

This isn't my code. It is located here.