User Tools

Site Tools


tutorial:inventory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
tutorial:inventory [2019/08/21 20:24]
fudge change giveItemStack to inventory.offerOrDrop
tutorial:inventory [2020/07/20 13:48] (current)
zentrale update ImplementedInventory with new bindings
Line 17: Line 17:
  */  */
 public interface ImplementedInventory extends Inventory { public interface ImplementedInventory extends Inventory {
 +
     /**     /**
-     ​* ​Gets the item list of this inventory.+     ​* ​Retrieves ​the item list of this inventory.
      * Must return the same instance every time it's called.      * Must return the same instance every time it's called.
      */      */
     DefaultedList<​ItemStack>​ getItems();     DefaultedList<​ItemStack>​ getItems();
-    ​// Creation+    ​
     /**     /**
      * 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 ​size.
      */      */
     static ImplementedInventory ofSize(int size) {     static ImplementedInventory ofSize(int size) {
         return of(DefaultedList.ofSize(size,​ ItemStack.EMPTY));​         return of(DefaultedList.ofSize(size,​ ItemStack.EMPTY));​
     }     }
-    ​// Inventory+    ​
     /**     /**
      * 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 ​isInvEmpty() { +    default boolean ​isEmpty() { 
-        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;
     }     }
 +    ​
     /**     /**
-     ​* ​Gets the item in the slot.+     ​* ​Retrieves ​the item in the slot.
      */      */
     @Override     @Override
-    default ItemStack ​getInvStack(int slot) {+    default ItemStack ​getStack(int slot) {
         return getItems().get(slot);​         return getItems().get(slot);​
     }     }
 +    ​
     /**     /**
-     ​* ​Takes a stack of the size from the slot. +     ​* ​Removes items from an inventory ​slot. 
-     ​* ​<​p>​(default implementation) ​If there are less items in the slot than what are requested,​ +     ​* ​@param slot  The slot to remove from. 
-     * takes all items in that slot.+     * @param count How many items to remove. ​If there are less items in the slot than what are requested,​ 
 +     ​* ​             takes all items in that slot.
      */      */
     @Override     @Override
-    default ItemStack ​takeInvStack(int slot, int count) {+    default ItemStack ​removeStack(int slot, int count) {
         ItemStack result = Inventories.splitStack(getItems(),​ slot, count);         ItemStack result = Inventories.splitStack(getItems(),​ slot, count);
         if (!result.isEmpty()) {         if (!result.isEmpty()) {
Line 76: Line 83:
         return result;         return result;
     }     }
 +    ​
     /**     /**
-     * Removes ​the current stack in the {@code slot} and returns it.+     * Removes ​all items from an inventory slot. 
 +     ​* ​@param slot The slot to remove from.
      */      */
     @Override     @Override
-    default ItemStack ​removeInvStack(int slot) {+    default ItemStack ​removeStack(int slot) {
         return Inventories.removeStack(getItems(),​ slot);         return Inventories.removeStack(getItems(),​ slot);
     }     }
 +    ​
     /**     /**
-     * Replaces the current stack in the {@code ​slotwith the provided stack. +     * Replaces the current stack in an inventory ​slot with the provided stack. 
-     ​* ​<p>If the stack is too big for this inventory ({@link Inventory#getInvMaxStackAmount()}), +     ​* ​@param slot  The inventory slot of which to replace the itemstack. 
-     * it gets resized to this inventory'​s maximum amount.+     * @param stack The replacing itemstack. ​If the stack is too big for 
 +     ​* ​             ​this inventory ({@link Inventory#getMaxCountPerStack()}), 
 +     ​* ​             it gets resized to this inventory'​s maximum amount.
      */      */
     @Override     @Override
-    default void setInvStack(int slot, ItemStack stack) {+    default void setStack(int slot, ItemStack stack) {
         getItems().set(slot,​ stack);         getItems().set(slot,​ stack);
-        if (stack.getCount() > getInvMaxStackAmount()) { +        if (stack.getCount() > getMaxCountPerStack()) { 
-            stack.setCount(getInvMaxStackAmount());+            stack.setCount(getMaxCountPerStack());
         }         }
     }     }
 +    ​
     /**     /**
-     * Clears ​{@linkplain #​getItems() ​the item list}}.+     * 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 canPlayerUseInv(PlayerEntity player) {     default boolean canPlayerUseInv(PlayerEntity player) {
Line 148: Line 171:
 </​code>​ </​code>​
 ===== 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 ''​Inventory''​ instance, not just our own (so you could do the same thing to a chest block, for example). Note that this can be done to any ''​Inventory''​ instance, not just our own (so you could do the same thing to a chest block, for example).
 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 boolean ​activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {+    public boolean ​onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
         if (world.isClient) return true;         if (world.isClient) return true;
         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);​         Inventory blockEntity = (Inventory) world.getBlockEntity(blockPos);​
Line 191: Line 214:
     [...]     [...]
     @Override     @Override
-    public boolean ​activate(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {+    public boolean ​onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity player, Hand hand, BlockHitResult blockHitResult) {
         ...         ...
         if (!player.getStackInHand(hand).isEmpty()) {         if (!player.getStackInHand(hand).isEmpty()) {
tutorial/inventory.1566419050.txt.gz · Last modified: 2019/08/21 20:24 by fudge