[SOLVED]Player Direction

Discussion in 'Plugin Development' started by thefiscster510, Apr 27, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    thefiscster510

    Hey, So all I want is to know how to get the direction the player is facing so that I can invert it... Please help if you can that'd be great! Thanks!
     
  2. Offline

    evilmidget38

    Player.getLocation().getDirection() should probably do what you want.
     
    PureGero likes this.
  3. Offline

    thefiscster510

    Well, I want something that I could invert and then convert to a BlockFace..
    Like, If they're pointing South West then I want to make a block face that's "BlockFace.NORTH_EAST;" i think...
     
  4. Offline

    evilmidget38

    Sorry, can't help you there.
     
  5. Offline

    thefiscster510

    Thanks anyway. And for anyone else that happens to see this, I want this because i'm placing a sign with a command and i want the sign to be facing the person who placed it... So, somehow getting this out of where the player is facing would work to... Just any method so long as it works.
     
  6. Offline

    Iron_Crystal

    Well, this would take some trial and error, as I do not know the directions. What you would do is find a block like x + 1 from the player. Then make a command or something that makes the player face that direction. Then press f3 and see what direction you are facing (I believe south = 0, west = 1, north =2, and east = 4). Then record what direction x + 1 made you turn. If it makes you face north, then x - 1 is south. Then try z + 1. If z + 1 makes you turn west, then z - 1 is east. If you want things like north east, then you will have to face the block z + 1 x + 1 or whatever it is. I do not know the directions at the top of my head so these are total guesses.
     
  7. Offline

    thefiscster510

    @Iron_Crystal
    Ok, So doing what i THINK you said, I have discovered that Z axis is North/South. Z+ = South, Z- = North. And X Axis is East/West, Z+ = East, Z- = West.

    Also, 0 = W, 1 = N, 2 = E, 3 = S
    So, how would i get their current direction from that?
     
  8. Offline

    Iron_Crystal

    Well, maybe while they are doing whatever they do that places the sign, you can get the target block with player.getTargetBlock(null, 100) and get the location and see what the coordinates are relative to the player.
     
  9. Offline

    nicholasntp

    Code:java
    1.  
    2. public static String getCardinalDirection(Player player) {
    3. double rotation = (player.getLocation().getYaw() - 90) % 360;
    4. if (rotation < 0) {
    5. rotation += 360.0;
    6. }
    7. if (0 <= rotation && rotation < 22.5) {
    8. return "N";
    9. } else if (22.5 <= rotation && rotation < 67.5) {
    10. return "NE";
    11. } else if (67.5 <= rotation && rotation < 112.5) {
    12. return "E";
    13. } else if (112.5 <= rotation && rotation < 157.5) {
    14. return "SE";
    15. } else if (157.5 <= rotation && rotation < 202.5) {
    16. return "S";
    17. } else if (202.5 <= rotation && rotation < 247.5) {
    18. return "SW";
    19. } else if (247.5 <= rotation && rotation < 292.5) {
    20. return "W";
    21. } else if (292.5 <= rotation && rotation < 337.5) {
    22. return "NW";
    23. } else if (337.5 <= rotation && rotation < 360.0) {
    24. return "N";
    25. } else {
    26. return null;
    27. }
    28. }
    29.  

    This returns the player's direction. I got some of this from one of sk89q 's plugins.
     
  10. Offline

    Iron_Crystal


    Thanks for this! I am definitly saving this code!
     
  11. Offline

    nicholasntp

    No problem! Happy to help! Thank sk89q for the math stuff. I used this for playing smoke effects.
     
  12. Offline

    thefiscster510

  13. Offline

    nicholasntp

    You're very welcome!

    Did it work out for you?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  14. Offline

    thefiscster510

    @nicholasntp
    I did =3 But i addapted it to my needs. Just made this function.


    Code:
     public Byte direction(Player player){
              double rotation = (player.getLocation().getYaw() - 90) % 360;
                if (rotation < 0) {
                    rotation += 360.0;
                }
                if (0 <= rotation && rotation < 22.5) {
                    return 0xC; //S > E
                } else if (22.5 <= rotation && rotation < 67.5) {
                    return 0xE; //SW > SE
                } else if (67.5 <= rotation && rotation < 112.5) {
                    return 0x0; //W > E
                } else if (112.5 <= rotation && rotation < 157.5) {
                    return 0x2; //NW > SW
                } else if (157.5 <= rotation && rotation < 202.5) {
                    return 0x4; //N > W
                } else if (202.5 <= rotation && rotation < 247.5) {
                    return 0x6; //NE > NW
                } else if (247.5 <= rotation && rotation < 292.5) {
                    return 0x8; //E > N
                } else if (292.5 <= rotation && rotation < 337.5) {
                    return 0xA; //SE > NE
                } else if (337.5 <= rotation && rotation < 360.0) {
                    return 0xC; //S > E
                } else {
                    return null;
                }
            }
    I actually needed it for a sign placement thingy. Thanks for all of your help gais!
     
    TwixGamingYT likes this.
  15. Offline

    nicholasntp

    Lol ok! No problem!
     
Thread Status:
Not open for further replies.

Share This Page