public class BoxBlock extends BlockWithEntity { protected BoxBlock(Settings settings) { super(settings); } // This method is required since 1.20.5. @Override protected MapCodec getCodec() { return createCodec(BoxBlock::new); } @Override public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return new BoxBlockEntity(pos, state); } @Override public BlockRenderType getRenderType(BlockState state) { // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! return BlockRenderType.MODEL; } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { if (!world.isClient) { // This will call the createScreenHandlerFactory method from BlockWithEntity, which will return our blockEntity casted to // a namedScreenHandlerFactory. If your block class does not extend BlockWithEntity, it needs to implement createScreenHandlerFactory. NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos); if (screenHandlerFactory != null) { // With this call the server will request the client to open the appropriate Screenhandler player.openHandledScreen(screenHandlerFactory); } } return ActionResult.SUCCESS; } // This method will drop all items onto the ground when the block is broken @Override public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { if (state.getBlock() != newState.getBlock()) { BlockEntity blockEntity = world.getBlockEntity(pos); if (blockEntity instanceof BoxBlockEntity boxBlockEntity) { ItemScatterer.spawn(world, pos, boxblockEntity); // update comparators world.updateComparators(pos,this); } super.onStateReplaced(state, world, pos, newState, moved); } } @Override public boolean hasComparatorOutput(BlockState state) { return true; } @Override public int getComparatorOutput(BlockState state, World world, BlockPos pos) { return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos)); } }