How do you use variables to build/add up when sneaking.

Discussion in 'Plugin Development' started by fireboyev, Sep 10, 2016.

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

    fireboyev

    Hey all, I would like to know how I could test when the player is sneaking then add onto a variable and once the player stops sneaking it uses the variable to multiply velocity.

    here is my code that I tried:
    Code:
    public class PlayerSneakEvent implements Listener {
        @EventHandler
        public void onPlayerSneak(PlayerToggleSneakEvent event) {
            Player player = event.getPlayer();
            Double power = 0D;
            Boolean running = false;
            if (event.isSneaking() == true) {
                running = true;
                while (running == true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    power = power + 1;
                    System.out.println(power);
                    if (event.isSneaking() == false) {
                        running = false;
    
                    }
                }
                if (event.isSneaking() == false) {
                    Vector playervel = player.getLocation().getDirection().multiply(power / 10);
                    player.setVelocity(playervel);
                    player.playSound(player.getLocation(), Sound.BLOCK_GLASS_BREAK, 100, 100);
                }
    
            }
    
        }
    }
    the velocity part just works fine but when I release sneak, the counter keeps going up and doesn't stop, eventually crashing the server...

    Any ideas?

    also if anyone knows I would prefer to have the variable store in the players xp bar so the player knows how much power they have built up.
     
  2. Offline

    geomap

    Just an Idea:

    ArrayList isSneaking - This will be used to store which players are sneaking
    Hashmap timeWhileSneak - This will be used to store how long the players were sneaking for

    onPlayerSneakEvent
    if isSneaking doesn't contain the players name, then add their name to the list
    and set their timeWhileSneak to 0;

    Make a Runnable (scheduleSyncRepeatingTask)

    In Runnable
    if isSneaking contains player name & !player.isSneaking() //player isn't sneaking
    then remove them from isSneaking

    How to get time while they were sneaking?
    In Runnable

    if isSneaking contains player name & player.isSneaking() //player IS sneaking (removed exclamation mark)
    timeWhileSneak++; // Increase the int by 1
    timeWhileSneak + <insert #>; // or you can specify how much you want it to go up by

    After they have stopped sneaking, the total amount of time they would have been sneaking for would be stored in the hashmap timeWhileSneak.

    How to set their power in the xp bar?
    In Runnable
    if isSneaking contains player name & player.isSneaking() //player IS sneaking (removed exclamation mark)
    int power = timeWhileSneak.get(<object>) // object would be however you stored the info, so like p.getName()
    p.setLevel(power);

    Hope this helps!
     
Thread Status:
Not open for further replies.

Share This Page