How to make an item that changes the block you click (Minecraft mod)
When a player right-clicks a block with an item, the item’s useOn(UseOnContext) runs first (onItemUse on 1.12.2). The context gives you the level, the clicked position (getClickedPos), the face (getClickedFace), the player (it can be null) and the stack. Return PASS to let the block’s own click run, or a success result to say you handled it. Change the world only on the server.
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 and override useOn.
- Read the clicked block with context.getLevel().getBlockState(context.getClickedPos()).
- Return PASS when it is not the block you want.
- On the server, call level.setBlockAndUpdate(pos, newState).
- Return the success result (InteractionResult.sidedSuccess(level.isClientSide()) before 1.21.2, InteractionResult.SUCCESS from 1.21.2).
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 |
|---|---|---|
| target | STONE |
The block it works on. |
| result | GOLD_BLOCK |
The block it makes. |
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.useOnUseOnContext.getClickedPosLevel.setBlockAndUpdateInteractionResult.sidedSuccess
src/main/java/com/example/examplemod/item/MidasWandItem.java
package com.example.examplemod.item;
import net.minecraft.core.BlockPos;import net.minecraft.world.InteractionResult;import net.minecraft.world.item.Item;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;
// Midas Wand: right-click STONE to turn it into GOLD_BLOCK. useOn runs in the client and on the server; the change is made on the server.public class MidasWandItem extends Item { public MidasWandItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { Level level = context.getLevel(); BlockPos pos = context.getClickedPos(); if (!level.getBlockState(pos).is(Blocks.STONE)) { return InteractionResult.PASS; } if (!level.isClientSide()) { level.setBlockAndUpdate(pos, Blocks.GOLD_BLOCK.defaultBlockState()); } return InteractionResult.sidedSuccess(level.isClientSide()); }}It’s registered like any other item, as MidasWandItem: 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.useOnUseOnContext.getClickedPosLevel.setBlockAndUpdateInteractionResult.sidedSuccess
src/main/java/com/example/examplemod/item/MidasWandItem.java
package com.example.examplemod.item;
import net.minecraft.core.BlockPos;import net.minecraft.world.InteractionResult;import net.minecraft.world.item.Item;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;
// Midas Wand: right-click STONE to turn it into GOLD_BLOCK. useOn runs in the client and on the server; the change is made on the server.public class MidasWandItem extends Item { public MidasWandItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { Level level = context.getLevel(); BlockPos pos = context.getClickedPos(); if (!level.getBlockState(pos).is(Blocks.STONE)) { return InteractionResult.PASS; } if (!level.isClientSide()) { level.setBlockAndUpdate(pos, Blocks.GOLD_BLOCK.defaultBlockState()); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as MidasWandItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.useOnItemUseContext.getClickedPosWorld.setBlockAndUpdateActionResultType.sidedSuccess
src/main/java/com/example/examplemod/item/MidasWandItem.java
package com.example.examplemod.item;
import net.minecraft.util.math.BlockPos;import net.minecraft.util.ActionResultType;import net.minecraft.item.Item;import net.minecraft.item.ItemUseContext;import net.minecraft.world.World;import net.minecraft.block.Blocks;
// Midas Wand: right-click STONE to turn it into GOLD_BLOCK. useOn runs in the client and on the server; the change is made on the server.public class MidasWandItem extends Item { public MidasWandItem(Item.Properties properties) { super(properties); }
@Override public ActionResultType useOn(ItemUseContext context) { World world = context.getLevel(); BlockPos pos = context.getClickedPos(); if (!world.getBlockState(pos).is(Blocks.STONE)) { return ActionResultType.PASS; } if (!world.isClientSide()) { world.setBlockAndUpdate(pos, Blocks.GOLD_BLOCK.defaultBlockState()); } return ActionResultType.sidedSuccess(world.isClientSide()); }}It’s registered like any other item, as MidasWandItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.useOnItem.getClickedPosWorld.setBlockAndUpdateEnumActionResult.sidedSuccess
In 1.12.2 the method is onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ), the world call is setBlockState, and the result is EnumActionResult.SUCCESS.
src/main/java/com/example/examplemod/item/MidasWandItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;import net.minecraft.init.Blocks;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;
// Midas Wand: right-click STONE to turn it into GOLD_BLOCK. onItemUse runs in the client and on the server; the change is made on the server.public class MidasWandItem extends Item { @Override public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (world.getBlockState(pos).getBlock() != Blocks.STONE) { return EnumActionResult.PASS; } if (!world.isRemote) { world.setBlockState(pos, Blocks.GOLD_BLOCK.getDefaultState()); } return EnumActionResult.SUCCESS; }}It’s registered like any other item, as MidasWandItem: 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”- A tool that breaks a 3 by 3 area
- An item that places blocks in a line
- An item you use on a mob or player
- A wand that strikes lightning where you look
- An item that does something when used
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.