How to make a tool that breaks a 3 by 3 area in a Minecraft mod
level.destroyBlock(pos, true) breaks a block and drops its items, like a player mined it. Loop over the neighbours of the block that was clicked and call it for each one. This example breaks the 3 by 3 square around the clicked block on right-click. Skip unbreakable blocks by checking that the block’s destroy speed is not negative (getDestroySpeed(level, pos) >= 0).
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.
- Override the use-on-block method (useOn; onItemUse on 1.12.2) and work only on the server.
- Loop dx and dz from -1 to 1 around the clicked position.
- Skip blocks with a destroy speed under 0, such as bedrock.
- Call level.destroyBlock(pos, true) for each.
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.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:
Level.destroyBlockBlockState.getDestroySpeedUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/BreakerHammerItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Breaker Hammer: right-click a block to break it and the eight blocks around it, on the same height.public class BreakerHammerItem extends Item { public BreakerHammerItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { Level level = context.getLevel(); if (!level.isClientSide()) { BlockPos center = context.getClickedPos(); for (int dx = -1; dx <= 1; dx++) { for (int dz = -1; dz <= 1; dz++) { BlockPos pos = center.offset(dx, 0, dz); if (level.getBlockState(pos).getDestroySpeed(level, pos) >= 0) { level.destroyBlock(pos, true); } } } } return InteractionResult.sidedSuccess(level.isClientSide()); }}It’s registered like any other item, as BreakerHammerItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
Level.destroyBlockBlockState.getDestroySpeedUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/BreakerHammerItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Breaker Hammer: right-click a block to break it and the eight blocks around it, on the same height.public class BreakerHammerItem extends Item { public BreakerHammerItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { Level level = context.getLevel(); if (!level.isClientSide()) { BlockPos center = context.getClickedPos(); for (int dx = -1; dx <= 1; dx++) { for (int dz = -1; dz <= 1; dz++) { BlockPos pos = center.offset(dx, 0, dz); if (level.getBlockState(pos).getDestroySpeed(level, pos) >= 0) { level.destroyBlock(pos, true); } } } } return InteractionResult.SUCCESS; }}It’s registered like any other item, as BreakerHammerItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
World.destroyBlockBlockState.getDestroySpeedItemUseContext.getClickedPos
src/main/java/com/example/examplemod/item/BreakerHammerItem.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;
// Breaker Hammer: right-click a block to break it and the eight blocks around it, on the same height.public class BreakerHammerItem extends Item { public BreakerHammerItem(Item.Properties properties) { super(properties); }
@Override public ActionResultType useOn(ItemUseContext context) { World world = context.getLevel(); if (!world.isClientSide()) { BlockPos center = context.getClickedPos(); for (int dx = -1; dx <= 1; dx++) { for (int dz = -1; dz <= 1; dz++) { BlockPos pos = center.offset(dx, 0, dz); if (world.getBlockState(pos).getDestroySpeed(world, pos) >= 0) { world.destroyBlock(pos, true); } } } } return ActionResultType.sidedSuccess(world.isClientSide()); }}It’s registered like any other item, as BreakerHammerItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
World.destroyBlockIBlockState.getDestroySpeedItem.getClickedPos
In 1.12.2 the method is onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ), and the check is state.getBlockHardness(world, pos) >= 0.
src/main/java/com/example/examplemod/item/BreakerHammerItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;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;
// Breaker Hammer: right-click a block to break it and the eight blocks around it, on the same height.public class BreakerHammerItem extends Item { @Override public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos center, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (!world.isRemote) { for (int dx = -1; dx <= 1; dx++) { for (int dz = -1; dz <= 1; dz++) { BlockPos pos = center.add(dx, 0, dz); if (world.getBlockState(pos).getBlockHardness(world, pos) >= 0) { world.destroyBlock(pos, true); } } } } return EnumActionResult.SUCCESS; }}It’s registered like any other item, as BreakerHammerItem: see a new item.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- An item that changes the block you click
- An item that places blocks in a line
- A wand that strikes lightning where you look
- An item that explodes where you aim
- An item that fills the hunger bar
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.