Skip to content

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.

  1. Make a class that extends Item with a constructor taking Item.Properties.
  2. Override use(…) and do the work on the server side.
  3. Add a cooldown, then return the success result.
  4. Register the item with your class.

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).

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.use
  • InteractionResultHolder.sidedSuccess
  • LivingEntity.heal
  • ItemCooldowns.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.

Uses:

  • Item.use
  • InteractionResultHolder.sidedSuccess
  • LivingEntity.heal
  • ItemCooldowns.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.

Uses:

  • Item.use
  • ActionResult.sidedSuccess
  • LivingEntity.heal
  • CooldownTracker.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.

Uses:

  • Item.use
  • ActionResult.sidedSuccess
  • EntityLivingBase.heal
  • CooldownTracker.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.

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.