Topic category: User side tutorials
Creating on-hit effects
Hello and welcome to my tutorial on how to create on-hit effects on your tools! This tutorial will teach you on how to add potion effects to mobs when you hit them with a tool.
Step 1:
Since there is no on-hit procedure in tools we will be coding in it ourselves, so go on ahead and create your tool.
Step 2:
Select your tool and click on the "Edit the code of the selected mod element" the button should look like this "</>".
Step 3:
Override the public boolean "hitEntity", here's the code if you don't know how to:
@Override
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)
{
return true;
}
Make sure it is placed inside the class that extends the tool, not the mod element.
Step 4:
Currently, the code itself isn't going to do anything, so, insert this next bit of code on top of return true:
target.addPotionEffect(new PotionEffect(MobEffects.WITHER, 100, 2, false, false));
This is the code that gives out the effect, the important part here is the "new PotionEffect()" part, I'll be explaining it now.
-
The first argument is the effect that it's gonna give, there are a lot more effects, test them out.
-
The second argument determines how long the potion lasts in ticks, 20 ticks is a second. I've set mine to 100 ticks so that the wither effect would last for 5 seconds.
-
The third argument, determines the strength of a potion, I've set it to 2 so that it gives out a Wither III effect.
-
The fourth argument makes the particles less visible.
-
The fifth argument determines if the particles are visible or not.
That is the end of my tutorial, if you need some help, leave a comment. Also, tell me if there's something wrong, I have never tested this in a mod element due to me being unable to create mod elements for some reason.
¿Can you put the full code of the sword to see if it's okay?
Can you put the complete code of the sword to see because I do not know where I should put your code @c3d7