How to draw something on a block with a block entity renderer (Minecraft mod)
A block model is fixed JSON. For anything that changes or moves, such as an item floating above a pedestal, write a block entity renderer (a tile entity renderer before 1.17). Its render method gets the block entity and draws with a pose stack to move and rotate with; the item stack is drawn with the item renderer. The renderer runs only on the client, so the block entity must send its data there: an update tag, an update packet and level.sendBlockUpdated. Register the renderer on the client only: BlockEntityRenderers.register on Fabric, the RegisterRenderers event on Forge and NeoForge. From 1.21.9 the renderer first copies what it draws into a render state and then submits it.
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.
- Make the block entity send its data to the client.
- Write the renderer and draw with the pose stack.
- Register the renderer on the client only.
- Right-click the block with an item and watch it spin.
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.20.4
Section titled “NeoForge 1.20.4”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()).build(null));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.neoforge.client.event.EntityRenderersEvent;import net.neoforged.bus.api.IEventBus;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.20.2
Section titled “NeoForge 1.20.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()).build(null));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.neoforge.client.event.EntityRenderersEvent;import net.neoforged.bus.api.IEventBus;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.19.4 to 1.20.2
Section titled “Fabric 1.19.4 to 1.20.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Before 1.20.5 a block entity saves and loads with plain CompoundTags and uses the old use(…) interaction method; the stack is written with ItemStack#save. Before 1.20.3 a block does not need a codec(): BaseEntityBlock has only a constructor.
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.20.3 to 1.20.4
Section titled “Fabric 1.20.3 to 1.20.4”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Before 1.20.5 a block entity saves and loads with plain CompoundTags and uses the old use(…) interaction method; the stack is written with ItemStack#save.
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.20.5 to 1.21.1
Section titled “Fabric 1.20.5 to 1.21.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.sidedSuccess(level.isClientSide()); }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.20.5 to 1.21.1
Section titled “NeoForge 1.20.5 to 1.21.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.sidedSuccess(level.isClientSide()); }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()).build(null));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.21.2 to 1.21.4
Section titled “Fabric 1.21.2 to 1.21.4”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.21.2 to 1.21.4
Section titled “NeoForge 1.21.2 to 1.21.4”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.21.5
Section titled “NeoForge 1.21.5”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay, Vec3 camera) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.21.5
Section titled “Fabric 1.21.5”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay, Vec3 cameraPos) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.21.6 to 1.21.8
Section titled “NeoForge 1.21.6 to 1.21.8”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Since 1.21.6 a block entity saves and loads through ValueOutput and ValueInput instead of CompoundTag.
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay, Vec3 camera) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.21.6 to 1.21.8
Section titled “Fabric 1.21.6 to 1.21.8”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay, Vec3 cameraPos) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 1.21.9 to 1.21.11
Section titled “NeoForge 1.21.9 to 1.21.11”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Since 1.21.9 a block entity renderer copies what it draws into a render state (extractRenderState) and then submits it (submit); the FML dist check is FMLEnvironment.getDist().
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.9 a renderer first copies what it needs into a render state, then submits the drawing.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
public static class State extends BlockEntityRenderState { final ItemStackRenderState item = new ItemStackRenderState(); float spin; }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 camera, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = entity.getLevel() == null ? 0.0F : (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees(state.spin)); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.getDist() == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.21.9 to 1.21.11
Section titled “Fabric 1.21.9 to 1.21.11”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.10 a block entity renderer first copies what it needs into a render state (extractRenderState), then submits the drawing (submit).public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { public static class State extends BlockEntityRenderState { public final ItemStackRenderState item = new ItemStackRenderState(); public float spin; }
private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 cameraPos, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); if (entity.getLevel() == null) { return; } itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees(state.spin)); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 26.1 to 26.2
Section titled “NeoForge 26.1 to 26.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Since 1.21.9 a block entity renderer copies what it draws into a render state (extractRenderState) and then submits it (submit); the FML dist check is FMLEnvironment.getDist().
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.level.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.9 a renderer first copies what it needs into a render state, then submits the drawing.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
public static class State extends BlockEntityRenderState { final ItemStackRenderState item = new ItemStackRenderState(); float spin; }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 camera, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = entity.getLevel() == null ? 0.0F : (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees(state.spin)); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.getDist() == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 26.1 to 26.2
Section titled “Fabric 26.1 to 26.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public static final MapCodec<SpinPedestalBlock> CODEC = simpleCodec(SpinPedestalBlock::new);
public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take()); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.level.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.10 a block entity renderer first copies what it needs into a render state (extractRenderState), then submits the drawing (submit).public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { public static class State extends BlockEntityRenderState { public final ItemStackRenderState item = new ItemStackRenderState(); public float spin; }
private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 cameraPos, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); if (entity.getLevel() == null) { return; } itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees(state.spin)); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
NeoForge 26.3
Section titled “NeoForge 26.3”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
Since 1.21.9 a block entity renderer copies what it draws into a render state (extractRenderState) and then submits it (submit); the FML dist check is FMLEnvironment.getDist().
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> new BlockEntityType<>(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL.get()));
private ModBlockEntities() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.level.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.9 a renderer first copies what it needs into a render state, then submits the drawing.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
public static class State extends BlockEntityRenderState { final ItemStackRenderState item = new ItemStackRenderState(); float spin; }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 camera, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = entity.getLevel() == null ? 0.0F : (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.rotateDegrees(Axis.YP, state.spin); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// Registers the renderer in the client (NeoForge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.BLOCK_ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
if (FMLEnvironment.getDist() == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 26.3
Section titled “Fabric 26.3”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(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 SpinPedestalBlockEntity 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 SpinPedestalBlockEntity entity && !entity.stored().isEmpty()) { player.getInventory().placeItemBackInInventory(entity.take(), Prediction.SERVER_ONLY); } return InteractionResult.SUCCESS; }}src/main/java/com/example/examplemod/block/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.renderer.SubmitNodeCollector;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.blockentity.state.BlockEntityRenderState;import net.minecraft.client.renderer.feature.ModelFeatureRenderer;import net.minecraft.client.renderer.item.ItemModelResolver;import net.minecraft.client.renderer.item.ItemStackRenderState;import net.minecraft.client.renderer.state.level.CameraRenderState;import net.minecraft.client.renderer.texture.OverlayTexture;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.phys.Vec3;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.// Since 1.21.10 a block entity renderer first copies what it needs into a render state (extractRenderState), then submits the drawing (submit).public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity, SpinPedestalRenderer.State> { public static class State extends BlockEntityRenderState { public final ItemStackRenderState item = new ItemStackRenderState(); public float spin; }
private final ItemModelResolver itemModels;
public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { this.itemModels = context.itemModelResolver(); }
@Override public State createRenderState() { return new State(); }
@Override public void extractRenderState(SpinPedestalBlockEntity entity, State state, float partialTick, Vec3 cameraPos, ModelFeatureRenderer.CrumblingOverlay crumbling) { BlockEntityRenderState.extractBase(entity, state, crumbling); if (entity.getLevel() == null) { return; } itemModels.updateForTopItem(state.item, entity.stored(), ItemDisplayContext.FIXED, entity.getLevel(), null, 0); state.spin = (entity.getLevel().getGameTime() + partialTick) * 4.0F; }
@Override public void submit(State state, PoseStack pose, SubmitNodeCollector collector, CameraRenderState camera) { if (state.item.isEmpty()) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.rotateDegrees(Axis.YP, state.spin); state.item.submit(pose, collector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.17.1
Section titled “Forge 1.17.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.18.2
Section titled “Forge 1.18.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.19 to 1.19.2
Section titled “Forge 1.19 to 1.19.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.19.3
Section titled “Forge 1.19.3”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.19.4 to 1.20.1
Section titled “Forge 1.19.4 to 1.20.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.java
package com.example.examplemod.block;
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.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!(level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity entity)) { return InteractionResult.PASS; } 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemDisplayContext;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemDisplayContext.FIXED, above, overlay, pose, buffers, entity.getLevel(), 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.eventbus.api.IEventBus;
// Registers the renderer in the client (Forge: EntityRenderersEvent.RegisterRenderers on the mod bus).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onRegister); }
private static void onRegister(EntityRenderersEvent.RegisterRenderers event) { event.registerBlockEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.19.3
Section titled “Fabric 1.19.3”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(BuiltInRegistries.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Axis;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Axis.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.19.2
Section titled “Fabric 1.19.2”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;import net.minecraft.core.Registry;import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities { public static final BlockEntityType<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraft.client.renderer.blockentity.BlockEntityRenderers;
// Registers the renderer in the client (Fabric: BlockEntityRenderers.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRenderers.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
TileEntityRenderer.renderItemRenderer.renderStaticMatrixStackTileEntity.getUpdatePacketClientRegistry.bindTileEntityRenderer
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends Block { public SpinPedestalBlock(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 SpinPedestalBlockEntity(); }
@Override public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) { TileEntity tile = world.getBlockEntity(pos); if (!(tile instanceof SpinPedestalBlockEntity)) { return ActionResultType.PASS; } SpinPedestalBlockEntity entity = (SpinPedestalBlockEntity) 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends TileEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity() { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity>> SPIN_PEDESTAL = BLOCK_ENTITY_TYPES.register("spin_pedestal", () -> TileEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_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()); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.matrix.MatrixStack;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.IRenderTypeBuffer;import net.minecraft.client.renderer.WorldRenderer;import net.minecraft.client.renderer.model.ItemCameraTransforms;import net.minecraft.client.renderer.tileentity.TileEntityRenderer;import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;import net.minecraft.item.ItemStack;import net.minecraft.util.math.vector.Vector3f;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer extends TileEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(TileEntityRendererDispatcher dispatcher) { super(dispatcher); }
@Override public void render(SpinPedestalBlockEntity entity, float partialTicks, MatrixStack pose, IRenderTypeBuffer buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTicks) * 4.0F)); int above = WorldRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemCameraTransforms.TransformType.FIXED, above, overlay, pose, buffers); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.client.registry.ClientRegistry;import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
// Registers the renderer in the client (Forge 1.16.5: ClientRegistry.bindTileEntityRenderer, in FMLClientSetupEvent).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(SpinPedestalClientSetup::onClientSetup); }
private static void onClientSetup(FMLClientSetupEvent event) { ClientRegistry.bindTileEntityRenderer(ModBlockEntities.SPIN_PEDESTAL.get(), SpinPedestalRenderer::new); }}In your main mod class, inside <init>:
ModBlockEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { SpinPedestalClientSetup.register(modEventBus);}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.18 to 1.19.1
Section titled “Fabric 1.18 to 1.19.1”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(pos, state); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (level.getBlockEntity(pos) instanceof SpinPedestalBlockEntity 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity(BlockPos pos, BlockState state) { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;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<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), FabricBlockEntityTypeBuilder.create(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build());
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer implements BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRendererProvider.Context context) { }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers, 0); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry;
// Registers the renderer in the client (Fabric: BlockEntityRendererRegistry.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRendererRegistry.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
BlockEntityRenderer.renderItemRenderer.renderStaticPoseStackBlockEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends BaseEntityBlock { public SpinPedestalBlock(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 SpinPedestalBlockEntity(); }
@Override public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { BlockEntity found = level.getBlockEntity(pos); if (found instanceof SpinPedestalBlockEntity) { SpinPedestalBlockEntity entity = (SpinPedestalBlockEntity) 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity extends BlockEntity { private ItemStack stored = ItemStack.EMPTY;
public SpinPedestalBlockEntity() { super(ModBlockEntities.SPIN_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.SpinPedestalBlockEntity;import net.minecraft.core.Registry;import net.minecraft.world.level.block.entity.BlockEntityType;
public final class ModBlockEntities { public static final BlockEntityType<SpinPedestalBlockEntity> SPIN_PEDESTAL = Registry.register(Registry.BLOCK_ENTITY_TYPE, ExampleMod.id("spin_pedestal"), BlockEntityType.Builder.of(SpinPedestalBlockEntity::new, ModBlocks.SPIN_PEDESTAL).build(null));
private ModBlockEntities() { }
// Called from ExampleMod#onInitialize after ModBlocks; loading this class registers the block entity types above. public static void register() { }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import com.mojang.blaze3d.vertex.PoseStack;import com.mojang.math.Vector3f;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.LevelRenderer;import net.minecraft.client.renderer.MultiBufferSource;import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;import net.minecraft.client.renderer.block.model.ItemTransforms;import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher;import net.minecraft.world.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer extends BlockEntityRenderer<SpinPedestalBlockEntity> { public SpinPedestalRenderer(BlockEntityRenderDispatcher dispatcher) { super(dispatcher); }
@Override public void render(SpinPedestalBlockEntity entity, float partialTick, PoseStack pose, MultiBufferSource buffers, int light, int overlay) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getLevel() == null) { return; } pose.pushPose(); pose.translate(0.5, 1.25, 0.5); pose.mulPose(Vector3f.YP.rotationDegrees((entity.getLevel().getGameTime() + partialTick) * 4.0F)); int above = LevelRenderer.getLightColor(entity.getLevel(), entity.getBlockPos().above()); Minecraft.getInstance().getItemRenderer().renderStatic(stack, ItemTransforms.TransformType.FIXED, above, overlay, pose, buffers); pose.popPose(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModBlockEntities;import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
// Registers the renderer in the client (Fabric: BlockEntityRendererRegistry.register).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from the mod's client entrypoint. public static void register() { BlockEntityRendererRegistry.INSTANCE.register(ModBlockEntities.SPIN_PEDESTAL, SpinPedestalRenderer::new); }}In your main mod class, inside onInitialize:
ModBlockEntities.register();In your client setup class, inside onInitializeClient:
SpinPedestalClientSetup.register();It’s registered like any other block, as SpinPedestalBlock: see a new block.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
TileEntitySpecialRenderer.renderRenderItem.renderItemClientRegistry.bindTileEntitySpecialRendererTileEntity.getUpdatePacket
src/main/java/com/example/examplemod/block/SpinPedestalBlock.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;
// Spin 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 SpinPedestalBlock extends Block { public SpinPedestalBlock() { 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 SpinPedestalBlockEntity(); }
@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 SpinPedestalBlockEntity)) { return false; } SpinPedestalBlockEntity entity = (SpinPedestalBlockEntity) 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/SpinPedestalBlockEntity.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 Spin Pedestal: it holds one item stack and saves it with the world.public class SpinPedestalBlockEntity 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.SpinPedestalBlockEntity;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(SpinPedestalBlockEntity.class, new ResourceLocation(ExampleMod.MOD_ID, "spin_pedestal")); }}src/main/java/com/example/examplemod/client/SpinPedestalRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import net.minecraft.client.Minecraft;import net.minecraft.client.renderer.GlStateManager;import net.minecraft.client.renderer.block.model.ItemCameraTransforms;import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;import net.minecraft.item.ItemStack;
// Draws the item of Spin Pedestal floating above the block, turning slowly. It runs in the client only.public class SpinPedestalRenderer extends TileEntitySpecialRenderer<SpinPedestalBlockEntity> { @Override public void render(SpinPedestalBlockEntity entity, double x, double y, double z, float partialTicks, int destroyStage, float alpha) { ItemStack stack = entity.stored(); if (stack.isEmpty() || entity.getWorld() == null) { return; } GlStateManager.pushMatrix(); GlStateManager.translate(x + 0.5, y + 1.25, z + 0.5); GlStateManager.rotate((entity.getWorld().getTotalWorldTime() + partialTicks) * 4.0F, 0.0F, 1.0F, 0.0F); Minecraft.getMinecraft().getRenderItem().renderItem(stack, ItemCameraTransforms.TransformType.FIXED); GlStateManager.popMatrix(); }}src/main/java/com/example/examplemod/client/SpinPedestalClientSetup.java
package com.example.examplemod.client;
import com.example.examplemod.block.SpinPedestalBlockEntity;import net.minecraftforge.fml.client.registry.ClientRegistry;
// Registers the renderer in the client (Forge 1.12.2: ClientRegistry.bindTileEntitySpecialRenderer, from preInit).public final class SpinPedestalClientSetup { private SpinPedestalClientSetup() { }
// Called from preInit, in the client only. public static void register() { ClientRegistry.bindTileEntitySpecialRenderer(SpinPedestalBlockEntity.class, new SpinPedestalRenderer()); }}In your main mod class, inside preInit:
ModBlockEntities.register();In your main mod class, inside preInit:
if (event.getSide() == Side.CLIENT) { SpinPedestalClientSetup.register();}It’s registered like any other block, as SpinPedestalBlock: see a new block.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/blocks/block-entities
- https://docs.neoforged.net/docs/1.21.1/blockentities/ber
Related how-tos
Section titled “Related how-tos”- Open a screen from an item
- A block entity that ticks
- A block that holds an item
- A block that gives off particles
- A command that runs on the client
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.