Skip to content

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.

  1. Make a class that extends Item and override useOn.
  2. Read the clicked block with context.getLevel().getBlockState(context.getClickedPos()).
  3. Return PASS when it is not the block you want.
  4. On the server, call level.setBlockAndUpdate(pos, newState).
  5. Return the success result (InteractionResult.sidedSuccess(level.isClientSide()) before 1.21.2, InteractionResult.SUCCESS from 1.21.2).

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.

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.useOn
  • UseOnContext.getClickedPos
  • Level.setBlockAndUpdate
  • InteractionResult.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.

Uses:

  • Item.useOn
  • UseOnContext.getClickedPos
  • Level.setBlockAndUpdate
  • InteractionResult.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.

Uses:

  • Item.useOn
  • ItemUseContext.getClickedPos
  • World.setBlockAndUpdate
  • ActionResultType.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.

Uses:

  • Item.useOn
  • Item.getClickedPos
  • World.setBlockAndUpdate
  • EnumActionResult.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.

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.