Skip to content

How to make a block that holds an item in a Minecraft mod

A block entity keeps data for one placed block (a tile entity before 1.17). To hold an item, give it an ItemStack field, write it when the block entity saves and read it back when it loads. On 1.20.5 and newer that is saveAdditional with stack.saveOptional(registries) and loadAdditional with ItemStack.parseOptional; before that it is stack.save and ItemStack.of. The block puts the item in when the player right-clicks holding something and takes it out with an empty hand (useItemOn and useWithoutItem on 1.20.5 and newer, use before). Call setChanged() after every change so the world saves it. To show the item in the world, add a block entity renderer.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.18 to 26.3 on Fabric; Minecraft 1.16.5 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 block entity with an ItemStack field, plus put and take.
  2. Save and load the stack in the block entity’s save and load methods.
  3. Register the block entity type and a block that makes it.
  4. In the block, put the held item in on right-click and take it out with an empty hand.

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build(null));
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

From 1.20.3 a block with a block entity needs a codec() (here simpleCodec) on top of the 1.20.1 code.

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build(null));
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return save(new CompoundTag());
}
@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
return new ClientboundBlockEntityDataPacket(worldPosition, 0, getUpdateTag());
}
@Override
public CompoundTag save(CompoundTag tag) {
super.save(tag);
tag.put("stored", stored.save(new CompoundTag()));
return tag;
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fmllegacy.RegistryObject;
public final class ModBlockEntities {
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
// Called from the ExampleMod constructor: puts the block entity types on the mod event bus.
public static void register() {
BLOCK_ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

In your main mod class, inside <init>:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public final class ModBlockEntities {
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
// Called from the ExampleMod constructor: puts the block entity types on the mod event bus.
public static void register() {
BLOCK_ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

In your main mod class, inside <init>:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
public final class ModBlockEntities {
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
// Called from the ExampleMod constructor: puts the block entity types on the mod event bus.
public static void register() {
BLOCK_ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

In your main mod class, inside <init>:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected ItemInteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return ItemInteractionResult.sidedSuccess(level.isClientSide());
}
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("stored", stored.saveOptional(registries));
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = ItemStack.parseOptional(registries, tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build(null));
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected ItemInteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return ItemInteractionResult.sidedSuccess(level.isClientSide());
}
return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("stored", stored.saveOptional(registries));
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = ItemStack.parseOptional(registries, tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

From 1.20.3 a block with a block entity needs a codec() (here simpleCodec) on top of the 1.20.1 code.

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

From 1.20.3 a block with a block entity needs a codec() (here simpleCodec) on top of the 1.20.1 code.

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("stored", stored.saveOptional(registries));
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = ItemStack.parseOptional(registries, tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
FabricBlockEntityTypeBuilder.create(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build());
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.put("stored", stored.saveOptional(registries));
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = ItemStack.parseOptional(registries, tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> new BlockEntityType<>(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.store("stored", ItemStack.OPTIONAL_CODEC, registries.createSerializationContext(NbtOps.INSTANCE), stored);
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = tag.read("stored", ItemStack.OPTIONAL_CODEC, registries.createSerializationContext(NbtOps.INSTANCE)).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> new BlockEntityType<>(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.store("stored", ItemStack.OPTIONAL_CODEC, registries.createSerializationContext(NbtOps.INSTANCE), stored);
}
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
stored = tag.read("stored", ItemStack.OPTIONAL_CODEC, registries.createSerializationContext(NbtOps.INSTANCE)).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
FabricBlockEntityTypeBuilder.create(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build());
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

Since 1.21.6 a block entity saves and loads through ValueOutput and ValueInput instead of CompoundTag.

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(ValueOutput output) {
super.saveAdditional(output);
output.store("stored", ItemStack.OPTIONAL_CODEC, stored);
}
@Override
protected void loadAdditional(ValueInput input) {
super.loadAdditional(input);
stored = input.read("stored", ItemStack.OPTIONAL_CODEC).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> new BlockEntityType<>(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.mojang.serialization.MapCodec;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public static final MapCodec<ItemPedestalBlock> CODEC = simpleCodec(ItemPedestalBlock::new);
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
@Override
protected MapCodec<? extends BaseEntityBlock> codec() {
return CODEC;
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(ValueOutput output) {
super.saveAdditional(output);
output.store("stored", ItemStack.OPTIONAL_CODEC, stored);
}
@Override
protected void loadAdditional(ValueInput input) {
super.loadAdditional(input);
stored = input.read("stored", ItemStack.OPTIONAL_CODEC).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
FabricBlockEntityTypeBuilder.create(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build());
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

Since 1.21.6 a block entity saves and loads through ValueOutput and ValueInput instead of CompoundTag.

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
ItemStack out = entity.take();
if (!player.addItem(out)) {
player.spawnAtLocation((ServerLevel) level, out);
}
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL.get(), pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(ValueOutput output) {
super.saveAdditional(output);
output.store("stored", ItemStack.OPTIONAL_CODEC, stored);
}
@Override
protected void loadAdditional(ValueInput input) {
super.loadAdditional(input);
stored = input.read("stored", ItemStack.OPTIONAL_CODEC).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModBlockEntities {
// Registered on the mod event bus in ExampleMod's constructor.
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(Registries.BLOCK_ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<BlockEntityType<?>, BlockEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> new BlockEntityType<>(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()));
private ModBlockEntities() {
}
}

In your main mod class, inside <init>:

ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.loadAdditional
  • ItemStack.saveOptional
  • ItemStack.parseOptional
  • BaseEntityBlock

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Prediction;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
protected RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
protected InteractionResult useItemOn(ItemStack held, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!held.isEmpty() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.SUCCESS;
}
return InteractionResult.TRY_WITH_EMPTY_HAND;
}
@Override
protected InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hit) {
if (!level.isClientSide() && level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity && !entity.stored().isEmpty()) {
player.getInventory().placeItemBackInInventory(entity.take(), Prediction.SERVER_ONLY);
}
return InteractionResult.SUCCESS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.ValueInput;
import net.minecraft.world.level.storage.ValueOutput;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copyWithCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider registries) {
return saveWithoutMetadata(registries);
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(ValueOutput output) {
super.saveAdditional(output);
output.store("stored", ItemStack.OPTIONAL_CODEC, stored);
}
@Override
protected void loadAdditional(ValueInput input) {
super.loadAdditional(input);
stored = input.read("stored", ItemStack.OPTIONAL_CODEC).orElse(ItemStack.EMPTY);
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
FabricBlockEntityTypeBuilder.create(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build());
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build(null));
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • TileEntity.save
  • TileEntity.load
  • ItemStack.save
  • ItemStack.of
  • TileEntityType.Builder.of

In 1.16.5 the block is a plain Block that makes its TileEntity in createTileEntity, and one use method handles both the put and the take.

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

package com.example.examplemod.block;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends Block {
public ItemPedestalBlock(AbstractBlock.Properties properties) {
super(properties);
}
// 1.16.5: a block with a tile entity says so and creates it here.
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return new ItemPedestalBlockEntity();
}
@Override
public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
TileEntity tile = world.getBlockEntity(pos);
if (!(tile instanceof ItemPedestalBlockEntity)) {
return ActionResultType.PASS;
}
ItemPedestalBlockEntity entity = (ItemPedestalBlockEntity) tile;
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!world.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return ActionResultType.sidedSuccess(world.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!world.isClientSide()) {
player.inventory.placeItemBackInInventory(world, entity.take());
}
return ActionResultType.sidedSuccess(world.isClientSide());
}
return ActionResultType.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity;
// The tile entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends TileEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity() {
super(ModBlockEntities.ITEM_PEDESTAL.get());
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the tile entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundNBT getUpdateTag() {
return save(new CompoundNBT());
}
@Override
public SUpdateTileEntityPacket getUpdatePacket() {
return new SUpdateTileEntityPacket(worldPosition, 0, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket packet) {
load(getBlockState(), packet.getTag());
}
@Override
public CompoundNBT save(CompoundNBT tag) {
super.save(tag);
tag.put("stored", stored.save(new CompoundNBT()));
return tag;
}
@Override
public void load(BlockState state, CompoundNBT tag) {
super.load(state, tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
public final class ModBlockEntities {
public static final DeferredRegister<TileEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<TileEntityType<ItemPedestalBlockEntity>> ITEM_PEDESTAL = BLOCK_ENTITY_TYPES.register("item_pedestal",
() -> TileEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL.get()).build(null));
private ModBlockEntities() {
}
// Called from the ExampleMod constructor: puts the tile entity types on the mod event bus.
public static void register() {
BLOCK_ENTITY_TYPES.register(FMLJavaModLoadingContext.get().getModEventBus());
}
}

In your main mod class, inside <init>:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
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.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new ItemPedestalBlockEntity(pos, state);
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.getBlockEntity(pos) instanceof ItemPedestalBlockEntity entity) {
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.getInventory().placeItemBackInInventory(entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity(BlockPos pos, BlockState state) {
super(ModBlockEntities.ITEM_PEDESTAL, pos, state);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return saveWithoutMetadata();
}
@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
}
@Override
protected void saveAdditional(CompoundTag tag) {
super.saveAdditional(tag);
tag.put("stored", stored.save(new CompoundTag()));
}
@Override
public void load(CompoundTag tag) {
super.load(tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
FabricBlockEntityTypeBuilder.create(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build());
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • BlockEntity.saveAdditional
  • BlockEntity.load
  • ItemStack.save
  • ItemStack.of
  • BaseEntityBlock

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

package com.example.examplemod.block;
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends BaseEntityBlock {
public ItemPedestalBlock(BlockBehaviour.Properties properties) {
super(properties);
}
// BaseEntityBlock draws nothing unless told: draw the block's model.
@Override
public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL;
}
@Override
public BlockEntity newBlockEntity(BlockGetter level) {
return new ItemPedestalBlockEntity();
}
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
BlockEntity found = level.getBlockEntity(pos);
if (found instanceof ItemPedestalBlockEntity) {
ItemPedestalBlockEntity entity = (ItemPedestalBlockEntity) found;
ItemStack held = player.getItemInHand(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!level.isClientSide()) {
entity.put(held);
held.shrink(1);
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!level.isClientSide()) {
player.inventory.placeItemBackInInventory(level, entity.take());
}
return InteractionResult.sidedSuccess(level.isClientSide());
}
}
return InteractionResult.PASS;
}
}

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

package com.example.examplemod.block;
import com.example.examplemod.registry.ModBlockEntities;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
// The block entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends BlockEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemPedestalBlockEntity() {
super(ModBlockEntities.ITEM_PEDESTAL);
}
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the block entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
setChanged();
if (level != null) {
level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), 3);
}
}
@Override
public CompoundTag getUpdateTag() {
return save(new CompoundTag());
}
@Override
public ClientboundBlockEntityDataPacket getUpdatePacket() {
return new ClientboundBlockEntityDataPacket(worldPosition, 0, getUpdateTag());
}
@Override
public CompoundTag save(CompoundTag tag) {
super.save(tag);
tag.put("stored", stored.save(new CompoundTag()));
return tag;
}
@Override
public void load(BlockState state, CompoundTag tag) {
super.load(state, tag);
stored = ItemStack.of(tag.getCompound("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities {
public static final BlockEntityType<ItemPedestalBlockEntity> ITEM_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("item_pedestal"),
BlockEntityType.Builder.of(ItemPedestalBlockEntity::new, ModBlocks.ITEM_PEDESTAL).build(null));
private ModBlockEntities() {
}
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above.
public static void register() {
}
}

In your main mod class, inside onInitialize:

ModBlockEntities.register();

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

Uses:

  • TileEntity.writeToNBT
  • TileEntity.readFromNBT
  • ItemStack.writeToNBT
  • GameRegistry.registerTileEntity

In 1.12.2 the block makes its TileEntity in createTileEntity, handles clicks in onBlockActivated, and the tile entity saves with writeToNBT and readFromNBT.

src/main/java/com/example/examplemod/block/ItemPedestalBlock.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.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
// Item Pedestal: a block that holds one item. Right-click with an item to put it in, right-click with an empty hand to take it out.
public class ItemPedestalBlock extends Block {
public ItemPedestalBlock() {
super(Material.ROCK);
}
// 1.12.2: a block with a tile entity says so and creates it here.
@Override
public boolean hasTileEntity(IBlockState state) {
return true;
}
@Override
public TileEntity createTileEntity(World world, IBlockState state) {
return new ItemPedestalBlockEntity();
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
TileEntity tile = world.getTileEntity(pos);
if (!(tile instanceof ItemPedestalBlockEntity)) {
return false;
}
ItemPedestalBlockEntity entity = (ItemPedestalBlockEntity) tile;
ItemStack held = player.getHeldItem(hand);
if (!held.isEmpty() && entity.stored().isEmpty()) {
if (!world.isRemote) {
entity.put(held);
held.shrink(1);
}
return true;
}
if (held.isEmpty() && !entity.stored().isEmpty()) {
if (!world.isRemote) {
ItemStack out = entity.take();
if (!player.inventory.addItemStackToInventory(out)) {
player.dropItem(out, false);
}
}
return true;
}
return false;
}
}

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

package com.example.examplemod.block;
import javax.annotation.Nullable;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
// The tile entity of Item Pedestal: it holds one item stack and saves it with the world.
public class ItemPedestalBlockEntity extends TileEntity {
private ItemStack stored = ItemStack.EMPTY;
public ItemStack stored() {
return stored;
}
public void put(ItemStack stack) {
stored = stack.copy();
stored.setCount(1);
changed();
}
public ItemStack take() {
ItemStack out = stored;
stored = ItemStack.EMPTY;
changed();
return out;
}
// Saves the tile entity and tells nearby clients, so a renderer can show what is inside.
private void changed() {
markDirty();
if (world != null) {
world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 3);
}
}
@Override
public NBTTagCompound getUpdateTag() {
return writeToNBT(new NBTTagCompound());
}
@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(pos, 0, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity packet) {
readFromNBT(packet.getNbtCompound());
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
tag.setTag("stored", stored.writeToNBT(new NBTTagCompound()));
return tag;
}
@Override
public void readFromNBT(NBTTagCompound tag) {
super.readFromNBT(tag);
stored = new ItemStack(tag.getCompoundTag("stored"));
}
}

src/main/java/com/example/examplemod/registry/ModBlockEntities.java

package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.block.ItemPedestalBlockEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.GameRegistry;
public final class ModBlockEntities {
private ModBlockEntities() {
}
// Called from preInit: a 1.12.2 tile entity is registered by its class and a name.
public static void register() {
GameRegistry.registerTileEntity(ItemPedestalBlockEntity.class, new ResourceLocation(ExampleMod.MOD_ID, "item_pedestal"));
}
}

In your main mod class, inside preInit:

ModBlockEntities.register();

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