While hold an item wins potion effect

Discussion in 'Plugin Development' started by jobsonmb, Jun 10, 2015.

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

    jobsonmb

    //Bug Server cash "need help"

    @EventHandler
    public void onPlayerItemHeld(PlayerItemHeldEvent event){
    Player p = event.getPlayer();

    while(p.getItemInHand().getType() == Material.DIAMOND_SWORD)
    {
    if(!p.hasPotionEffect(PotionEffectType.INCREASE_DAMAGE))
    {
    p.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 50, 0));
    }
    }
    }
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    jobsonmb

    when i put a sword in hand, server crash
     
  4. Offline

    Adamki11s

    Because while you have the sword in your hand you enter the while loop, locking up the main server thread, so no other code can execute. Consequently you cannot change the item in your hand, or do anything else for that matter and eventually the loop will cause your server to crash.

    Change your while loop, to an if statement. That'll fix your problem.
     
  5. Offline

    jobsonmb

    I tried using "if" more he did not renew the potion effect
     
  6. Offline

    timtower Moderator Moderator

    You should run that code in a task instead of an event, without the while loop then.
     
  7. Offline

    Irantwomiles

    I've never used while loops while making bukkit plugins simply because I wasn't experienced enough with it and it kept crashing the server. Just do a task like timtower said.
     
  8. Offline

    caderape

    @jobsonmb
    You should check with a if statement. if item is sword and player doesnt have your potion effect //do stuff
     
Thread Status:
Not open for further replies.

Share This Page