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.
- Set the durability in the item’s properties (.durability(n)).
- In the action (useOn, hurtEnemy), call stack.hurtAndBreak(1, …) on the server.
- Make the hand that held the item take the damage (LivingEntity.getSlotForHand(hand) on 1.20.5 to 1.21.8).
- Add a repair recipe or let an anvil repair it with an ingredient.
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 |
|---|---|---|
| uses | 100 |
How many points of damage it takes before it breaks. |
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.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.durabilityItemStack.hurtAndBreakLivingEntity.broadcastBreakEventItem.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.
Fabric and NeoForge 1.20.5 to 1.21.1
Section titled “Fabric and NeoForge 1.20.5 to 1.21.1”Uses:
Item.Properties.durabilityItemStack.hurtAndBreakLivingEntity.getSlotForHandItem.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.
Fabric and NeoForge 1.21.2 to 1.21.8
Section titled “Fabric and NeoForge 1.21.2 to 1.21.8”Uses:
Item.Properties.durabilityItemStack.hurtAndBreakLivingEntity.getSlotForHandItem.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.
Fabric and NeoForge 1.21.9 to 26.3
Section titled “Fabric and NeoForge 1.21.9 to 26.3”Uses:
Item.Properties.durabilityItemStack.hurtAndBreakItem.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.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.Properties.durabilityItemStack.hurtAndBreakLivingEntity.broadcastBreakEventItem.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.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.Properties.durabilityItemStack.hurtAndBreakEntityLivingBase.broadcastBreakEventItem.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.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/items/first-item
- https://docs.neoforged.net/docs/1.21.1/items/
Related how-tos
Section titled “Related how-tos”- A food item
- A tool tier of your own
- An item with the properties you choose
- Food that gives a potion effect
- Store a value on an item with a data component
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.