How to check when a player stops moving

Discussion in 'Plugin Development' started by Vinceguy1, Dec 13, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Vinceguy1

    How do I check when a player stops moving? I know there is the onPlayerMoveEvent, but that only works while they're moving, is there any way we can check to see if the player is not moving? Is there any event I can use that is active when a player is standing still/doing nothing to check their velocity?
     
  2. Offline

    Wingzzz

    Interesting, perhaps you could try something along the lines of (when handling PlayerMoveEvent):
     
  3. I think you need to check the player's velocity.
    Player p=something;
    Velocity v=p.getVelocity();
    if((v.getX()==0)&&(v.getY()==0)&&(v.getZ()==0)){
    }

    The code is not tested and may be wrong.
     
  4. Offline

    Vinceguy1

    That won't work, because it will only try to execute that while the player is moving... >_<

    But, I would need an event to put that in

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. no, you don't have to.
    String playerName="louis3325";
    Player p=Bukkit.getPlayer(playerName);
    Velocity v=p.getVelocity();
    if((v.getX()==0)&&(v.getY()==0)&&(v.getZ()==0)){
    }
     
  6. Offline

    CubieX

    I guess you need this information regardless of the players current action, right?
    Then I would suggest you put that in a repeating task that runs every couple of ticks.
    But if running it so frequently, better keep the code short in the timer task to not create lag.
     
  7. Offline

    Wingzzz

    Hahaha! Seems that one got the best of me! Hilarious ^_^
     
  8. Offline

    werter318

    He didn't say anything about putting that into a PlayerMoveEvent?
     
  9. Offline

    1Rogue Retired Staff

    Run an asynchronous thread that checks the player's velocity every x seconds.
     
  10. Offline

    werter318

    @1Rogue I think you mean synchronous because Asynchronous tasks should never access any API in Bukkit
     
  11. Offline

    1Rogue Retired Staff


    No, I mean asynchronous. You can safely read a player's velocity asynchronously, and execute a callable on the main thread when a task should be run for a player.
     
  12. Offline

    CubieX

    I think the pure action of checking the players velocity is much less expensive when doing it synchrounously,
    than creating an asynchronous task every time the check runs and then create a synchronous task when he needs to take action.
    So I would not use an async task here. There is simply no benefit from it in this situation.
     
  13. Offline

    1Rogue Retired Staff


    How is it less expensive to keep an asynchronous thread that is running repeatedly?

    Edit: More*
     
  14. creating threads uses system resources.
     
  15. Offline

    1Rogue Retired Staff


    It's resources versus stability. Keeping a single thread instance and running it when necessary / having it self-managed wouldn't be too resource intensive anyhow.
     
  16. Offline

    Vinceguy1

    I've never had to run anything like that before, could you give me an example of some code please?
     
Thread Status:
Not open for further replies.

Share This Page