public class DemoBlock extends BlockWithEntity { [...] @Override protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (world.isClient) return ActionResult.SUCCESS; if (!(world.getBlockEntity(pos) instanceof DemoBlockEntity blockEntity)) { return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; } if (!player.getStackInHand(hand).isEmpty()) { // 检查第一个开放槽位是什么,并从玩家手中将物品放入 if (blockEntity.getStack(0).isEmpty()) { // 将玩家持有的物品堆放入物品栏 blockEntity.setStack(0, player.getStackInHand(hand).copy()); // 从玩家手中移除物品堆 player.getStackInHand(hand).setCount(0); } else if (blockEntity.getStack(1).isEmpty()) { blockEntity.setStack(1, player.getStackInHand(hand).copy()); player.getStackInHand(hand).setCount(0); } else { // 如果物品栏满,提醒玩家 player.sendMessage(Text.literal("物品栏满!第一个槽位是") .append(blockEntity.getStack(0).getName()) .append(",第二个槽位是") .append(blockEntity.getStack(1).getName())); } } return ItemActionResult.SUCCESS; } }