# Solved PlayerMoveEvent - decimal point precision

Discussion in 'Spigot Plugin Development' started by Wilsoon, Oct 9, 2021.

Not open for further replies.
1. ### Wilsoon

Current PlayerMoveEvent:
Code (Java):

@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if(event.getTo().getBlockX() == event.getFrom().getBlockX() && event.getTo().getBlockY() == event.getFrom().getBlockY() && event.getTo().getBlockZ() == event.getFrom().getBlockZ()) return;
if(PreventMovementManager.contains(event.getPlayer())) {
Location loc = new Location(event.getFrom().getWorld(), event.getFrom().getBlockX(), (int) event.getFrom().getY(), event.getFrom().getZ(), event.getFrom().getYaw(), event.getFrom().getPitch());
event.getPlayer().teleport(loc);
return;
}
}
This is my current code with this line:
Code (Java):
if(event.getTo().getBlockX() == event.getFrom().getBlockX() && event.getTo().getBlockY() == event.getFrom().getBlockY() && event.getTo().getBlockZ() == event.getFrom().getBlockZ()) return;
This line makes the code only cancel the event if the player moved a block.

However, I have another piece of code that helps to teleport the player to the centre of a block,

and I instead want to make it so that the code cancels movement events if the player moves out of the centre point (~.5, ~.0, ~.5). How would I do this?

2. ### kingOf0

Remove first decimal part of x then check that is it between 0.45 and 0.55
same goes for z too.

You can shorten different. Maybe try exactly 0.5? Test and see

#2
Last edited: Oct 9, 2021
3. ### britishtable

I think you can also use event.setCancelled(true) instead of teleporting them when they move

• Agree x 1
4. ### _ItsWagPvP

i've tried to replicate this and solved with saving double x,y,z and double toX,toY,toZ, if it is not equal cancel event

5. ### Wilsoon

I eventually managed to accomplish this, thank you for all your responses!

6. ### Rezz

What was the solution you ended up going with?

7. ### Wilsoon

I got the end point by doing basic calculation and just subtracting the two values like kingOf0 mentioned, then checked against the 0.45 - 0.55