public class BoxBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, ImplementedInventory { private final DefaultedList inventory = DefaultedList.ofSize(9, ItemStack.EMPTY); public BoxBlockEntity() { super(Test.BOX_BLOCK_ENTITY); } //From the ImplementedInventory Interface @Override public DefaultedList getItems() { return inventory; } //These Methods are from the NamedScreenHandlerFactory Interface @Override public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) { //We provide this to the screenHandler as our class Implements Inventory //Only the Server has the Inventory at the start, this will be synced to the client in the ScreenHandler return new BoxScreenHandler(syncId, playerInventory, this); } @Override public Text getDisplayName() { // versions 1.18 and below return new TranslatableText(getCachedState().getBlock().getTranslationKey()); // versions 1.19 and later return Text.translatable(getCachedState().getBlock().getTranslationKey()); } //This Method is from the ExtendedScreenHandlerFactory //This method gets called on the Server when it requests the client to open the screenHandler. //The contents you write into the packetByteBuf will automatically be transferred in a packet to the client //and the ScreenHandler Constructor with the packetByteBuf argument gets called on the client // //The order you insert things here is the same as you need to extract them. You do not need to reverse the order! @Override public void writeScreenOpeningData(ServerPlayerEntity serverPlayerEntity, PacketByteBuf packetByteBuf) { //The pos field is a public field from BlockEntity packetByteBuf.writeBlockPos(pos); } }