Solved Getting the direction that the player is facing?

Discussion in 'Plugin Development' started by Rndom293, Dec 30, 2015.

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

    Rndom293

    First off, hey guys! I'm new to the Bukkit forums and also sorta new to bukkit plugin development. I've hit a patch in the road where I simply cannot find the answer, and I'm the kinda person who likes to do everything by himself without any help, so this truly means something. I've looking around on the web and also tried experimenting by myself, However I can't find a simple answer saying how to find if a player is facing North, East, South or west. Thank you everyone who responds :)

    A little extra information on the plugin..

    My brother thought it'd be cool if every 3 blocks a player walked a random block would jump in front of their path. however I am experiencing difficulty trying to spawn the block directly in front of them, no matter which direction they are facing. I have managed to complete everything else, but this has snagged me. I can make it so if they are on the X or Z axis the block spawns in front. but it has to be the X or Z and never at the same time.
    The same with if they are walking North, but if I change it so that if they are walking West, for example, it won't spawn a block if they walk North. I hope you understand what I am trying to say :) Thank you once again.
     
  2. Offline

    Zombie_Striker

    Found this old post:
    https://bukkit.org/threads/help-with-direction.71593/

    [Edit] Re-read the post, you might want something like this instead
    Code:
    int Yaw = player's yaw
    if(yaw >= 45 && yaw < 135)
       return "South";
    if(yaw >= 135 && yaw < 180)
       return "West";
    
    if(yaw >= 180 && yaw < 225)
       return "Noth";
    
    //the else
    return "East";
     
  3. Code:
    public class BlockFacing {
    
    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 }; 
    
    public static BlockFace locationToFace(Location location) {
      return yawToFace(location.getYaw());
    }
    
    public static BlockFace yawToFace(float yaw) {
      return yawToFace(yaw, true);
    }
    
    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];
     }
    }
    }
    
    BlockFace face = BlockFacing.locationToFace(player.getLocation());
    
     
  4. Offline

    Mrs. bwfctower

    @MaTaMoR_ Please, just stop spoonfeeding. You are helping nobody.
     
  5. I didn't even made that code lol, i found that code on the forums and i know it works.
     
  6. Offline

    Mrs. bwfctower

    Still, don't post it. You're not helping the OP, and if he has another similar problem, he's going to make another thread instead of knowing what to do.
     
  7. Offline

    Zombie_Striker

    @Mrs. bwfctower
    I found that once someone spoonfeeds, there's really nothing you can do. Most likely, they will continue to do it, and they most likely will never edit their previous post. Most of the time, pointing it out is useless.
     
  8. Lol isn't not like i don't know it is spoon feed, i just don't give a damn, that spoon feed thing is just crap.
     
  9. Offline

    Mrs. bwfctower

    Yeah. It's sad.

    Oh dear.
     
  10. Offline

    Zombie_Striker

    Proves my point even more. There is really nothing you can do.
     
  11. Well... You really don't help people by spoonfeeding... You solve the immediate issue, but they won't really understand the code or know how to solve a similar issue later... Especially when you spoonfeed uncommented code...
     
  12. Yeah good for you, keep talking about spoon feed and don't actually help, don't worry probably a mod will come and delete our pots.

    That's what you think, lol.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  13. Offline

    Rndom293

    ^^ This guy is completely correct :/ one look at the piece of code immediately turned me off, I had no idea what was going on or what it was about even, after a quick try to decipher it, I turned away without a second thought.
     
  14. Offline

    teej107

  15. Offline

    Rndom293

    Thank you all SO very much! It's helped greatly! I've however found the solution! :) I can't thank you enough for the guidance
     
Thread Status:
Not open for further replies.

Share This Page