Skip to content

How to make an item that wears out in a Minecraft mod

An item wears out when its properties set a durability: new Item.Properties().durability(250) (setMaxDamage on 1.12.2). The game draws the bar and breaks the item at zero. Nothing uses it up by itself, so your item calls stack.hurtAndBreak(…) when it does its job; the arguments are the amount and the user, plus a callback before 1.20.5, the equipment slot from 1.20.5, and the hand from 1.21.10. Tools and armour from a Tier take their durability from the tier 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.

  1. Set the durability in the item’s properties (.durability(n)).
  2. In the action (useOn, hurtEnemy), call stack.hurtAndBreak(1, …) on the server.
  3. Make the hand that held the item take the damage (LivingEntity.getSlotForHand(hand) on 1.20.5 to 1.21.8).
  4. Add a repair recipe or let an anvil repair it with an ingredient.

The example uses these values. Change them in the code.

Setting In the example What it does
uses 100 How many points of damage it takes before it breaks.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1

Section titled “Fabric and NeoForge 1.20.1 to 1.20.4, Fabric 1.16.5 to 1.20, Forge 1.17.1 to 1.20.1”

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • LivingEntity.broadcastBreakEvent
  • Item.useOn

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem(Item.Properties properties) {
super(properties.durability(100));
}
@Override
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
if (!context.getLevel().isClientSide() && player != null) {
context.getItemInHand().hurtAndBreak(1, player, user -> user.broadcastBreakEvent(context.getHand()));
}
return InteractionResult.sidedSuccess(context.getLevel().isClientSide());
}
}

It’s registered like any other item, as WornChiselItem: see a new item.

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • LivingEntity.getSlotForHand
  • Item.useOn

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem(Item.Properties properties) {
super(properties.durability(100));
}
@Override
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
if (!context.getLevel().isClientSide() && player != null) {
context.getItemInHand().hurtAndBreak(1, player, LivingEntity.getSlotForHand(context.getHand()));
}
return InteractionResult.sidedSuccess(context.getLevel().isClientSide());
}
}

It’s registered like any other item, as WornChiselItem: see a new item.

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • LivingEntity.getSlotForHand
  • Item.useOn

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem(Item.Properties properties) {
super(properties.durability(100));
}
@Override
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
if (!context.getLevel().isClientSide() && player != null) {
context.getItemInHand().hurtAndBreak(1, player, LivingEntity.getSlotForHand(context.getHand()));
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as WornChiselItem: see a new item.

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • Item.useOn

Since 1.21.10 hurtAndBreak takes the InteractionHand directly, so LivingEntity.getSlotForHand is gone.

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem(Item.Properties properties) {
super(properties.durability(100));
}
@Override
public InteractionResult useOn(UseOnContext context) {
Player player = context.getPlayer();
if (!context.getLevel().isClientSide() && player != null) {
context.getItemInHand().hurtAndBreak(1, player, context.getHand());
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as WornChiselItem: see a new item.

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • LivingEntity.broadcastBreakEvent
  • Item.useOn

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.util.ActionResultType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUseContext;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem(Item.Properties properties) {
super(properties.durability(100));
}
@Override
public ActionResultType useOn(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
if (!context.getLevel().isClientSide() && player != null) {
context.getItemInHand().hurtAndBreak(1, player, user -> user.broadcastBreakEvent(context.getHand()));
}
return ActionResultType.sidedSuccess(context.getLevel().isClientSide());
}
}

It’s registered like any other item, as WornChiselItem: see a new item.

Uses:

  • Item.Properties.durability
  • ItemStack.hurtAndBreak
  • EntityLivingBase.broadcastBreakEvent
  • Item.useOn

In 1.12.2 the durability is setMaxDamage(n) in the item’s constructor, and the call is stack.damageItem(1, player).

src/main/java/com/example/examplemod/item/WornChiselItem.java

package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
// Worn Chisel: it has 100 uses. Each right-click on a block costs one point of durability; at zero it breaks.
public class WornChiselItem extends Item {
public WornChiselItem() {
setMaxStackSize(1);
setMaxDamage(100);
}
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {
player.getHeldItem(hand).damageItem(1, player);
}
return EnumActionResult.SUCCESS;
}
}

It’s registered like any other item, as WornChiselItem: 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.