Skip to content

How to make a block that does something when clicked (Minecraft mod)

A block’s own class overrides the right-click method. On 1.20.5 and newer that is useWithoutItem(state, level, pos, player, hit), which runs when a player right-clicks with an empty hand or an item that does nothing there (useItemOn comes first for items); before that it is one use(…) method. Change the world only when !level.isClientSide(), and answer with a success result: InteractionResult.sidedSuccess(level.isClientSide()) before 1.21.2, InteractionResult.SUCCESS from 1.21.2. From 1.20.5 to 26.2 every block class also has a codec: simpleCodec(YourBlock::new).

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 Block with a constructor taking the block properties (and a codec on 1.20.5 to 26.2).
  2. Override the right-click method (useWithoutItem, or use before 1.20.5) and do the work on the server.
  3. Return the success result for your version.
  4. Register the block with your class.

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

Setting In the example What it does
message Ding dong! What the player sees when they click it.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. useWithoutItem runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public static final MapCodec<DoorBellBlock> CODEC = simpleCodec(DoorBellBlock::new);
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. useWithoutItem runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public static final MapCodec<DoorBellBlock> CODEC = simpleCodec(DoorBellBlock::new);
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.SUCCESS
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Player.sendOverlayMessage

Since 26.1 displayClientMessage(text, true) is sendOverlayMessage(text) (the action bar).

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. useWithoutItem runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public static final MapCodec<DoorBellBlock> CODEC = simpleCodec(DoorBellBlock::new);
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.sendOverlayMessage(Component.literal("Ding dong!"));
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.SUCCESS
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Player.sendOverlayMessage

Since 26.1 displayClientMessage(text, true) is sendOverlayMessage(text) (the action bar). Since 26.3 a Block no longer has a codec: there is no CODEC field and no codec() method to write.

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. useWithoutItem runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.sendOverlayMessage(Component.literal("Ding dong!"));
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.use
  • InteractionResult.sidedSuccess
  • Level.playSound

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL, SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(new TextComponent("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.use
  • InteractionResult.sidedSuccess
  • Level.playSound

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL, SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.use
  • InteractionResult.sidedSuccess
  • Level.playSound

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL, SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • AbstractBlock.use
  • ActionResultType.sidedSuccess
  • World.playSound

In 1.16.5 the method is use(…) and it returns an ActionResultType.

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResultType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.World;
import net.minecraft.block.Block;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockRayTraceResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(AbstractBlock.Properties properties) {
super(properties);
}
@Override
public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
if (!world.isClientSide()) {
world.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL, SoundCategory.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(new StringTextComponent("Ding dong!"), true);
}
return ActionResultType.sidedSuccess(world.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL, SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(new TextComponent("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • BlockBehaviour.useWithoutItem
  • InteractionResult.sidedSuccess
  • Level.playSound
  • SoundEvents.NOTE_BLOCK_BELL
  • Block.simpleCodec

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Door Bell: right-click it to ring a bell. use runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!level.isClientSide()) {
level.playSound(null, pos, SoundEvents.NOTE_BLOCK_BELL.value(), SoundSource.BLOCKS, 1.0F, 1.0F);
player.displayClientMessage(Component.literal("Ding dong!"), true);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

Uses:

  • Block.onBlockActivated
  • EntityPlayer.sendStatusMessage
  • World.playSound

In 1.12.2 the method is onBlockActivated(…) and it returns a boolean.

src/main/java/com/example/examplemod/block/DoorBellBlock.java

package com.example.examplemod.block;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
// Door Bell: right-click it to ring a bell. onBlockActivated runs in the client and on the server; the sound and message come from the server.
public class DoorBellBlock extends Block {
public DoorBellBlock() {
super(Material.ROCK);
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (!world.isRemote) {
world.playSound(null, pos, SoundEvents.BLOCK_NOTE_BELL, SoundCategory.BLOCKS, 1.0F, 1.0F);
player.sendStatusMessage(new TextComponentString("Ding dong!"), true);
}
return true;
}
}

It’s registered like any other block, as DoorBellBlock: see a new block.

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.