1.8.8 Getting all potion effects

Discussion in 'Spigot Plugin Development' started by Hellinduction, Apr 6, 2020.

  1. How would i loop through each and every potion effect? I am trying to create a troll command.

    My code so far does not seem to work:

    for (PotionEffectType effect : PotionEffectType.values()) {
    target.addPotionEffect(new PotionEffect(effect, Integer.MAX_VALUE, 255, true, false));
    }
     
  2. Define not working
     
  3. it returns potioneffecttype null
     
  4. Where?
     
  5. Hmm... You definitely don't want to be using Integer.MAX_VALUE (that's 2,147,483,647). If I remember correctly, most potion effects only go up to 255. Perhaps run a null check on `target` and make sure that it's initialized.
     
  6. The Integer.MAX_VALUE is for the duration of the potion, in this case, infinite
     
  7. Oh ok my bad. I completely read that wrong.
     
  8. In console, Caused by: java.lang.IllegalArgumentException: effect type cannot be null
     
  9. There's a specific line in the error message. We need to know to what line in your code it's pointing to.
     
  10. target.addPotionEffect(new PotionEffect(effect, Integer.MAX_VALUE, 255, true, false));

    ^ that is the line


    for (PotionEffectType effect : PotionEffectType.values()) {
    target.addPotionEffect(new PotionEffect(effect, Integer.MAX_VALUE, 255, true, false));
    }

    ^ that is the for loop
     
  11. You're getting all existing PotionEffectType's and I believe not all of them are applicable to a LivingEntity, so if you want every applicable potion effect, create a PotionEffect and call #apply(target) (Though your way should actually work the same).
    Also try debugging everything used in that line to see if the target or the potion effect is null.