public class DemoBlock extends BlockWithEntity { [...] @Override protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { ... if (!player.getStackInHand(hand).isEmpty()) { ... } else { // 如果玩家手中没有东西,就依次给玩家方块实体中的物品 // 找到第一个有物品的槽位,并给玩家 if (!blockEntity.getStack(1).isEmpty()) { // 给玩家物品栏中的物品堆 player.getInventory().offerOrDrop(blockEntity.getStack(1)); // 从物品栏移除物品堆 blockEntity.removeStack(1); } else if (!blockEntity.getStack(0).isEmpty()) { player.getInventory().offerOrDrop(blockEntity.getStack(0)); blockEntity.removeStack(0); } else { return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } } return ItemActionResult.SUCCESS; } }