Removing ALL Potion Effects

Discussion in 'Plugin Development' started by WaffleOnABike, Jun 11, 2012.

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

    WaffleOnABike

    If a player has multiple potion effects, how would I remove them all, rather than one at a time?

    Using player.removePotionEffect(), and player.hasPotionEffect().

    -Waffle
     
  2. Offline

    ZeusAllMighty11

    use case and end case.

    case{ player haseffect X;
    remove effect
    }
    case{etc
    }
     
  3. Offline

    WaffleOnABike

  4. Offline

    stelar7

    Code:
    if (cmd.getName().equalsIgnoreCase("remove"))
        for (PotionEffect effect : player.getActivePotionEffects())
            player.removePotionEffect(effect.getType());
     
  5. Offline

    XbannisherX

    if you cant find a solution you can always remove them 1 at a time with

    Code:
    targetPlayer.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 0, 0),true);
     
  6. Offline

    hammale

    thank you sir for saving me a trip to the docs :)
     
  7. Or.....
    Code:
    player.removePotionEffect(PotionEffectType.(Name));
    done :)
     
  8. Offline

    Atakyn

    Could you explain this?
     
  9. Offline

    Tirelessly

    If someone types /remove
    Loop through all of their current potion effects
    Remove that potion effect type from them.
     
    Atakyn and devilquak like this.
  10. Offline

    Atakyn

    One more question what does the : do in this?
     
  11. Offline

    KeybordPiano459

    Atakyn
    That's how you make/use a for loop... It isn't just Bukkit, that's general Java stuff.
     
    Atakyn likes this.
  12. Offline

    stelar7

    When you see the colon ( : ), read it as “in.” Thus, the loop above reads as “for each PotionEffect in the players active potion effects” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand, programmers don’t always do so.
     
    Atakyn likes this.
  13. Offline

    the_merciless

    Whats wrong with...

    Code:
    player.getActivePotionEffects().clear()
    I see this thread is a few months old, maybe that has been added since.
     
    devilquak likes this.
  14. Offline

    emorodney


    It won't work. getActivePotionEffects() returns a Collection which is not referenced by anything else. So modifying that collection (using clear() - or anything else) wont make a difference.

    That's evident from the fact that this code:

    Code:
    for (PotionEffect effect : player.getActivePotionEffects())
            player.removePotionEffect(effect.getType());
    does work and does not throw a concurrent modification exception.
     
    BeatCycle likes this.
Thread Status:
Not open for further replies.

Share This Page