How to react when a player right-clicks a block (Minecraft mod)
All loaders have an event for a right-click on a block. Fabric API: UseBlockCallback.EVENT, whose handler returns an InteractionResult (PASS to carry on, SUCCESS or FAIL to stop it). Forge and NeoForge: PlayerInteractEvent.RightClickBlock on the game event bus, which you stop with setCanceled(true). The event fires on both sides, and once per hand, so check the side and the hand.
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.
- Register a listener for your loader’s event.
- Return early on the client and for the off hand.
- Read the clicked block from the position and test it (state.is(Blocks.X); getBlock() == Blocks.X on 1.12.2).
- Do your work, then right-click the block.
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 |
|---|---|---|
| block | DIAMOND_BLOCK |
The block that reacts. |
| message | You touched it! |
What the player sees. |
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 1.19 to 1.21.1, Fabric 1.21.2 to 1.21.8
Section titled “Fabric 1.19 to 1.21.1, Fabric 1.21.2 to 1.21.8”Uses:
UseBlockCallback.EVENTBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionResult;import net.minecraft.world.level.block.Blocks;
// Right-clicking DIAMOND_BLOCK shows a message (Fabric API: UseBlockCallback). Returning PASS lets the click go on as normal.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's onInitialize. public static void register() { UseBlockCallback.EVENT.register((player, world, hand, hit) -> { if (!world.isClientSide && world.getBlockState(hit.getBlockPos()).is(Blocks.DIAMOND_BLOCK)) { player.displayClientMessage(Component.literal("You touched it!"), true); } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
ClickWatchClick.register();NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 1.21.11
Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 1.21.11”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.level.block.Blocks;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (NeoForge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getLevel().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getEntity().displayClientMessage(Component.literal("You touched it!"), true); } }}In your main mod class, inside <init>:
ClickWatchClick.register();Fabric 1.21.9 to 1.21.11
Section titled “Fabric 1.21.9 to 1.21.11”Uses:
UseBlockCallback.EVENTBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionResult;import net.minecraft.world.level.block.Blocks;
// Right-clicking DIAMOND_BLOCK shows a message (Fabric API: UseBlockCallback). Returning PASS lets the click go on as normal.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's onInitialize. public static void register() { UseBlockCallback.EVENT.register((player, world, hand, hit) -> { if (!world.isClientSide() && world.getBlockState(hit.getBlockPos()).is(Blocks.DIAMOND_BLOCK)) { player.displayClientMessage(Component.literal("You touched it!"), true); } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
ClickWatchClick.register();NeoForge 26.1 to 26.3
Section titled “NeoForge 26.1 to 26.3”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.level.block.Blocks;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (NeoForge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getLevel().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getEntity().sendOverlayMessage(Component.literal("You touched it!")); } }}In your main mod class, inside <init>:
ClickWatchClick.register();Fabric 26.1 to 26.3
Section titled “Fabric 26.1 to 26.3”Uses:
UseBlockCallback.EVENTBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionResult;import net.minecraft.world.level.block.Blocks;
// Right-clicking DIAMOND_BLOCK shows a message (Fabric API: UseBlockCallback). Returning PASS lets the click go on as normal.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's onInitialize. public static void register() { UseBlockCallback.EVENT.register((player, world, hand, hit) -> { if (!world.isClientSide() && world.getBlockState(hit.getBlockPos()).is(Blocks.DIAMOND_BLOCK)) { player.sendOverlayMessage(Component.literal("You touched it!")); } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
ClickWatchClick.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.TextComponent;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.level.block.Blocks;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (Forge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getWorld().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getWorld().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getPlayer().displayClientMessage(new TextComponent("You touched it!"), true); } }}In your main mod class, inside <init>:
ClickWatchClick.register();Forge 1.19 to 1.20.1
Section titled “Forge 1.19 to 1.20.1”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.level.block.Blocks;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (Forge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getLevel().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getEntity().displayClientMessage(Component.literal("You touched it!"), true); } }}In your main mod class, inside <init>:
ClickWatchClick.register();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.level.block.Blocks;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (NeoForge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getLevel().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getEntity().displayClientMessage(Component.literal("You touched it!"), true); } }}In your main mod class, inside <init>:
ClickWatchClick.register();Fabric 1.16.5 to 1.18.2
Section titled “Fabric 1.16.5 to 1.18.2”Uses:
UseBlockCallback.EVENTBlockState.isPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.network.chat.TextComponent;import net.fabricmc.fabric.api.event.player.UseBlockCallback;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionResult;import net.minecraft.world.level.block.Blocks;
// Right-clicking DIAMOND_BLOCK shows a message (Fabric API: UseBlockCallback). Returning PASS lets the click go on as normal.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's onInitialize. public static void register() { UseBlockCallback.EVENT.register((player, world, hand, hit) -> { if (!world.isClientSide && world.getBlockState(hit.getBlockPos()).is(Blocks.DIAMOND_BLOCK)) { player.displayClientMessage(new TextComponent("You touched it!"), true); } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
ClickWatchClick.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
PlayerInteractEvent.RightClickBlockBlockState.isPlayerEntity.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.Hand;import net.minecraft.block.Blocks;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-clicking DIAMOND_BLOCK shows a message (Forge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.public final class ClickWatchClick { private ClickWatchClick() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(ClickWatchClick::onClick); }
private static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getWorld().isClientSide() && event.getHand() == Hand.MAIN_HAND && event.getWorld().getBlockState(event.getPos()).is(Blocks.DIAMOND_BLOCK)) { event.getPlayer().displayClientMessage(new StringTextComponent("You touched it!"), true); } }}In your main mod class, inside <init>:
ClickWatchClick.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
PlayerInteractEvent.RightClickBlockIBlockState.isEntityPlayer.displayClientMessage
src/main/java/com/example/examplemod/event/ClickWatchClick.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.init.Blocks;import net.minecraft.util.EnumHand;import net.minecraft.util.text.TextComponentString;import net.minecraftforge.event.entity.player.PlayerInteractEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// Right-clicking DIAMOND_BLOCK shows a message (Forge: PlayerInteractEvent.RightClickBlock). It fires once per hand, so the main hand is checked.@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class ClickWatchClick { private ClickWatchClick() { }
@SubscribeEvent public static void onClick(PlayerInteractEvent.RightClickBlock event) { if (!event.getWorld().isRemote && event.getHand() == EnumHand.MAIN_HAND && event.getWorld().getBlockState(event.getPos()).getBlock() == Blocks.DIAMOND_BLOCK) { event.getEntityPlayer().sendStatusMessage(new TextComponentString("You touched it!"), true); } }}Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/concepts/events
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/event/player/UseBlockCallback.html
Related how-tos
Section titled “Related how-tos”- Do something when a player joins
- Give players items when they first join
- React to chat messages
- React when a player breaks a block
- React when something takes damage
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.