public class BoxBlock extends BlockWithEntity { protected BoxBlock(Settings settings) { super(settings); } // 从 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) { // 从 BlockWithEntity 继承的默认值为 INVISIBLE,所以这里需要进行改变! return BlockRenderType.MODEL; } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (!world.isClient) { // 这里会调用 BlockWithEntity 的 createScreenHandlerFactory 方法,会将返回的方块实体强转为 // 一个 namedScreenHandlerFactory。如果你的方块没有继承 BlockWithEntity,那就需要单独实现 createScreenHandlerFactory。 NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos); if (screenHandlerFactory != null) { // 这个调用会让服务器请求客户端开启合适的 Screenhandler player.openHandledScreen(screenHandlerFactory); } } return ActionResult.SUCCESS; } // 这个方法能让方块破坏时物品全部掉落 @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) { ItemScatterer.spawn(world, pos, (BoxBlockEntity)blockEntity); // 更新比较器 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)); } }