Solved Need help with PlayerToggleSneakEvent

Discussion in 'Plugin Development' started by Tendey, Aug 23, 2016.

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

    Tendey

    Hey!
    I have a question.
    My server chrashes everytime I release my 'Sneak' button. I want that the player gets a several amount of velocity while he is sneaking. But my code doesnt work.
    Code:
    @EventHandler
       public void onSetSneak(PlayerToggleSneakEvent event) {
         Player p = event.getPlayer();
         if (!p.isSneaking()) {
           //p.sendMessage("Hey1");
           while (p.isSneaking()) {
             p.setVelocity(p.getLocation().getDirection().multiply(0.5));
             p.setVelocity(new Vector(p.getVelocity().getX(), 0.3D, p.getVelocity().getZ()));
            // p.sendMessage("Hey2");
           }
         }
       }
    
    Can somebody help me?
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Tendey Please don't put while loops in your code.
    This will cause lag.
     
  3. Offline

    Tendey

    @timtower Thats why I am asking for help.
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Tendey Put the velocity stuff in a runnable.
     
  5. @Tendey
    Replace the while loop with a repeating BukkitRunnable.
     
  6. Offline

    Zombie_Striker

    @Tendey
    Replace this with a Repeating Task. Every time it repeats, check if the player is shifting. If they are shifting, set the velocity. If not, cancel the task.
     
  7. Offline

    Tendey

    @Zombie_Striker @AlvinB @timtower Does anybody have an example how to fix this? Because I'm doing 'Java Bukkit' just a week and learning everyday new stuff :)
     
  8. Offline

    Zombie_Striker

    @Tendey
    Click the link in my last post (OR HERE) to get the wiki page.
     
  9. Offline

    Tendey

  10. Offline

    Zombie_Striker

    @Tendey
    No one here is going to spoonfeed you code. We are here to help you, not write your plugin for you. The link I provided will teach you how to set up repeating tasks, and already has some examples in it.
     
    Astrophylite and bwfcwalshy like this.
  11. Offline

    Tendey

    @timtower @Zombie_Striker So, my code is looking like that right now:

    @EventHandler
    public void onSneak(PlayerToggleSneakEvent event) {
    // Create the task anonymously and schedule to run it once, after 20 ticks
    new BukkitRunnable() {

    @Override
    public void run() {
    Player p = event.getPlayer();
    if (p.isSneaking()) {
    p.setVelocity(p.getLocation().getDirection().multiply(0.5));
    p.setVelocity(new Vector(p.getVelocity().getX(), 0.3D, p.getVelocity().getZ()));
    p.sendMessage("Hey");
    }
    }

    }.runTaskLater(this.plugin, 1);
    }


    But now I want that while the player is sneaking he gets velocity.
     
    Last edited: Aug 26, 2016
  12. Offline

    Tendey

Thread Status:
Not open for further replies.

Share This Page