Help. Remove a potion effect from a player.

Discussion in 'Plugin Development' started by Laserhog, Jan 26, 2012.

  1. Offline

    Laserhog

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I need help with removing a potion effect from a player.
    I found some code in another topic that looked like it would work, but didn't.
    Code:
    ((CraftPlayer)player).getHandle().getEffects.remove(effectID);
    Where the effectIDs were listed here.

    Any help is much appreciated.
    kahlilnc likes this.
  2. Offline

    tomjw64

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Code:java
    1. EntityPlayer ep=((CraftPlayer)player).getHandle();
    2. ep.netServerHandler.sendPacket(new Packet42RemoveMobEffect(ep.id,effect);

    This should work, but make sure you pass an effect to the constructor of Packet42RemoveMobEffect instead of the ID. Also if you don't know the imports:

    import net.minecraft.server.EntityPlayer;

    import net.minecraft.server.Packet42RemoveMobEffect;

    I hope this helps you! :)

    This post has been edited 3 times. It was last edited by tomjw64 Jan 26, 2012.
    kahlilnc likes this.
  3. Offline

    Laserhog

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Thank you so much. However im not sure what the "effect" should be if its not the ID. Could you possibly give me an example of this? ie. the poison effect or the speed effect.

    EDIT: Actually i think i may have figured it out. So for poison it would be
    Code:
    ep.netServerHandler.sendPacket(new Packet42RemoveMobEffect(ep.id,new MobEffect(19,0,0)));
    ?
    ANOTHER EDIT: Never mind,"new MobEffect(19,0,0)" didn't work....

    This post has been edited 2 times. It was last edited by Laserhog Jan 26, 2012.
  4. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    To remove an effect, you need to do three things:
    1. Remove the effect from the entity
    2. If it's a player, tell the player they no longer have the effect
    3. Remove the graphical particle effect

    This is what I currently use:
    Code:
        public static void removeMobEffect(LivingEntity entity, int type) {
            try {
                // remove effect
                Field field = EntityLiving.class.getDeclaredField("effects");
                field.setAccessible(true);
                HashMap effects = (HashMap)field.get(((CraftLivingEntity)entity).getHandle());
                effects.remove(type);
                // alert player that effect is gone
                if (entity instanceof Player) {
                    EntityPlayer player = ((CraftPlayer)entity).getHandle();
                    player.netServerHandler.sendPacket(new Packet42RemoveMobEffect(player.id, new MobEffect(type, 0, 0)));
                }
                // remove graphical effect
                ((CraftLivingEntity)entity).getHandle().getDataWatcher().watch(8, Integer.valueOf(0));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
  5. Offline

    Laserhog

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Thanks, but not sure what "type" should be. Is it possibly the effect ID? So poison would be 19? And replacing "type" with 19 would remove the poison effect?
  6. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Correct.
  7. Offline

    Laserhog

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Works like a charm, cheers.
  8. Offline

    atlan1

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You said you remove the particles with this line. I tried that but it doesn't work.
    Could please explain what you did in this line?
    And is it possible to remove the particle effect without removing the mobeffect?
  9. Offline

    Rathfon

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Yes, it is possible to remove the particle and keep the effect.
  10. Offline

    atlan1

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok but I still don't understand this line and how to get it working. Or do I need to use something else?
  11. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    It sets a specific data watcher value to 0. http://wiki.vg/Entities
  12. Offline

    atlan1

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok now I got it. But it doesn't work still :
    Code:
    cp.getHandle().addEffect(new MobEffect(2, 200, 5));
                            cp.getHandle().getDataWatcher().watch(8, Integer.valueOf(0));
  13. Offline

    nisovin

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Sorry, I don't know why that is. You'll have to do some research on your own. When you choose to access server code directly like this, you have to accept the fact that sometimes you'll have to start digging through that code to see what's going on.
  14. Offline

    MrSnare

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    What is "Field" ? Where do i import from?
  15. Offline

    Woobie

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Why not just use
    Code:
    player.removePotionEffect(PotionEffectType.POOP);
    ?...

Share This Page