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.
- Make a class that extends Block with a constructor taking the block properties (and a codec on 1.20.5 to 26.2).
- Override the right-click method (useWithoutItem, or use before 1.20.5) and do the work on the server.
- Return the success result for your version.
- Register the block with your class.
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 |
|---|---|---|
| message | Ding dong! |
What the player sees when they click it. |
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.1 to 1.20.4
Section titled “Fabric and NeoForge 1.20.1 to 1.20.4”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Fabric and NeoForge 1.20.5 to 1.21.1
Section titled “Fabric and NeoForge 1.20.5 to 1.21.1”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Fabric and NeoForge 1.21.2 to 1.21.11
Section titled “Fabric and NeoForge 1.21.2 to 1.21.11”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Fabric and NeoForge 26.1 to 26.2
Section titled “Fabric and NeoForge 26.1 to 26.2”Uses:
BlockBehaviour.useWithoutItemInteractionResult.SUCCESSLevel.playSoundSoundEvents.NOTE_BLOCK_BELLPlayer.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.
Fabric and NeoForge 26.3
Section titled “Fabric and NeoForge 26.3”Uses:
BlockBehaviour.useWithoutItemInteractionResult.SUCCESSLevel.playSoundSoundEvents.NOTE_BLOCK_BELLPlayer.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.
Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
BlockBehaviour.useInteractionResult.sidedSuccessLevel.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.
Forge 1.19 to 1.19.2
Section titled “Forge 1.19 to 1.19.2”Uses:
BlockBehaviour.useInteractionResult.sidedSuccessLevel.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.
Forge 1.19.3 to 1.20.1
Section titled “Forge 1.19.3 to 1.20.1”Uses:
BlockBehaviour.useInteractionResult.sidedSuccessLevel.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.
Fabric 1.19 to 1.19.2
Section titled “Fabric 1.19 to 1.19.2”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
AbstractBlock.useActionResultType.sidedSuccessWorld.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.
Fabric 1.16.5 to 1.18.2
Section titled “Fabric 1.16.5 to 1.18.2”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Fabric 1.19.3 to 1.20
Section titled “Fabric 1.19.3 to 1.20”Uses:
BlockBehaviour.useWithoutItemInteractionResult.sidedSuccessLevel.playSoundSoundEvents.NOTE_BLOCK_BELLBlock.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.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Block.onBlockActivatedEntityPlayer.sendStatusMessageWorld.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.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/blocks/blockstates
- https://docs.neoforged.net/docs/1.21.1/items/interactionpipeline
Related how-tos
Section titled “Related how-tos”- A block that gives off particles
- A mob with attributes and a spawn egg
- An item that does something when used
- An item that gives a potion effect
- A block that faces the player
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.