Solved PlayerMoveEvent - decimal point precision

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

Thread Status:
Not open for further replies.
  1. 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,
    upload_2021-10-9_14-48-11.png

    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. 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? :unsure: Test and see
     
    #2 kingOf0, Oct 9, 2021
    Last edited: Oct 9, 2021
  3. I think you can also use event.setCancelled(true) instead of teleporting them when they move
     
    • Agree Agree x 1
  4. 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. I eventually managed to accomplish this, thank you for all your responses!
     
  6. What was the solution you ended up going with?
     
  7. 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
     
Thread Status:
Not open for further replies.