Skip to content

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.

  1. Register a listener for your loader’s event.
  2. Return early on the client and for the off hand.
  3. Read the clicked block from the position and test it (state.is(Blocks.X); getBlock() == Blocks.X on 1.12.2).
  4. Do your work, then right-click the block.

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.

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.EVENT
  • BlockState.is
  • Player.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.RightClickBlock
  • BlockState.is
  • Player.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();

Uses:

  • UseBlockCallback.EVENT
  • BlockState.is
  • Player.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • BlockState.is
  • Player.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();

Uses:

  • UseBlockCallback.EVENT
  • BlockState.is
  • Player.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • BlockState.is
  • Player.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • BlockState.is
  • Player.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • BlockState.is
  • Player.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();

Uses:

  • UseBlockCallback.EVENT
  • BlockState.is
  • Player.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • BlockState.is
  • PlayerEntity.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();

Uses:

  • PlayerInteractEvent.RightClickBlock
  • IBlockState.is
  • EntityPlayer.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);
}
}
}

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.