Comparing PotionEffectType

Discussion in 'Plugin Development' started by MineTheCube, Sep 10, 2015.

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

    MineTheCube

    Hi,

    I tried to get the level of a potion effect (health boost), but I can't compare directly potion effect:

    Code:
    for (PotionEffect pe : e.getPlayer().getActivePotionEffects()) {
        if (pe.getType() == PotionEffectType.HEALTH_BOOST) {
            Bukkit.broadcastMessage("§a" + pe.getType().toString() + " == " + PotionEffectType.HEALTH_BOOST.toString());
        } else {
            Bukkit.broadcastMessage("§c" + pe.getType().toString() + " != " + PotionEffectType.HEALTH_BOOST.toString());
        }
    }
    I get this, which was a bit unexpected:
    [​IMG]

    After looking at code, it saw that PotionEffectType is not an enum, but the potions values are static and final, meaning that it cannot be changed during runtime (as far as I know), so why it doesn't work ?
     
  2. Offline

    finalblade1234

    @MineTheCube
    Strange, both strings are the same, perhaps compare both strings?
     
  3. Sometimes you just don't know why something isn't working, so the best solution is to simply write the code a different way. This is another, simpler way you can test for a potion effect:

    Code:
    if (p.hasPotionEffect(PotionEffectType.HEALTH_BOOST)) {///code}
     
  4. Offline

    MineTheCube

    It should work, but I don't know if I can safely compare strings generated by the object..

    I need to retrieve the level of the effect, so hasPotionEffect isn't enough. And for some unknown reasons there isn't a method "getPotionEffect(type)"...
     
  5. Code:
    if (p.hasPotionEffect(PotionEffectType.HEALTH_BOOST)) {
    for (PotionEffect pe : e.getPlayer().getActiveEffects()) {
    amplifier = pe.getAmplifier();
    }
    }
    
     
  6. Offline

    MineTheCube

    I pretty sure it will not work if the player has more than one effect.

    Suppose that the player has 2 effects, health boost and regeneration, then hasPotionEffect(HEALTH_BOOST) will return true, but when looping effects, the amplifier can be set to amplifier of health boost, and after to amplifier of regeneration..
     
Thread Status:
Not open for further replies.

Share This Page