How to make an item you use on a mob or player (Minecraft mod)
When a player right-clicks a mob with an item, the item’s interactLivingEntity(stack, player, target, hand) runs (itemInteractionForEntity on 1.12.2). The target is the mob. Add an effect with target.addEffect(new MobEffectInstance(effect, ticks)) (EffectInstance on 1.16.5, PotionEffect on 1.12.2), do it on the server, and return a success result so the player’s arm swings. For things that are not living (a boat, a painting) the click goes through the entity instead.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Make a class that extends Item and override interactLivingEntity.
- On the server, add the effect to the target.
- Return the success result (InteractionResult.sidedSuccess(level.isClientSide()) before 1.21.2, InteractionResult.SUCCESS from 1.21.2).
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| effect | GLOWING |
The effect the mob gets. |
| seconds | 10 |
How long the effect lasts, in seconds. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.20 to 1.20.1, Fabric 1.20 to 1.20.3
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.20 to 1.20.1, Fabric 1.20 to 1.20.3”Uses:
Item.interactLivingEntityLivingEntity.addEffectMobEffectInstanceMobEffects
src/main/java/com/example/examplemod/item/GlowWandItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;
// Glow Wand: right-click a mob to give it GLOWING for 10 seconds. interactLivingEntity runs in the client and on the server; the effect is added on the server.public class GlowWandItem extends Item { public GlowWandItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity target, InteractionHand hand) { if (!player.level().isClientSide()) { target.addEffect(new MobEffectInstance(MobEffects.GLOWING, 10 * 20)); } return InteractionResult.sidedSuccess(player.level().isClientSide()); }}It’s registered like any other item, as GlowWandItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
Item.interactLivingEntityLivingEntity.addEffectMobEffectInstanceMobEffects
src/main/java/com/example/examplemod/item/GlowWandItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;
// Glow Wand: right-click a mob to give it GLOWING for 10 seconds. interactLivingEntity runs in the client and on the server; the effect is added on the server.public class GlowWandItem extends Item { public GlowWandItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity target, InteractionHand hand) { if (!player.level().isClientSide()) { target.addEffect(new MobEffectInstance(MobEffects.GLOWING, 10 * 20)); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as GlowWandItem: see a new item.
Forge 1.17.1 to 1.19.4, Fabric 1.16.5 to 1.19.4
Section titled “Forge 1.17.1 to 1.19.4, Fabric 1.16.5 to 1.19.4”Uses:
Item.interactLivingEntityLivingEntity.addEffectMobEffectInstanceMobEffects
src/main/java/com/example/examplemod/item/GlowWandItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.effect.MobEffectInstance;import net.minecraft.world.effect.MobEffects;import net.minecraft.world.entity.LivingEntity;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;
// Glow Wand: right-click a mob to give it GLOWING for 10 seconds. interactLivingEntity runs in the client and on the server; the effect is added on the server.public class GlowWandItem extends Item { public GlowWandItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity target, InteractionHand hand) { if (!player.level.isClientSide()) { target.addEffect(new MobEffectInstance(MobEffects.GLOWING, 10 * 20)); } return InteractionResult.sidedSuccess(player.level.isClientSide()); }}It’s registered like any other item, as GlowWandItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.interactLivingEntityLivingEntity.addEffectEffectInstanceEffects
src/main/java/com/example/examplemod/item/GlowWandItem.java
package com.example.examplemod.item;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResultType;import net.minecraft.potion.EffectInstance;import net.minecraft.potion.Effects;import net.minecraft.entity.LivingEntity;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;
// Glow Wand: right-click a mob to give it GLOWING for 10 seconds. interactLivingEntity runs in the client and on the server; the effect is added on the server.public class GlowWandItem extends Item { public GlowWandItem(Item.Properties properties) { super(properties); }
@Override public ActionResultType interactLivingEntity(ItemStack stack, PlayerEntity player, LivingEntity target, Hand hand) { if (!player.level.isClientSide()) { target.addEffect(new EffectInstance(Effects.GLOWING, 10 * 20)); } return ActionResultType.sidedSuccess(player.level.isClientSide()); }}It’s registered like any other item, as GlowWandItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.interactLivingEntityEntityLivingBase.addEffectPotionEffectMobEffects
In 1.12.2 the method is itemInteractionForEntity(stack, player, target, hand) and it returns a boolean.
src/main/java/com/example/examplemod/item/GlowWandItem.java
package com.example.examplemod.item;
import net.minecraft.entity.EntityLivingBase;import net.minecraft.entity.player.EntityPlayer;import net.minecraft.init.MobEffects;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.potion.Potion;import net.minecraft.potion.PotionEffect;import net.minecraft.util.EnumHand;
// Glow Wand: right-click a mob to give it GLOWING for 10 seconds. itemInteractionForEntity runs in the client and on the server; the effect is added on the server.public class GlowWandItem extends Item { @Override public boolean itemInteractionForEntity(ItemStack stack, EntityPlayer player, EntityLivingBase target, EnumHand hand) { if (!player.world.isRemote) { target.addPotionEffect(new PotionEffect(effect("GLOWING"), 10 * 20)); } return true; }
// 1.12.2 names its effects differently from newer versions: GLOWING, LEVITATION, REGENERATION and WEAKNESS are the same, SLOW_FALLING does not exist yet. private static Potion effect(String name) { switch (name) { case "LEVITATION": return MobEffects.LEVITATION; case "REGENERATION": return MobEffects.REGENERATION; case "WEAKNESS": return MobEffects.WEAKNESS; default: return MobEffects.GLOWING; } }}It’s registered like any other item, as GlowWandItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/items/custom-item-interactions
- https://docs.neoforged.net/docs/1.21.1/items/interactionpipeline
Related how-tos
Section titled “Related how-tos”- An item that changes the block you click
- Ride a mob
- A tool that breaks a 3 by 3 area
- A wand that strikes lightning where you look
- An item that explodes where you aim
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.