sneaking

Discussion in 'Plugin Development' started by MrTheNewGuy, Apr 8, 2013.

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

    MrTheNewGuy

    In my plugin i am making it is the player sneaks that they lose a health,
    but how can i detect if a player is sneaking.
     
  2. Offline

    Tirelessly

  3. Offline

    Adzwoolly

    Code:
    @EventHandler
    onPlayerToggleSneak(PlayerToggleSneakEvent e){
        if(e.getPlayer.isSneaking == false){//actually true
              e.getPlayer.setHealth(e.getPlayer.getHealth() - 1);
        }
    }
    I don't know how to take it off over time
    or I'm too lazy to make it at the moment.

    redone
    Code:
    @EventHandler
    public void onPlayerToggleSneak(final PlayerToggleSneakEvent e){
        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            @Override
            public void run() {
                if(e.getPlayer().isSneaking() == false){//actually true
                    e.getPlayer().setHealth(e.getPlayer.getHealth() - 1);
              }
            }
        }, 0L, 20L);
    }
    What about that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
    XlegitXcrazymanX likes this.
  4. Offline

    Rprrr

    Adzwoolly
    Might think about cancelling that runnable when the player stops sneaking (and check if he actually started sneaking), or you'll stack up tons of runnables and creating an insane amount of lag over time. :)
     
  5. Offline

    Adzwoolly

    Rprrr You're right. I'm not sure how to give it an ID though so it can be cancelled. I forgot to mention it.

    http://forums.bukkit.org/threads/have-scheduler-task-cancel-itself.53617/

    Conveniently found someone else's thread that's solved. I'm not sure how it'd work for other players though.

    Code:
    @EventHandler
        public void onPlayerToggleSneak(final PlayerToggleSneakEvent e){
            if(e.getPlayer().isSneaking() == false){//actually true
                int crouch = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {   
                    @Override
                    public void run() {
                        e.getPlayer().setHealth(e.getPlayer.getHealth() - 1);
                    }
                }, 0L, 20L);
            } else{
                Bukkit.getServer().getScheduler().cancelTask(crouch);
            }
        }
    The only problem is that I can't cancel crouch from there because it's in the else of it's creation.
    Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
Thread Status:
Not open for further replies.

Share This Page