# Solved Get location relative to return double ?

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

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
` //the backward variable gets the blockface behind the playerplayer.getLocation().getBlock().getRelative(backward).getLocation().getX() `

#1
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.

#2
3. Offline

### 1RogueRetired Staff

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

#3
4. Offline

### ThunderWaffeMC

Created a lengthy method but it does the job:

Code:java
`   public Location getRelativeLocation(Player player, String direction) {     Location location = player.getLocation();     String facingDirection = getDirection(player);     if(facingDirection.equalsIgnoreCase("north")) {       if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }     }     if(facingDirection.equalsIgnoreCase("east")) {       if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }     }     if(facingDirection.equalsIgnoreCase("south")) {       if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }     }     if(facingDirection.equalsIgnoreCase("west")) {       if(direction.equalsIgnoreCase("forward")) { return new Location(location.getWorld(), location.getX() - 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("backward")) { return new Location(location.getWorld(), location.getX() + 1, location.getY(), location.getZ(), location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("left")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() + 1, location.getYaw(), location.getYaw()); }       if(direction.equalsIgnoreCase("right")) { return new Location(location.getWorld(), location.getX(), location.getY(), location.getZ() - 1, location.getYaw(), location.getYaw()); }     }     return null;   } `

#4