Skip to content

How to make an item that places blocks in a line (Minecraft mod)

Placing a block in the world is one call on the level: setBlockAndUpdate(pos, state) puts the block there and updates its neighbours. Call it only on the server. Get the state from Blocks.STONE.defaultBlockState(), or from your own block’s defaultBlockState(). Replace only air, or you delete what the player built.

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. Override use and work only on the server.
  2. Start at the player’s feet and step along the facing direction.
  3. Where the position is empty (isEmptyBlock), place the block.
  4. Add a cooldown.

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.setBlockAndUpdate
  • Level.isEmptyBlock
  • Blocks.STONE

src/main/java/com/example/examplemod/item/BridgeWandItem.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;
// Bridge Wand: right-click to lay 8 blocks of stone ahead of you, one block below your feet. Only air is replaced.
public class BridgeWandItem extends Item {
public BridgeWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
BlockPos start = player.blockPosition().below();
for (int i = 1; i <= 8; i++) {
BlockPos pos = start.relative(player.getDirection(), i);
if (level.isEmptyBlock(pos)) {
level.setBlockAndUpdate(pos, Blocks.STONE.defaultBlockState());
}
}
player.getCooldowns().addCooldown(this, 20);
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

It’s registered like any other item, as BridgeWandItem: see a new item.

Uses:

  • Level.setBlockAndUpdate
  • Level.isEmptyBlock
  • Blocks.STONE

src/main/java/com/example/examplemod/item/BridgeWandItem.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;
// Bridge Wand: right-click to lay 8 blocks of stone ahead of you, one block below your feet. Only air is replaced.
public class BridgeWandItem extends Item {
public BridgeWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
BlockPos start = player.blockPosition().below();
for (int i = 1; i <= 8; i++) {
BlockPos pos = start.relative(player.getDirection(), i);
if (level.isEmptyBlock(pos)) {
level.setBlockAndUpdate(pos, Blocks.STONE.defaultBlockState());
}
}
player.getCooldowns().addCooldown(player.getItemInHand(hand), 20);
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as BridgeWandItem: see a new item.

Uses:

  • World.setBlockAndUpdate
  • World.isEmptyBlock
  • Blocks.STONE

src/main/java/com/example/examplemod/item/BridgeWandItem.java

package com.example.examplemod.item;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.block.Blocks;
// Bridge Wand: right-click to lay 8 blocks of stone ahead of you, one block below your feet. Only air is replaced.
public class BridgeWandItem extends Item {
public BridgeWandItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClientSide()) {
BlockPos start = player.blockPosition().below();
for (int i = 1; i <= 8; i++) {
BlockPos pos = start.relative(player.getDirection(), i);
if (world.isEmptyBlock(pos)) {
world.setBlockAndUpdate(pos, Blocks.STONE.defaultBlockState());
}
}
player.getCooldowns().addCooldown(this, 20);
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

It’s registered like any other item, as BridgeWandItem: see a new item.

Uses:

  • World.setBlockAndUpdate
  • World.isEmptyBlock
  • Blocks.STONE

In 1.12.2 the item overrides onItemRightClick, the checks are world.isAirBlock(pos) and world.setBlockState(pos, state), and the state comes from Blocks.STONE.getDefaultState().

src/main/java/com/example/examplemod/item/BridgeWandItem.java

package com.example.examplemod.item;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.init.Blocks;
// Bridge Wand: right-click to lay 8 blocks of stone ahead of you, one block below your feet. Only air is replaced.
public class BridgeWandItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (!world.isRemote) {
BlockPos start = player.getPosition().down();
for (int i = 1; i <= 8; i++) {
BlockPos pos = start.offset(player.getHorizontalFacing(), i);
if (world.isAirBlock(pos)) {
world.setBlockState(pos, Blocks.STONE.getDefaultState());
}
}
player.getCooldownTracker().setCooldown(this, 20);
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

It’s registered like any other item, as BridgeWandItem: see a new item.

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.