tutorial:inventory
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:inventory [2019/08/06 18:34] – fudge | tutorial:inventory [2020/07/20 13:48] – update ImplementedInventory with new bindings zentrale | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Storing items in a block as an Inventory ====== | ====== Storing items in a block as an Inventory ====== | ||
+ | Make sure you've [[tutorial: | ||
+ | |||
The standard way to store items in a BlockEntity is to make it an '' | The standard way to store items in a BlockEntity is to make it an '' | ||
This allows hoppers (or other mods) to insert and extract items from your BlockEntity without any extra work. | This allows hoppers (or other mods) to insert and extract items from your BlockEntity without any extra work. | ||
Line 15: | Line 17: | ||
*/ | */ | ||
public interface ImplementedInventory extends Inventory { | public interface ImplementedInventory extends Inventory { | ||
+ | |||
/** | /** | ||
- | | + | |
* Must return the same instance every time it's called. | * Must return the same instance every time it's called. | ||
*/ | */ | ||
DefaultedList< | DefaultedList< | ||
- | | + | |
/** | /** | ||
* Creates an inventory from the item list. | * Creates an inventory from the item list. | ||
Line 27: | Line 30: | ||
return () -> items; | return () -> items; | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * Creates a new inventory with the size. | + | * Creates a new inventory with the specified |
*/ | */ | ||
static ImplementedInventory ofSize(int size) { | static ImplementedInventory ofSize(int size) { | ||
return of(DefaultedList.ofSize(size, | return of(DefaultedList.ofSize(size, | ||
} | } | ||
- | | + | |
/** | /** | ||
* Returns the inventory size. | * Returns the inventory size. | ||
*/ | */ | ||
@Override | @Override | ||
- | default int getInvSize() { | + | default int size() { |
return getItems().size(); | return getItems().size(); | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * @return true if this inventory has only empty stacks, false otherwise | + | * Checks if the inventory is empty. |
+ | * @return true if this inventory has only empty stacks, false otherwise. | ||
*/ | */ | ||
@Override | @Override | ||
- | default boolean | + | default boolean |
- | for (int i = 0; i < getInvSize(); i++) { | + | for (int i = 0; i < size(); i++) { |
- | ItemStack stack = getInvStack(i); | + | ItemStack stack = getStack(i); |
if (!stack.isEmpty()) { | if (!stack.isEmpty()) { | ||
return false; | return false; | ||
Line 54: | Line 60: | ||
return true; | return true; | ||
} | } | ||
+ | | ||
/** | /** | ||
- | | + | |
*/ | */ | ||
@Override | @Override | ||
- | default ItemStack | + | default ItemStack |
return getItems().get(slot); | return getItems().get(slot); | ||
} | } | ||
+ | | ||
/** | /** | ||
- | | + | |
- | | + | |
- | * takes all items in that slot. | + | * @param count How many items to remove. |
+ | | ||
*/ | */ | ||
@Override | @Override | ||
- | default ItemStack | + | default ItemStack |
ItemStack result = Inventories.splitStack(getItems(), | ItemStack result = Inventories.splitStack(getItems(), | ||
if (!result.isEmpty()) { | if (!result.isEmpty()) { | ||
Line 74: | Line 83: | ||
return result; | return result; | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * Removes | + | * Removes |
+ | | ||
*/ | */ | ||
@Override | @Override | ||
- | default ItemStack | + | default ItemStack |
return Inventories.removeStack(getItems(), | return Inventories.removeStack(getItems(), | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * Replaces the current stack in the {@code | + | * Replaces the current stack in an inventory |
- | | + | |
- | * it gets resized to this inventory' | + | * @param stack The replacing itemstack. |
+ | | ||
+ | | ||
*/ | */ | ||
@Override | @Override | ||
- | default void setInvStack(int slot, ItemStack stack) { | + | default void setStack(int slot, ItemStack stack) { |
getItems().set(slot, | getItems().set(slot, | ||
- | if (stack.getCount() > getInvMaxStackAmount()) { | + | if (stack.getCount() > getMaxCountPerStack()) { |
- | stack.setCount(getInvMaxStackAmount()); | + | stack.setCount(getMaxCountPerStack()); |
} | } | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * Clears | + | * Clears the inventory. |
*/ | */ | ||
@Override | @Override | ||
Line 100: | Line 115: | ||
getItems().clear(); | getItems().clear(); | ||
} | } | ||
+ | | ||
+ | /** | ||
+ | * Marks the state as dirty. | ||
+ | * Must be called after changes in the inventory, so that the game can properly save | ||
+ | * the inventory contents and notify neighboring blocks of inventory changes. | ||
+ | | ||
@Override | @Override | ||
default void markDirty() { | default void markDirty() { | ||
// Override if you want behavior. | // Override if you want behavior. | ||
} | } | ||
+ | | ||
+ | /** | ||
+ | * @return true if the player can use the inventory, false otherwise. | ||
+ | | ||
@Override | @Override | ||
default boolean canPlayerUseInv(PlayerEntity player) { | default boolean canPlayerUseInv(PlayerEntity player) { | ||
Line 146: | Line 171: | ||
</ | </ | ||
===== Extracting and inserting from your inventory (or any inventory) ===== | ===== Extracting and inserting from your inventory (or any inventory) ===== | ||
- | In our block class, we'll override the `activate` behavior to insert and extract items from our inventory. | + | In our block class, we'll override the `onUse` behavior to insert and extract items from our inventory. |
Note that this can be done to any '' | 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. | First we'll handle inserting into the inventory. The player will insert the item he is holding if he is holding one. | ||
Line 156: | Line 181: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
if (world.isClient) return true; | if (world.isClient) return true; | ||
Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos); | Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos); | ||
Line 189: | Line 214: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
... | ... | ||
if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
Line 196: | Line 221: | ||
// 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.getInvStack(1).isEmpty()) { | if (!blockEntity.getInvStack(1).isEmpty()) { | ||
// Give the player the stack in the inventory | // Give the player the stack in the inventory | ||
- | player.giveItemStack(blockEntity.getInvStack(1)); | + | player.inventory.offerOrDrop(world, |
// Remove the stack from the inventory | // Remove the stack from the inventory | ||
blockEntity.removeInvStack(1); | blockEntity.removeInvStack(1); | ||
} else if (!blockEntity.getInvStack(0).isEmpty()) { | } else if (!blockEntity.getInvStack(0).isEmpty()) { | ||
- | player.giveItemStack(blockEntity.getInvStack(0)); | + | player.inventory.offerOrDrop(world, |
blockEntity.removeInvStack(0); | blockEntity.removeInvStack(0); | ||
} | } |
tutorial/inventory.txt · Last modified: 2023/11/06 23:28 by binaris00