Skip to content

How to make an item that heals you when you use it (Minecraft mod)

LivingEntity.heal(amount) adds health, counted in half hearts, and stops at max health. Run it on the server. A one-use item removes itself with stack.shrink(1). Skip creative players (player.getAbilities().instabuild) so the item stays.

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. Override use and work only on the server.
  2. Call player.heal with the number of half hearts.
  3. Shrink the stack by one unless the player is in creative mode.
  4. Add a cooldown.

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

Setting In the example What it does
amount 6 Half hearts to heal. 2 is one heart.

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.17.1 to 1.20.1, Fabric 1.17 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.17.1 to 1.20.1, Fabric 1.17 to 1.20.3”

Uses:

  • LivingEntity.heal
  • ItemStack.shrink
  • ItemCooldowns.addCooldown

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

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Bandage: right-click to heal 6.0F half hearts and use up one item.
public class BandageItem extends Item {
public BandageItem(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.getHealth() < player.getMaxHealth()) {
player.heal(6.0F);
player.getCooldowns().addCooldown(this, 20);
if (!player.getAbilities().instabuild) {
stack.shrink(1);
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

Uses:

  • LivingEntity.heal
  • ItemStack.shrink
  • ItemCooldowns.addCooldown

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

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Bandage: right-click to heal 6.0F half hearts and use up one item.
public class BandageItem extends Item {
public BandageItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
ItemStack stack = player.getItemInHand(hand);
if (!level.isClientSide() && player.getHealth() < player.getMaxHealth()) {
player.heal(6.0F);
player.getCooldowns().addCooldown(stack, 20);
if (!player.getAbilities().instabuild) {
stack.shrink(1);
}
}
return InteractionResult.SUCCESS;
}
}

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

Uses:

  • LivingEntity.heal
  • ItemStack.shrink
  • CooldownTracker.addCooldown

In 1.16.5 the creative check is player.abilities.instabuild.

src/main/java/com/example/examplemod/item/BandageItem.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;
// Bandage: right-click to heal 6.0F half hearts and use up one item.
public class BandageItem extends Item {
public BandageItem(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.getHealth() < player.getMaxHealth()) {
player.heal(6.0F);
player.getCooldowns().addCooldown(this, 20);
if (!player.abilities.instabuild) {
stack.shrink(1);
}
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

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

Uses:

  • LivingEntity.heal
  • ItemStack.shrink
  • ItemCooldowns.addCooldown

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

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Bandage: right-click to heal 6.0F half hearts and use up one item.
public class BandageItem extends Item {
public BandageItem(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.getHealth() < player.getMaxHealth()) {
player.heal(6.0F);
player.getCooldowns().addCooldown(this, 20);
if (!player.abilities.instabuild) {
stack.shrink(1);
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

Uses:

  • EntityLivingBase.heal
  • ItemStack.shrink
  • CooldownTracker.addCooldown

In 1.12.2 the creative check is player.capabilities.isCreativeMode.

src/main/java/com/example/examplemod/item/BandageItem.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;
// Bandage: right-click to heal 6.0F half hearts and use up one item.
public class BandageItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
ItemStack stack = player.getHeldItem(hand);
if (!world.isRemote && player.getHealth() < player.getMaxHealth()) {
player.heal(6.0F);
player.getCooldownTracker().setCooldown(this, 20);
if (!player.capabilities.isCreativeMode) {
stack.shrink(1);
}
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

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