I've got a "wand" item that, on right-click, I wish to be able to inflict various things on mobs: set on fire or apply a potion effect to them. My two attempts at doing this are below.
Apply a poison potion effect:
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemstack, World world, EntityPlayer player, EnumHand hand) {
RayTraceResult target = Minecraft.getMinecraft().objectMouseOver;
if (target.typeOfHit == RayTraceResult.Type.ENTITY) {
if (target.entityHit instanceof EntityLivingBase) {
EntityLivingBase entitylivingbase = (EntityLivingBase) target.entityHit;
Potion potion = Potion.REGISTRY.getObject(new ResourceLocation("poison"));
PotionEffect potioneffect = new PotionEffect(potion, 50, 1);
if (potion.isInstant()) {
potion.affectEntity(null, null, entitylivingbase, potioneffect.getAmplifier(), 0.5D);
} else {
entitylivingbase.addPotionEffect(potioneffect);
}
}
}
return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
}
The result of the above is that the potion effect doesn't appear to be applied at all.
Changing the code to instead set the entity on fire:
@Nonnull
@Override
public ActionResult<ItemStack> onItemRightClick(@Nonnull ItemStack itemstack, World world, EntityPlayer player, EnumHand hand) {
RayTraceResult target = Minecraft.getMinecraft().objectMouseOver;
if (target.typeOfHit == RayTraceResult.Type.ENTITY) {
target.entityHit.setFire(4);
}
return ActionResult.newResult(EnumActionResult.SUCCESS, itemstack);
}
This results in a brief flash of fire effect on the entity, but it does not remain on fire for the full 4 seconds that I specify. I know that I've invoked the setFire method correctly because if I do so in a hitEntity() override like so:
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
target.setFire(4);
return true;
}
The entity stays on fire!