1.16.5 Player Move Event

Discussion in 'Spigot Plugin Development' started by PmzHeroV2, Jun 19, 2021.

  1. i want player move event trigger only when player walks. can someone help ?
     
  2. PlayerMoveEvent has a #getFrom and a #getTo. Check that #getFrom#getX is not equal to #getTo#getX, and then do the same thing for #getZ. If both of those checks return true, then the player is walking.
     
  3. That will also make it trigger if the player is pushed by a water current, no?
     
  4. I'd assume so. Even if they didn't manually move, their location values are still changing.
     
  5. can you give me an example code ?
     
  6. I'm not here to code for you, but I'll give some pseudo-code if it helps. You'll just have to turn it into real code yourself.
    Code (Text):
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
        from = event.getFrom()
        to = event.getTo()
       
        if from.getX() equals to.getX() and from.getZ() equals to.getZ() {
            return
        }

        // ... player is walking, do whatever here
    }
    Probably, but it might be good enough for OP's use case.
     
  7. There is something very important to know about moving.

    All movement stuff is handled by the client (the game running on the user's computer) and not by the server. The client detects keyboard input like walking, it detects stuff like water pushing, it detects stuff like slime blocks, it handles all the complex reasons that a player might move.

    Then, every single tick (about 20 times a second) the client will send its coordinates to the server. This is so the server knows where the player is (so it can show that player to other players etc).

    This means that plugins can tell when and where a player moves, but it has absolutely no way of knowing what the reason for that movement was. The reason for the movement is only known by the client.

    All it is possible for your plugin to know is when the player's location changes.

    So, depending on exactly what you want, the best way to work out if a player is walking is probably doing what the other responses say and comparing the X and Z coordinates of the From and To locations.

    If you need to do stuff like specifically detecting if the player is walking and not sprinting, you could try and estimate their speed by working out the distance between From and To (with pythagoras) and taking into account that movement events are triggered 20 times a second. But that will be messy and imperfect.


    If you can't figure out what to do, the best thing to do might be to give us some more context about WHY you need to know when a player walks. It may be that there is another way to achieve what you want - or it might be that what you want is impossible with a plugin.

    Hope this answer helps you understand things a bit! Let me know if you'd like me to explain anything more clearly.