Help. Remove a potion effect from a player.

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

  1. 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 (Text):
    1. ((CraftPlayer)player).getHandle().getEffects.remove(effectID);
    Where the effectIDs were listed here.

    Any help is much appreciated.
     
    kahlilnc likes this.
  2. 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. 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 (Text):
    1. 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. 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 (Text):
    1.  
    2.     public static void removeMobEffect(LivingEntity entity, int type) {
    3.         try {
    4.             // remove effect
    5.             Field field = EntityLiving.class.getDeclaredField("effects");
    6.             field.setAccessible(true);
    7.             HashMap effects = (HashMap)field.get(((CraftLivingEntity)entity).getHandle());
    8.             effects.remove(type);
    9.             // alert player that effect is gone
    10.             if (entity instanceof Player) {
    11.                 EntityPlayer player = ((CraftPlayer)entity).getHandle();
    12.                 player.netServerHandler.sendPacket(new Packet42RemoveMobEffect(player.id, new MobEffect(type, 0, 0)));
    13.             }
    14.             // remove graphical effect
    15.             ((CraftLivingEntity)entity).getHandle().getDataWatcher().watch(8, Integer.valueOf(0));
    16.         } catch (Exception e) {
    17.             e.printStackTrace();
    18.         }
    19.     }
    20.  
     
  5. 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. Correct.
     
  7. Works like a charm, cheers.
     
  8. 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. Ok but I still don't understand this line and how to get it working. Or do I need to use something else?
     
  11. It sets a specific data watcher value to 0. http://wiki.vg/Entities
     
  12. Ok now I got it. But it doesn't work still :
    Code (Text):
    1. cp.getHandle().addEffect(new MobEffect(2, 200, 5));
    2.                         cp.getHandle().getDataWatcher().watch(8, Integer.valueOf(0));
     
  13. 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. What is "Field" ? Where do i import from?
     
  15. Why not just use
    Code (Text):
    1. player.removePotionEffect(PotionEffectType.POOP);
    ?...
     

Share This Page