Solved Get location relative to return double ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jan 26, 2015.

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

    ThunderWaffeMC

    Need a little math help:

    If I were to get a relative block location to a player, how can I return it as a double? Basically, I want to teleport a player one block behind, but it will return the relative block coordinates as integers, not spawning the player exactly one block behind.

    Example:

    Player is at Z location -294.59042
    I want to get the Z location behind the player at -295.59042
    But the below code will only return -296.0 (rounds it up, which makes it even more confusing...)

    Code:java
    1.  
    2. //the backward variable gets the blockface behind the player
    3. player.getLocation().getBlock().getRelative(backward).getLocation().getX()
    4.  
     
  2. Offline

    mythbusterma

    @ThunderWaffeMC

    You can use Math.round(double) to round it. There are also corresponding functions that always round up or down in the same class.
     
  3. Offline

    1Rogue

    Keep in mind you're getting the location of the block, not the player (second .getLocation call).
     
  4. Offline

    ThunderWaffeMC

    Created a lengthy method but it does the job:

    Code:java
    1.  
    2. public Location getRelativeLocation(Player player, String direction) {
    3. Location location = player.getLocation();
    4. String facingDirection = getDirection(player);
    5. if(facingDirection.equalsIgnoreCase("north")) {
    6. if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }
    7. if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }
    8. if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    9. if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    10. }
    11. if(facingDirection.equalsIgnoreCase("east")) {
    12. if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    13. if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    14. if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }
    15. if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }
    16. }
    17. if(facingDirection.equalsIgnoreCase("south")) {
    18. if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }
    19. if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }
    20. if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    21. if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    22. }
    23. if(facingDirection.equalsIgnoreCase("west")) {
    24. if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    25. if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }
    26. if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }
    27. if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }
    28. }
    29. return null;
    30. }
    31.  
     
Thread Status:
Not open for further replies.

Share This Page