Remove specific Potion Effects

Discussion in 'Spigot Plugin Development' started by Syncazor, May 16, 2017.

  1. Hello,
    I add infinite duration potion effects to a player.
    I was wondering, how could I determine that the player has the same potion effect I added, then remove it?

  2. Example: You want to remove the poison effect from the player.

    Add a PotionEffect with the PotionEffectType.POISON with value 0 and duration 0.
    That should work I guess...
  3. I know how to remove a potion effect, but how would i know it's the same one I added beforehand.
  4. Code (Text):
    for (PotionEffect pEffect : p.getActivePotionEffects())
                        p.sendMessage(ChatColor.translateAlternateColorCodes('&', this.plugin.getConfig().getString("Heal")));
    Cycle through the potion effects and remove the one you want?
  5. 1.12 allows you to get a potion effect that is applied to a player by using the following method: (Note: This can be null! )

    Code (Text):

    PotionEffect potionEffect = yourPlayer.getPotionEffect(PotionEffectType.YOURTYPE);

    int amplifier = potionEffect.getAmplifier();
    int duration = potionEffect.getDuration();
    PotionEffectType potionEffectType = potionEffect.getType();
    Color color = potionEffect.getColor();

    Now you can easily check some methods to check the duration & amplifier of the effect if that's exactly your effect.

    Otherwise for 1.11 or lower i would recommend @LeOllie 's post ( If you need to check the duration or amplifier or similar things.

    If you only have to check if the player has the potion effect just call

    Code (Text):

    boolean hasEffect = yourPlayer.hasPotionEffect(PotionEffectType.YOURTYPE);
  6. So there is not unique identifier or meta data :(
    This is the most helpful answer.
    Thank you.
  7. Choco


    No, that method's been in the API since the 6th of August, which I believe is back in 1.9.
    • Informative Informative x 1
    • Like Like x 1