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
Next revision
Previous revision
Last revisionBoth sides next revision
zh_cn:tutorial:inventory [2021/09/25 14:37] – [实现Inventory接口] solidblockzh_cn:tutorial:inventory [2023/08/28 10:14] – [从物品栏(或任何物品栏)中提取和放入] wjz_p
Line 1: Line 1:
-====== 将物品放容器中 ====== +====== 在方块存储物品 ====== 
-阅读本教程之前,请确保[[tutorial:blockentity |方块实体]]制作完成+阅读本教程之前,请确保已经做好了[[zh_cn:tutorial:blockentity|方块实体]]。
  
-将物品存储在BlockEntity中的标准方法是使其成为''Inventory'' +将物品存储在 BlockEntity(方块实体)中的标准方法是使其成为''Inventory''。这使得漏斗(或其他模组)无需任何额外的工作即可从您的 BlockEntity 放入和提取物品。  
-这使得漏斗(或其他mods)无需任何额外的工作即可从您的BlockEntity放入和提取物品。  +===== 实现 Inventory 接口 =====
-===== 实现Inventory接口 =====+
 ''Inventory'' 只是一个接口,这意味着实际的 ''ItemStack'' 状态将需要存储在您的''BlockEntity''上。可以使用''DefaultedList <ItemStack>''作为存储这些''ItemStacks''的简便方法,且可以将其默认设置为''ItemStack.Empty'',用来表示物品堆没有任何物品。实现 ''Inventory''非常简单,但乏味且容易出错,因此,我们将使用其默认实现,该实现只需要给它一个''DefaultList <ItemStack>''(将其复制为新文件): ''Inventory'' 只是一个接口,这意味着实际的 ''ItemStack'' 状态将需要存储在您的''BlockEntity''上。可以使用''DefaultedList <ItemStack>''作为存储这些''ItemStacks''的简便方法,且可以将其默认设置为''ItemStack.Empty'',用来表示物品堆没有任何物品。实现 ''Inventory''非常简单,但乏味且容易出错,因此,我们将使用其默认实现,该实现只需要给它一个''DefaultList <ItemStack>''(将其复制为新文件):
 <code java ImplementedInventory.java> <code java ImplementedInventory.java>
Line 165: 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