tutorial:inventory
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:inventory [2019/08/21 20:24] – change giveItemStack to inventory.offerOrDrop fudge | tutorial:inventory [2023/11/06 23:28] (current) – binaris00 | ||
---|---|---|---|
Line 17: | 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 29: | 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 56: | 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 76: | 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() > stack.getMaxCount()) { |
- | stack.setCount(getInvMaxStackAmount()); | + | stack.setCount(stack.getMaxCount()); |
} | } | ||
} | } | ||
+ | | ||
/** | /** | ||
- | * Clears | + | * Clears the inventory. |
*/ | */ | ||
@Override | @Override | ||
Line 102: | 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 | + | default boolean |
return true; | return true; | ||
} | } | ||
Line 135: | Line 158: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public void fromTag(CompoundTag tag) { | + | public void readNbt(NbtCompound nbt) { |
- | super.fromTag(tag); | + | super.readNbt(nbt); |
- | Inventories.fromTag(tag,items); | + | Inventories.readNbt(nbt, items); |
} | } | ||
@Override | @Override | ||
- | public | + | public |
- | Inventories.toTag(tag,items); | + | Inventories.writeNbt(nbt, items); |
- | return super.toTag(tag); | + | return super.writeNbt(nbt); |
} | } | ||
} | } | ||
</ | </ | ||
===== 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 158: | Line 181: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
- | if (world.isClient) return | + | if (world.isClient) return |
Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos); | Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos); | ||
Line 165: | Line 188: | ||
if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
// Check what is the first open slot and put an item from the player' | // 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 | // 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' | // 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 | // If the inventory is full we'll print it's contents | ||
System.out.println(" | System.out.println(" | ||
- | + blockEntity.getInvStack(0) + " and the second slot holds " + blockEntity.getInvStack(1)); | + | + blockEntity.getStack(0) + " and the second slot holds " + blockEntity.getStack(1)); |
} | } | ||
} | } | ||
- | return | + | return |
} | } | ||
} | } | ||
Line 191: | Line 214: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public | + | public |
... | ... | ||
if (!player.getStackInHand(hand).isEmpty()) { | if (!player.getStackInHand(hand).isEmpty()) { | ||
Line 199: | Line 222: | ||
// Find the first slot that has an item and give it to the player | // 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 | // 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.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); |
} | } | ||
} | } | ||
| | ||
- | return | + | return |
} | } | ||
} | } | ||
Line 223: | Line 246: | ||
[...] | [...] | ||
@Override | @Override | ||
- | public int[] getInvAvailableSlots(Direction | + | public int[] getInvAvailableSlots(Direction |
// Just return an array of all slots | // Just return an array of all slots | ||
int[] result = new int[getItems().size()]; | int[] result = new int[getItems().size()]; | ||
Line 234: | Line 257: | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
return direction != Direction.UP; | return direction != Direction.UP; | ||
} | } | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
return true; | return true; | ||
} | } |
tutorial/inventory.1566419050.txt.gz · Last modified: 2019/08/21 20:24 by fudge