Location which is exacly 1 block behind player

Discussion in 'Plugin Development' started by MgMaor, Jun 23, 2014.

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

    MgMaor

    I need to get the location which is exacly 1 block behind player, how to do it?
    It should also depend on the yaw, and I don't know how to do it
     
  2. Offline

    NonameSL

    When player is looking SOUTH the location 1 block behind him is -1Z,
    When player is looking NORTH the location 1 block behind him is +1Z,
    When player is looking WEST the location 1 block behind him is+1X,
    When player is looking EAST the location 1 block behind him is -1X

    If you want to get directions like South West, or North East:
    For example, lets take South East: South is -1Z, and East is -1x. South East is -1Z, -1X.


    To subtract X or Z or Y (or add X,Y,Z of course) use the following method:
    Code:
    p.getLocation().add(int x, int y, int z);
    And replace x, y and z with the number of blocks you want to add, or remove with a negative number.

    Use the following method to get a players location:
    Code:java
    1. public static String getDirection(Player player) {
    2. double rotation = (player.getLocation().getYaw() - 90) % 360;
    3. if (rotation < 0) {
    4. rotation += 360.0;
    5. }
    6. if (0 <= rotation && rotation < 22.5) {
    7. return "NORTH";
    8. } else if (22.5 <= rotation && rotation < 67.5) {
    9. return "NORTHEAST";
    10. } else if (67.5 <= rotation && rotation < 112.5) {
    11. return "EAST";
    12. } else if (112.5 <= rotation && rotation < 157.5) {
    13. return "SOUTHEEAST";
    14. } else if (157.5 <= rotation && rotation < 202.5) {
    15. return "SOUTH";
    16. } else if (202.5 <= rotation && rotation < 247.5) {
    17. return "SOUTHWEST";
    18. } else if (247.5 <= rotation && rotation < 292.5) {
    19. return "WEST";
    20. } else if (292.5 <= rotation && rotation < 337.5) {
    21. return "NORTHWEST";
    22. } else if (337.5 <= rotation && rotation < 360.0) {
    23. return "NORTH";
    24. } else {
    25. return null;
    26. }
    27. }
     
    AoH_Ruthless likes this.
  3. Offline

    NoLiver92

    MgMaor Get the players position, get the direction they are facing and then take one off the relevant axis in the location. this will get the block behind.

    Code:java
    1. Location loc = player.getLocation();
    2. if(//facing north)
    3. {
    4. loc.setx(loc.getx() - 1);
    5. }

    This is an example and not actually the code, you will have to get the right x/y/z when you do it but the code is to show you the principle

    Edit: scratch this, ive been beaten to it by the post above ^
     
  4. NonameSL
    Geez, talk about spoon-feeding eh
     
    GrandmaJam and mythbusterma like this.
  5. Offline

    AoH_Ruthless

    RAFA_GATO_FOFO
    Not really. NonameSL explained what SOUTH, WEST, EAST ... (etc) was referring too nicely in my opinion, and how to add and subtract locations without telling him exactly what to do.

    That method he gave doesn't really spoonfeed as you can't do much else than that. While he could have explained the method a bit more, it is kind of understandable that each direction 'gets' 45 degrees out of a circle (360 / 8 = 45, 360 degrees in a circular rotation and 8 directions).
     
    NonameSL likes this.

  6. This would have sufficed in my opinion but that's why it's called an opinion :)
     
Thread Status:
Not open for further replies.

Share This Page