How to make an item that does something when used (Minecraft mod)
An item’s own class overrides use(level, player, hand), which runs when a player right-clicks with it, once in the client and once on the server. Do game changes only when !level.isClientSide(), and answer with a success result: InteractionResultHolder.sidedSuccess(stack, level.isClientSide()) up to 1.21.1, InteractionResult.SUCCESS from 1.21.2. player.getCooldowns().addCooldown(this, ticks) greys it out for a while.
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 with a constructor taking Item.Properties.
- Override use(…) and do the work on the server side.
- Add a cooldown, then return the success result.
- Register the item with your class.
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 |
|---|---|---|
| heal | 4 |
The health it gives back, in half hearts. |
| cooldown | 40 |
Ticks before it can be used again (20 ticks are a second). |
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, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Fabric 1.16.5 to 1.20.3, Forge 1.17.1 to 1.20.1”Uses:
Item.useInteractionResultHolder.sidedSuccessLivingEntity.healItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/HealingCharmItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Healing Charm: right-click to heal, then wait for the cooldown. use runs in the client and on the server; the game changes on the server.public class HealingCharmItem extends Item { public HealingCharmItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.heal(4.0F); player.getCooldowns().addCooldown(this, 40); } return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as HealingCharmItem: 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.useInteractionResultHolder.sidedSuccessLivingEntity.healItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/HealingCharmItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Healing Charm: right-click to heal, then wait for the cooldown. use runs in the client and on the server; the game changes on the server.public class HealingCharmItem extends Item { public HealingCharmItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { player.heal(4.0F); player.getCooldowns().addCooldown(stack, 40); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as HealingCharmItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.useActionResult.sidedSuccessLivingEntity.healCooldownTracker.addCooldown
src/main/java/com/example/examplemod/item/HealingCharmItem.java
package com.example.examplemod.item;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Healing Charm: right-click to heal, then wait for the cooldown. use runs in the client and on the server; the game changes on the server.public class HealingCharmItem extends Item { public HealingCharmItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack stack = player.getItemInHand(hand); if (!world.isClientSide()) { player.heal(4.0F); player.getCooldowns().addCooldown(this, 40); } return ActionResult.sidedSuccess(stack, world.isClientSide()); }}It’s registered like any other item, as HealingCharmItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.useActionResult.sidedSuccessEntityLivingBase.healCooldownTracker.addCooldown
In 1.12.2 the method is onItemRightClick(world, player, hand), it returns new ActionResult<>(EnumActionResult.SUCCESS, stack), and the item has no Item.Properties constructor.
src/main/java/com/example/examplemod/item/HealingCharmItem.java
package com.example.examplemod.item;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.EntityPlayer;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Healing Charm: right-click to heal, then wait for the cooldown. use runs in the client and on the server; the game changes on the server.public class HealingCharmItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!world.isRemote) { player.heal(4.0F); player.getCooldownTracker().setCooldown(this, 40); } return new ActionResult<>(EnumActionResult.SUCCESS, stack); }}It’s registered like any other item, as HealingCharmItem: 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 gives a potion effect
- A block that does something when clicked
- A mob with attributes and a spawn egg
- A sword that sets targets on fire
- An item that changes the block you click
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.