Skip to content

Fix a crash when a block is placed in a Minecraft mod

A crash on placing a block comes from code that runs at that moment: setPlacedBy (onBlockPlacedBy on 1.12.2), onPlace, getStateForPlacement or a block entity. Open the newest file in crash-reports. Read the line under ‘Description’ and the first stack line that names your package. The usual cause is a null: setPlacedBy gets no placer when a dispenser or a command places the block, and the block entity at a position can be null. Test with instanceof, which is false for null, and change the world only on the server.

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. Open the newest file in the crash-reports folder of your run directory.
  2. Find the first line that names your mod’s package. That line and its number are the bug.
  3. If it is a NullPointerException, find the variable that can be null: the placer, a block entity, a stack.
  4. Test it first. ‘placer instanceof Player player’ is false for null and gives you a typed variable when it is true (on 1.16.5 and 1.12.2, test with instanceof and then cast).
  5. Put game changes behind !level.isClientSide() (!world.isRemote on 1.12.2), then place the block by hand and with a dispenser.

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

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.displayClientMessage

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(Component.literal("Placed at " + pos.toShortString()), true);
}
}
}

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

Fabric and NeoForge 1.20.3 to 1.21.1, Fabric and NeoForge 1.21.2 to 1.21.11

Section titled “Fabric and NeoForge 1.20.3 to 1.21.1, Fabric and NeoForge 1.21.2 to 1.21.11”

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.displayClientMessage

src/main/java/com/example/examplemod/block/CarefulBlock.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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public static final MapCodec<CarefulBlock> CODEC = simpleCodec(CarefulBlock::new);
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(Component.literal("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.sendOverlayMessage

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

src/main/java/com/example/examplemod/block/CarefulBlock.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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public static final MapCodec<CarefulBlock> CODEC = simpleCodec(CarefulBlock::new);
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends Block> codec() {
return CODEC;
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.sendOverlayMessage(Component.literal("Placed at " + pos.toShortString()));
}
}
}

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

Uses:

  • BlockBehaviour.setPlacedBy
  • 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/CarefulBlock.java

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.sendOverlayMessage(Component.literal("Placed at " + pos.toShortString()));
}
}
}

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

Uses:

  • Block.setPlacedBy

src/main/java/com/example/examplemod/block/CarefulBlock.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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(new TextComponent("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • Block.setPlacedBy

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(Component.literal("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.displayClientMessage

src/main/java/com/example/examplemod/block/CarefulBlock.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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(new TextComponent("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • Block.setPlacedBy

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

package com.example.examplemod.block;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.block.Block;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(AbstractBlock.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(world, pos, state, placer, stack);
if (world.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) placer;
player.displayClientMessage(new StringTextComponent("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.displayClientMessage

src/main/java/com/example/examplemod/block/CarefulBlock.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.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player) {
Player player = (Player) placer;
player.displayClientMessage(new TextComponent("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • BlockBehaviour.setPlacedBy
  • Block.simpleCodec
  • Player.displayClientMessage

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
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;
// Careful Block: handles being placed without a crash. setPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
public void setPlacedBy(Level level, BlockPos pos, BlockState state, LivingEntity placer, ItemStack stack) {
super.setPlacedBy(level, pos, state, placer, stack);
if (level.isClientSide()) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof Player player) {
player.displayClientMessage(Component.literal("Placed at " + pos.toShortString()), true);
}
}
}

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

Uses:

  • Block.onBlockPlacedBy

src/main/java/com/example/examplemod/block/CarefulBlock.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.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
// Careful Block: handles being placed without a crash. onBlockPlacedBy runs in the client and on the server.
public class CarefulBlock extends Block {
public CarefulBlock() {
super(Material.ROCK);
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
super.onBlockPlacedBy(world, pos, state, placer, stack);
if (world.isRemote) {
return;
}
// The placer is null when a dispenser or a command places the block. instanceof is false for null, so this is safe.
if (placer instanceof EntityPlayer) {
((EntityPlayer) placer).sendStatusMessage(new TextComponentString("Placed at " + pos.getX() + ", " + pos.getY() + ", " + pos.getZ()), true);
}
}
}

It’s registered like any other block, as CarefulBlock: 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.