1.15.2 [Solved] How to check if a player moved a full block

Discussion in 'Spigot Plugin Development' started by SpyingBadge2143, Jun 28, 2021.

Thread Status:
Not open for further replies.
  1. I'm making a plugin so whenever you move a block you get +2 hp. How would I be able to tell if they moved a full block?
     
  2. Listen to the PlayerMoveEvent and check if the getFrom().getBlockX() != getTo().getBlockX(), and the same thing for the Z axis
     
  3. Use a PlayerMoveEvent and compare the getFrom & getTo function returns.

    Edit: Literally, sniped LOL
     
  4. PlayerMoveEvent fires a lot of times per second, so I'm not sure if the distance will ever be more than 1 block. You could store the cumulative distance moved outside of the function if you really wanted to go that route.

    I would do it by scheduling a repeating task to that would check the distance of the player's location from their location the last time the task ran.
     
  5. The PlayerMoveEvent sounds like exactly the right thing here. As long as the block X/Z locations are used in getTo() and getFrom() it will work as OP requires.

    Edit: If you also want to check the a block change on the Y axis, you can just compare event.getTo().getBlock() != event.getFrom().getBlock()
     
    #5 Esophose, Jun 28, 2021
    Last edited: Jun 28, 2021
  6. Y too if desired

    This isn’t too heavy for the PlayerMoveEvent. This is way simpler and optimized for what op wants to achieve than what you proposed.
     
  7. I though of that but would it compare the points in the coordinates?
     
  8. BlockX, blockY and blockZ are integers, so no decimal point
     
  9. I'm not concerned about computation impact. The speed of a sprinting player is about 5.6 blocks per second. PlayerMoveEvent fires more than six times per second when the player is moving continuously, so how can using getTo and getFrom ever register a movement of more than one block under normal circumstances (no speed attribute modification, no speed potion, etc)?
     
  10. Because at some point, the X/Z value will e.g. change from 4.9 to 5.1 so getTo().getBlock().getX() will be different from getFrom().getTo().getBlock().getX()
     
  11. Its just a general misconception that PlayerMoveEvent will have a negative impact on the server but it always come to what you do with it. a single if with simple comparison and changing the health of a player isnt that much of a deal. PlayerMoveEvent is called everytime the player moves (wasd) and look in a direction direction (yaw, pitch). Here we wanna limit it to a full block change with a if, mean that the event will be called everytime, but won't go past the if condition.
     

  12. This solution will cause unwanted behavior if a player moves a short distance back and forth over a boundary between two blocks.
     
  13. I don't think so, I think it would prevent the player from leaving a certain block because no matter how short the distance from one location to another is, you will know if a player is in a new block, for example if a player goes from x: 3.99999 to 4.0 blockX will still be 3 and 4 relatively.
     
    1. Player walks from 3.9999 to 4.0. Player gains 2 health
    2. Player walks from 4.0 to 3.9999 Player gains 2 health
    3. Repeat
    The player can walk less than a full block and still gain the health. This is unwanted behavior.
     
  14. Its faster to just keep sprinting forward
     
    • Agree Agree x 1
  15. I’ve used the PlayerMoveEvent and comparing block XZ values method before, and there’s no unwanted behavior.


    We’ve suggested using getBlockX/Y/Z. Block locations are floored to integers, meaning this would be:
    1. Player walks from 3 to 4 Player gains 2 health
    2. Player walks from 4 to 3 Player gains 2 health

    And depending on OP’s use-case, they may want to implement a check to see if they’ve just moved back and forth or some other condition, but that’s not anything specific to using the PlayerMoveEvent.



    Also this thread is getting really messy. @SpyingBadge2143 if your issue has been solved with any of our suggestions, I’d recommend locking this post and changing the tag to “Solved”
     
    • Agree Agree x 1
Thread Status:
Not open for further replies.