Check if Player is standing still.

Discussion in 'Plugin Development' started by VNGC, Mar 4, 2020.

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

    VNGC

    Hello again,

    i have a problem,
    i tried to use the PlayerMoveEvent to check if the Player is moving.

    @EventHandler
    public void ifnotmoving(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    if(p.getLocation().getX() == p.getLocation().getX()){
    if(p.getLocation().getY() == p.getLocation().getY()){
    if(p.getLocation().getZ() == p.getLocation().getZ()){
    p.sendMessage("You are not moving!!");

    but 1st: it does nothing, or sends me the message IF I AM moving.

    a friend told me, i cant use the PlayerMoveEvent, because if i am not moving, the event is not called and not working.
    but he told me he dont want to help me, because i should learn it myself by trying out. Well i dont know much about more than Listeners or Schedulers, because i dont use much more in most times.

    so i am asking here, how could i do it?
    if i am not moving, it should give me 1hp damage in fire tick speed. so i has to move everytime.
    but i cant get it. btw i used the "youre not moving" message just for testing purposes.

    thank you!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @VNGC Current and current location will always be the same.
    Check the e.from and the e.to instead
    Using a BukkitRunnable and a velocity check might be easier though.
     
  3. Offline

    VNGC

    @timtower

    so this should do it?

    if(e.getFrom().getX() == e.getTo().getX()) {
    the same for y and z
     
  4. Offline

    timtower Administrator Administrator Moderator

    @VNGC Best way to find out is by trying.
    Your friend is right though about the event not being called when standing still though.
     
  5. Offline

    VNGC

    @timtower
    well its giving the message if i move my mouse.
     
  6. Offline

    timtower Administrator Administrator Moderator

    And if you are not touching anything? You want to have damage then as well right?
     
  7. Offline

    VNGC

    i would change the p.sendmessage do p.damage(1);
    but using the message for testing, so i will not get killed fast if its not working

    edit: if i do nothing, then nothing happens.

    thats my actual code:

    Code:
        public void MoveDamage(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            if(e.getFrom().getX() == e.getTo().getX()) {
            if(e.getFrom().getY() == e.getTo().getY()) {
            if(e.getFrom().getZ() == e.getTo().getZ()) {
                                p.sendMessage("You are not moving!!");   
                    }
                }
            }
        }
    }
     
  8. Offline

    timtower Administrator Administrator Moderator

    @VNGC Because the move event only gets called when the player is actually moving.
    If you don't rotate then you don't get damaged.
     
  9. Offline

    VNGC

    isnt there something like a PlayerStandStillEvent or something similar to do it?
     
  10. Offline

    timtower Administrator Administrator Moderator

    Nope, why would there be?
    Use a BukkitRunnable, use the Player#getVelocity method.
     
  11. Offline

    VNGC

    so if im right, (if not im dumb i think xd)

    i have to get if playerVelocity is 0? and then set the damage
     
  12. Offline

    timtower Administrator Administrator Moderator

    It is a vector, so you need to check the length.
    And yes.
     
  13. Offline

    VNGC

    well i checked google and youtube.
    how i see it, i need an event. but i actually think i dont need. so how to do
     
  14. Offline

    timtower Administrator Administrator Moderator

    Why would you need an event? There is no need for one, there isn't even one for this.
    Make a BukkitRunnable, run it every tick.
     
  15. Offline

    VNGC

    is this right? :c

    BukkitScheduler scheduler = getServer().getScheduler();
    scheduler.scheduleSyncDelayedTask(this, new Runnable() {
    @Override
    public void run() {
    // damage
    }
    }, 15L);
     
  16. Offline

    timtower Administrator Administrator Moderator

    @VNGC Timer task, not delayed.
    And that will probably give a deprecated warning.
    Make a BukkitRunnable, call runTaskTimer on it.
     
  17. Offline

    VNGC

    BukkitTask task = new BukkitRunnable() {
    public void run() {

    }
    }.runTaskTimer(this, 1L, 1L );
    }


    this is it i think? if not idk, if yes it does'nt work

    "The method runTaskTimer(Plugin, long, long) in the type BukkitRunnable is not applicable for the arguments (MoveDamage, long, long)"
     
  18. Offline

    Kars

    @VNGC MoveDamage needs to be your plugin, or some instance that extends JavaPlugin.
     
  19. Offline

    VNGC

    thank you. how do i now get The Player? i cant use event.getPlayer because its not an Event, and cant use Player p = (Player) sender because its not a command. and more i dont know.
     
  20. Offline

    timtower Administrator Administrator Moderator

    You loop over all online players.
     
  21. Offline

    Kars

    @VNGC Honestly, i would just create a task that checks every players movement every second (or some interval). It keeps track of the last known position for each player and compares that to the current position. If the position is changed you'll know that the player has moved.
     
  22. Offline

    VNGC

    idk anything about velocity. i got the runnable now and the player.but have to get if the velocity is 0 (i think?)
    how do i do that...
    sry btw for asking so much
    @Kars how...
     
  23. Offline

    timtower Administrator Administrator Moderator

    @VNGC You use Player#getVelocity and there is probably a length method.
     
  24. Offline

    VNGC

    Yeah but only p.getVelocity().length() wont do it i think
     
  25. Offline

    Kars

    I have no experience with velocity either, but assuming it is 0 when a player is not moving this would work @VNGC
    PHP:
    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(MoveDamage, new BukkitRunnable() {
        public 
    void run() {
                for (
    Player player Bukkit.getServer().getOnlinePlayers()) {
                if (
    player.getVelocity() > 0) {
                    
    // player is moving or has moved
                
    } else {
                    
    // player is not moving
                
    }
            }
        }
    }, 
    0L20L);
    I didn't test this, nor did i verify syntax. But it schedules a repeating task that hcecks if a player has moved every second.

    **EDITED THE CODE**
     
    Last edited: Mar 5, 2020
  26. Offline

    VNGC

    well does this work for 1.14 how you sent it? i think not :eek:

    @Kars idk if it is 0 but i think so.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 5, 2020
  27. Offline

    KarimAKL

    @VNGC I don't think the way velocity works is any different in 1.14 than in previous versions, so i don't think that code is version dependant.

    Do note what @Kars said though:
     
  28. Offline

    Kars

    You need to replace 'Plugin plugin' with your plugin. (Not twice, that was my bad there)
    I also made some other mistakes in that code, i edited the code now in the original message.
     
  29. Offline

    VNGC

    well thanks but still got errors. idk if its my fault.
    1. on Bukkit.getServer : Syntax error on token "getServer", Identifier expected after this token
    2. on player.velocity : The operator > is undefined for the argument type(s) Vector, int
     
  30. Offline

    timtower Administrator Administrator Moderator

    @VNGC Please post your code.
     
Thread Status:
Not open for further replies.

Share This Page