tutorial:inventory
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:inventory [2020/12/28 11:01] – oops yanis48 | tutorial:inventory [2025/03/30 22:06] (current) – [Storing items in a block as an Inventory] technici4n | ||
---|---|---|---|
Line 2: | Line 2: | ||
Make sure you've [[tutorial: | Make sure you've [[tutorial: | ||
- | The standard | + | The simplest |
- | This allows hoppers (or other mods) to insert and extract items from your BlockEntity without any extra work. | + | |
+ | This tutorial is written for 1.21. For older versions, some methods may change. | ||
===== Implementing Inventory ===== | ===== Implementing Inventory ===== | ||
- | '' | + | |
- | A '' | + | '' |
- | as it can be set to default to '' | + | |
- | Implementing '' | + | |
- | so we'll use a default implementation of it which only requires giving it a '' | + | |
<code java ImplementedInventory.java> | <code java ImplementedInventory.java> | ||
/** | /** | ||
* A simple {@code Inventory} implementation with only default methods + an item list getter. | * A simple {@code Inventory} implementation with only default methods + an item list getter. | ||
* | * | ||
- | | + | |
*/ | */ | ||
public interface ImplementedInventory extends Inventory { | public interface ImplementedInventory extends Inventory { | ||
Line 103: | Line 103: | ||
default void setStack(int slot, ItemStack stack) { | default void setStack(int slot, ItemStack stack) { | ||
getItems().set(slot, | getItems().set(slot, | ||
- | if (stack.getCount() > getMaxCountPerStack()) { | + | if (stack.getCount() > stack.getMaxCount()) { |
- | stack.setCount(getMaxCountPerStack()); | + | stack.setCount(stack.getMaxCount()); |
} | } | ||
} | } | ||
Line 136: | Line 136: | ||
</ | </ | ||
- | Now in your '' | + | Now in your '' |
- | and provide it with an instance of '' | + | < |
- | For this example we'll store a maximum of 2 items in the inventory: | + | |
- | <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 150: | ||
</ | </ | ||
- | We're also gonna need to save the inventories to tag and load it from there. | + | We're also gonna need to save the inventories to NBT and load it from there. '' |
- | '' | + | < |
- | <code java> | + | |
public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | public class DemoBlockEntity extends BlockEntity implements ImplementedInventory { | ||
[...] | [...] | ||
@Override | @Override | ||
- | public void fromTag(BlockState state, CompoundTag tag) { | + | public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { |
- | super.fromTag(state, tag); | + | super.readNbt(nbt, registryLookup); |
- | Inventories.fromTag(tag,items); | + | Inventories.readNbt(nbt, items, registryLookup); |
} | } | ||
@Override | @Override | ||
- | public | + | public |
- | Inventories.toTag(tag,items); | + | Inventories.writeNbt(nbt, items, registryLookup); |
- | return super.toTag(tag); | + | return super.writeNbt(nbt, registryLookup); |
} | } | ||
} | } | ||
</ | </ | ||
- | ===== Extracting and inserting from your inventory | + | |
- | In our block class, we'll override the `onUse` behavior to insert and extract items from our inventory. | + | ===== Extracting and inserting from your inventory ===== |
- | Note that this can be done to any '' | + | |
- | First we'll handle inserting into the inventory. The player will insert the item he is holding if he is holding one. | + | In our block class, we'll override the '' |
- | It'll go into the first slot if it is empty, or to the second slot if the first one is empty, | + | |
- | or if the second is empty too we'll print some information about the inventory. | + | First we'll handle inserting into the inventory. The player will insert the item he is holding if he is holding one. It'll go into the first slot if it is empty, or to the second slot if the first one is empty, or if the second is empty too we'll print some information about the inventory. |
Note that we call '' | Note that we call '' | ||
- | <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()) { | ||
Line 197: | Line 197: | ||
player.getStackInHand(hand).setCount(0); | player.getStackInHand(hand).setCount(0); | ||
} else { | } else { | ||
- | // If the inventory is full we' | + | // If the inventory is full we' |
- | | + | |
- | | + | |
+ | .append(" and the second slot holds ") | ||
+ | .append(blockEntity.getStack(1).getName())); | ||
} | } | ||
} | } | ||
- | return | + | return |
} | } | ||
} | } | ||
Line 208: | Line 210: | ||
- | We'll have the opposite behavior when the player is not holding an item. We'll take the item from the second slot, and then the first one of the second is empty. | + | We'll have the opposite behavior when the player is not holding an item. We'll take the item from the second slot, and then the first one if the second is empty. |
- | If the first is empty as well we won't do anything. | + | If the first is empty we pass it to default behavior. |
- | <code java> | + | < |
- | public class ExampleBlock | + | public class DemoBlock |
[...] | [...] | ||
@Override | @Override | ||
- | | + | |
... | ... | ||
if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
Line 221: | Line 223: | ||
// If the player is not holding anything we'll get give him the items in the block entity one by one | // 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.getStack(1).isEmpty()) { | if (!blockEntity.getStack(1).isEmpty()) { | ||
// Give the player the stack in the inventory | // Give the player the stack in the inventory | ||
- | player.inventory.offerOrDrop(world, | + | player.getInventory().offerOrDrop(blockEntity.getStack(1)); |
// Remove the stack from the inventory | // Remove the stack from the inventory | ||
blockEntity.removeStack(1); | blockEntity.removeStack(1); | ||
} else if (!blockEntity.getStack(0).isEmpty()) { | } else if (!blockEntity.getStack(0).isEmpty()) { | ||
- | player.inventory.offerOrDrop(world, | + | player.getInventory().offerOrDrop(blockEntity.getStack(0)); |
blockEntity.removeStack(0); | blockEntity.removeStack(0); | ||
+ | } else { | ||
+ | return ItemActionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION; | ||
} | } | ||
} | } | ||
| | ||
- | return | + | return |
} | } | ||
} | } | ||
Line 239: | Line 243: | ||
===== Implementing SidedInventory ===== | ===== Implementing SidedInventory ===== | ||
- | If you want to have different logic based on what side things (hopper or other mods) interact with your block | + | If you want to have different logic based on what side things (hopper or other mods) interact with your block you need to implement '' |
- | you need to implement '' | + | |
- | If say you wanted to make it so you cannot insert from the upper side of the block, you would do this: | + | < |
- | <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 266: | Line 264: | ||
} | } | ||
} | } | ||
- | |||
</ | </ | ||
tutorial/inventory.1609153307.txt.gz · Last modified: 2020/12/28 11:01 by yanis48