Help. Remove a potion effect from a player.

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

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

    Laserhog

    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

    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! :)
     
    kahlilnc likes this.
  3. Offline

    Laserhog

    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....
     
  4. Offline

    nisovin

    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

    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

    Correct.
     
  7. Offline

    Laserhog

    Works like a charm, cheers.
     
  8. Offline

    atlan1

    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

    Yes, it is possible to remove the particle and keep the effect.
     
  10. Offline

    atlan1

    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

    It sets a specific data watcher value to 0. http://wiki.vg/Entities
     
  12. Offline

    atlan1

    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

    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

    What is "Field" ? Where do i import from?
     
  15. Offline

    Woobie

    Why not just use
    Code:
    player.removePotionEffect(PotionEffectType.POOP);
    ?...
     
Thread Status:
Not open for further replies.

Share This Page