zh_cn:tutorial:inventory
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| zh_cn:tutorial:inventory [2021/09/25 14:10] – [实施补充内容物] solidblock | zh_cn:tutorial:inventory [2024/08/27 03:03] (current) – solidblock | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== 在方块中存储物品 |
| - | 阅读本教程之前,请确保您已将[[tutorial: | + | 阅读本教程之前,请确保已经做好了[[zh_cn:tutorial: |
| + | |||
| + | 将物品存储在 BlockEntity(方块实体)中的标准方法是使其成为 '' | ||
| + | |||
| + | 这个教程是用于 1.21 的,对于旧版本,一些方法可能改变。 | ||
| + | |||
| + | ===== 实现 Inventory 接口 ===== | ||
| + | |||
| + | '' | ||
| - | 将物品存储在BlockEntity中的标准方法是使其成为'' | ||
| - | 这使得漏斗(或其他mods)无需任何额外的工作即可从您的BlockEntity放入和提取物品。 | ||
| - | ===== 实现Inventory接口 ===== | ||
| - | '' | ||
| - | 可以使用'' | ||
| - | 因为可以将其默认设置为'' | ||
| - | 实现'' | ||
| - | 因此,我们将使用它的默认实现,该实现只需要给它一个'' | ||
| <code java ImplementedInventory.java> | <code java ImplementedInventory.java> | ||
| /** | /** | ||
| - | | + | |
| * | * | ||
| * Originally by Juuz | * Originally by Juuz | ||
| Line 19: | Line 19: | ||
| /** | /** | ||
| - | | + | |
| - | | + | |
| */ | */ | ||
| DefaultedList< | DefaultedList< | ||
| | | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| static ImplementedInventory of(DefaultedList< | static ImplementedInventory of(DefaultedList< | ||
| Line 32: | Line 32: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| static ImplementedInventory ofSize(int size) { | static ImplementedInventory ofSize(int size) { | ||
| Line 39: | Line 39: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| @Override | @Override | ||
| Line 47: | Line 47: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| - | * @return true if this inventory has only empty stacks, false otherwise. | + | * @return true,如果物品栏仅有一个空堆,否则为true。 |
| */ | */ | ||
| @Override | @Override | ||
| Line 62: | Line 62: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| @Override | @Override | ||
| Line 70: | Line 70: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| - | * @param slot | + | * @param slot |
| - | * @param count How many items to remove. If there are less items in the slot than what are requested, | + | * @param count 需要移除的物品个数。如果槽位中的物品少于需要的,则将其全部取出。 |
| - | | + | |
| */ | */ | ||
| @Override | @Override | ||
| Line 85: | Line 84: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| - | * @param slot The slot to remove from. | + | * @param slot 从该槽位移除。 |
| */ | */ | ||
| @Override | @Override | ||
| Line 94: | Line 93: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| - | * @param slot | + | * @param slot |
| - | * @param stack The replacing itemstack. If the stack is too big for | + | * @param stack 替换后新的物品堆。如果堆对于此物品栏过大({@link Inventory# |
| - | | + | |
| - | | + | |
| */ | */ | ||
| @Override | @Override | ||
| Line 109: | Line 106: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| */ | */ | ||
| @Override | @Override | ||
| Line 117: | Line 114: | ||
| | | ||
| /** | /** | ||
| - | | + | |
| - | | + | |
| - | * the inventory contents and notify neighboring blocks of inventory changes. | + | |
| | | ||
| @Override | @Override | ||
| default void markDirty() { | default void markDirty() { | ||
| - | // Override if you want behavior. | + | // 需要行为时,覆盖此方法。 |
| } | } | ||
| | | ||
| /** | /** | ||
| - | * @return true if the player can use the inventory, | + | * @return true 如果玩家可以使用物品栏,否则为 |
| | | ||
| @Override | @Override | ||
| Line 136: | Line 132: | ||
| </ | </ | ||
| - | 现在在您的'' | + | 现在在你的 '' |
| - | 并为其提供存储该项目的'' | + | < |
| - | 对于此示例,我们将在库存中最多存储2件物品: | + | |
| - | <code java> | + | |
| public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | ||
| private final DefaultedList< | private final DefaultedList< | ||
| Line 152: | Line 146: | ||
| </ | </ | ||
| - | 我们还需要保存容器以标记并从那里加载。 | + | 我们还需要将物品栏保存到 NBT 并从那里加载。'' |
| - | '' | + | < |
| - | <code java> | + | |
| public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | ||
| [...] | [...] | ||
| @Override | @Override | ||
| - | public void readNbt(NbtCompound nbt) { | + | public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { |
| - | super.readNbt(nbt); | + | super.readNbt(nbt, registryLookup); |
| - | Inventories.readNbt(nbt, | + | Inventories.readNbt(nbt, |
| } | } | ||
| @Override | @Override | ||
| - | public NbtCompound writeNbt(NbtCompound nbt) { | + | public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { |
| - | Inventories.writeNbt(nbt, | + | Inventories.writeNbt(nbt, |
| - | return super.writeNbt(nbt); | + | return super.writeNbt(nbt, registryLookup); |
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | ===== 从容器(或任何容器)中提取和放入 ===== | + | |
| - | 在我们的块类中,我们将覆盖'' | + | ===== 从物品栏(或任何物品栏)中提取和放入 ===== |
| - | 请注意,可以对任何'''' | + | |
| - | 首先,我们将处理放入内容物。 | + | 我们覆盖方块类中的 '' |
| - | 如果为空,它将进入第一个插槽;如果第一个为空,它将进入第二个插槽, | + | |
| - | 或者如果第二个也为空,我们将打印有关库存的一些信息。 | + | 首先我们处理第一个槽位,如果是空的。玩家如果拿着物品,则会将拿着的物品放入。物品进入第一个槽位,如果是空的,或者进入第二个槽位,如果第一个是空的,或者如果第二个是空的,我们则会输出与物品栏有关的信息。 |
| - | 请注意,将'' | + | |
| - | <code java> | + | 注意我们将 '' |
| - | public class ExampleBlock | + | < |
| + | public class DemoBlock | ||
| [...] | [...] | ||
| @Override | @Override | ||
| - | | + | |
| - | if (world.isClient) return | + | if (world.isClient) return |
| - | | + | |
| + | if (!(world.getBlockEntity(pos) instanceof DemoBlockEntity blockEntity)) { | ||
| + | return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; | ||
| + | } | ||
| if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
| - | // Check what is the first open slot and put an item from the player' | + | // 检查第一个开放槽位是什么,并从玩家手中将物品放入 |
| - | 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' | + | // 从玩家手中移除物品堆 |
| 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 | + | // 如果物品栏满,提醒玩家 |
| - | | + | |
| - | | + | |
| + | .append(",第二个槽位是") | ||
| + | .append(blockEntity.getStack(1).getName())); | ||
| } | } | ||
| } | } | ||
| - | return | + | return |
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | 玩家不持有物品时,我们将采取相反的行为。我们将从第二个槽位中取出物品,如果第二个是空的就第一个。如果第一个也是空的,就不做任何事情。 | ||
| - | 当玩家不持有物品时,我们将采取相反的行为。 我们将从第二个插槽中取出项目,然后第二个中的第一个为空。 | + | < |
| - | 如果第一个也是空的,我们将不做任何事情。 | + | public class DemoBlock |
| - | + | ||
| - | <code java> | + | |
| - | public class ExampleBlock | + | |
| [...] | [...] | ||
| @Override | @Override | ||
| - | | + | |
| ... | ... | ||
| if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
| ... | ... | ||
| } else { | } else { | ||
| - | // If the player is not holding anything we'll get give him the items in the block entity one by one | + | // 如果玩家手中没有东西,就依次给玩家方块实体中的物品 |
| - | // Find the first slot that has an item and give it to the player | + | |
| - | if (!blockEntity.getInvStack(1).isEmpty()) { | + | if (!blockEntity.getStack(1).isEmpty()) { |
| - | // Give the player the stack in the inventory | + | // 给玩家物品栏中的物品堆 |
| - | player.inventory.offerOrDrop(world, | + | player.getInventory().offerOrDrop(blockEntity.getStack(1)); |
| - | // Remove the stack from the inventory | + | // 从物品栏移除物品堆 |
| - | blockEntity.removeInvStack(1); | + | blockEntity.removeStack(1); |
| - | } else if (!blockEntity.getInvStack(0).isEmpty()) { | + | } else if (!blockEntity.getStack(0).isEmpty()) { |
| - | player.inventory.offerOrDrop(world, | + | player.getInventory().offerOrDrop(blockEntity.getStack(0)); |
| - | blockEntity.removeInvStack(0); | + | blockEntity.removeStack(0); |
| + | } else { | ||
| + | return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; | ||
| } | } | ||
| } | } | ||
| | | ||
| - | return | + | return |
| } | } | ||
| } | } | ||
| Line 241: | Line 239: | ||
| ===== 实现 SidedInventory 接口 ===== | ===== 实现 SidedInventory 接口 ===== | ||
| 如果你希望有基于与方块不同的面(漏斗或者其他模组)进行交互的不同逻辑,你可以实现 SidedInventory 接口。如果说你想使得方块不能从上侧插入,可以这样做: | 如果你希望有基于与方块不同的面(漏斗或者其他模组)进行交互的不同逻辑,你可以实现 SidedInventory 接口。如果说你想使得方块不能从上侧插入,可以这样做: | ||
| - | <code java> | + | |
| + | < | ||
| public class DemoBlockEntity extends BlockEntity implements ImplementedInventory, | public class DemoBlockEntity extends BlockEntity implements ImplementedInventory, | ||
| [...] | [...] | ||
| @Override | @Override | ||
| - | public int[] getInvAvailableSlots(Direction | + | public int[] getInvAvailableSlots(Direction |
| // Just return an array of all slots | // Just return an array of all slots | ||
| - | | + | |
| - | for (int i = 0; i < result.length; i++) { | + | |
| - | result[i] = i; | + | |
| - | } | + | |
| - | + | ||
| - | return result; | + | |
| } | } | ||
| Line 265: | Line 259: | ||
| } | } | ||
| } | } | ||
| - | |||
| </ | </ | ||
zh_cn/tutorial/inventory.1632579052.txt.gz · Last modified: 2021/09/25 14:10 by solidblock