User Tools

Site Tools


zh_cn:tutorial:inventory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
zh_cn:tutorial:inventory [2021/09/25 14:39] solidblockzh_cn:tutorial:inventory [2023/08/28 10:14] – [从物品栏(或任何物品栏)中提取和放入] wjz_p
Line 164: Line 164:
     [...]     [...]
     @Override     @Override
-    public boolean activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { +    public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) { 
-        if (world.isClient) return true;+        if (world.isClient) return ActionResult.SUCCESS;
         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);
- +  
 + 
         if (!player.getStackInHand(hand).isEmpty()) {         if (!player.getStackInHand(hand).isEmpty()) {
-            // 检查第一个打开的槽位是什么,并从玩家手中放入物品 +            // Check what is the first open slot and put an item from the player's hand there 
-            if (blockEntity.getInvStack(0).isEmpty()) { +            if (blockEntity.getStack(0).isEmpty()) { 
-                // 将玩家手中的物品堆放入物品栏中 +                // Put the stack the player is holding into the inventory 
-                blockEntity.setInvStack(0, player.getStackInHand(hand).copy()); +                blockEntity.setStack(0, player.getStackInHand(hand).copy()); 
-                // 从玩家手中移除物品堆+                // Remove the stack from the player's hand
                 player.getStackInHand(hand).setCount(0);                 player.getStackInHand(hand).setCount(0);
-            } else if (blockEntity.getInvStack(1).isEmpty()) { +            } else if (blockEntity.getStack(1).isEmpty()) { 
-                blockEntity.setInvStack(1, player.getStackInHand(hand).copy());+                blockEntity.setStack(1, player.getStackInHand(hand).copy());
                 player.getStackInHand(hand).setCount(0);                 player.getStackInHand(hand).setCount(0);
             } else {             } else {
-                // 如果物品栏是满的,我们输出其内容+                // If the inventory is full we'll print it's contents
                 System.out.println("The first slot holds "                 System.out.println("The first slot holds "
-                        + blockEntity.getInvStack(0) + " and the second slot holds " + blockEntity.getInvStack(1));+                        + blockEntity.getStack(0) + " and the second slot holds " + blockEntity.getStack(1));
             }             }
         }          } 
-        return true;+        return ActionResult.SUCCESS;
     }     }
 } }
zh_cn/tutorial/inventory.txt · Last modified: 2023/08/28 10:14 by wjz_p