My amazing code crashes my macbook :c

Discussion in 'Plugin Development' started by Voltex, Mar 21, 2014.

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

    Voltex

    I'm doing a small guns plugin test and I want to spawn a particle wherever the snowball is.
    This is my code:
    Code:
    public void onInteract (PlayerInteractEvent e) {
           
            if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
            if (!(e.getPlayer().getItemInHand().getTypeId() == 419)) return;
           
            Player player = e.getPlayer();
            Snowball sb = (Snowball) player.getWorld().spawn(player.getLocation(), Snowball.class);
            sb.setShooter(player);
           
            while (!sb.isDead()) {
               
                player.playEffect(sb.getLocation(), Effect.ENDER_SIGNAL, 3);
               
            }
           
        }
    But when i right click, my macbook crashes. Any ideas why or what i'm doing wrong?
    Thanks!
     
  2. Offline

    rfsantos1996

    While the sb isn't dead, you'll keep playing effects forever. Do this instead:
    final Snowball sb = bla bla snowball.class);
    and create a repeating task like 1-3 tick interval playing the effect after checking if its alive, if doesn't cancel().
     
  3. Offline

    StealerSlain

    I may be wrong, but
    Code:java
    1. while (!sb.isDead()) {
    2.  
    3. player.playEffect(sb.getLocation(), Effect.ENDER_SIGNAL, 3);
    4.  
    5. }

    is endless loop. Better you create a scheduler and every 3 seconds (for example) play ender_signal effect.
     
  4. Offline

    TryB4

    Voltex

    the while statement freezes the thread until sb is dead.
     
  5. Offline

    GameplayJDK

    Voltex
    Because the while loop is freezing your main server thread (as TryB4 said), you need to create a SyncRepeatingTask (like StealerSlain said) which spawns particles every time it's called.
    If you google "Bukkit schedule SyncRepeatingTask" you 'll find a few threads
     
  6. Offline

    Voltex

    Thanks, I'll look at the schedule repeating task now :)
     
  7. Offline

    Barinade

    use isValid, not isDead, pretty sure snowballs never were alive to begin with
     
Thread Status:
Not open for further replies.

Share This Page