Interface Inventory

All Superinterfaces:
ValueContainer
All Known Subinterfaces:
BlockEntityInventory<T>, CarriedInventory<C>, Container, CraftingGridInventory, CraftingInventory, CraftingOutput, EmptyInventory, EquipmentInventory, EquipmentSlot, FilteringSlot, FuelSlot, GridInventory, Hotbar, InputSlot, Inventory2D, InventoryColumn, InventoryRow, OutputSlot, PersistentInventory, PlayerInventory, PrimaryPlayerInventory, SidedSlot, Slot, UserInventory, ViewableInventory, ViewableInventory.Custom

public interface Inventory extends ValueContainer
Base interface for queryable inventories.
  • Method Details

    • builder

      static Inventory.Builder builder()
      Creates a new Inventory.Builder to build a basic Inventory.

      Inventories created by this builder cannot be opened.

      If you want to show the inventory to a Player use ViewableInventory.builder()

      Returns:
      The builder
    • parent

      Inventory parent()
      Gets the parent Inventory of this Inventory.
      Returns:
      the parent inventory, returns this inventory if there is no parent (this is a top-level inventory)
    • root

      Inventory root()
      Gets the root Inventory of this Inventory. This is equivalent to calling parent() until it returns itself.
      Returns:
      the root inventory, returns this inventory if there is no parent (this is a top-level inventory)
    • slots

      List<Slot> slots()
      Returns a list of all Slots (leaf nodes) in this Inventory.
      Returns:
      a list of all Slots (leaf nodes) in this inventory
    • children

      List<Inventory> children()
      Returns a list of all direct child inventories.
      Returns:
      a list of all direct child inventories.
    • hasChildren

      boolean hasChildren()
      Returns true if this Inventory contains children.
      Returns:
      true if this inventory contains child inventories
    • poll

      Gets and removes the first non empty stack from this Inventory.
      Returns:
      A SUCCESS transaction-result if an item was removed. FAILURE when nothing was removed.
    • poll

      Gets and removes up to limit items of the type in the first non empty stack in this Inventory from all stacks in this Inventory.

      Note that this method attempts to consume items up to limit, which may consume items from an arbitrary number of slots.

      For example, assume an inventory containing 4 slots contains stacks as follows:

      [Stone x10] [Dirt x3] [Arrows x9] [Stone x32]

      Calling poll(16) on this inventory will consume Stone from the Inventory (because the first stack contains stone), and will then consume the remaining 6 items from the 4th slot.

      It is intended that this method is used in conjunction with a query which returns a set of slots containing a specific item type:

      ItemStack q = inv.query(ItemTypes.DIRT).poll(1);
           
      Parameters:
      limit - Maximum quantity of items to consume from the inventory
      Returns:
      A SUCCESS transaction-result only if all limit items were removed else FAILURE.
    • peek

      ItemStack peek()
      Returns a copy of the first non empty stack from this Inventory.
      Returns:
      First non empty ItemStack, or ItemStack.empty() if unavailable
    • offer

      Adds one or more ItemStacks to this inventory.
      Parameters:
      stacks - The stacks to add to this inventory.
      Returns:
      A SUCCESS transaction-result if all stacks were added and FAILURE when at least one stack was not or only partially added to the inventory.
    • canFit

      boolean canFit(ItemStack stack)
      Returns true if the entire stack can fit in this inventory.

      If this returns true offer(ItemStack...) should always succeed.

      Parameters:
      stack - The stack of items to check if it can fit in this inventory.
      Returns:
      true if the entire stack can fit in this inventory.
    • pollFrom

      InventoryTransactionResult.Poll pollFrom(int index)
      Gets and removes the stack at the supplied index in this Inventory.
      Parameters:
      index - The slot index
      Returns:
      SUCCESS transaction-result if an item was removed. FAILURE when nothing was removed.
      See Also:
    • pollFrom

      InventoryTransactionResult.Poll pollFrom(int index, int limit)
      Gets and removes the stack at the supplied index in this Inventory.
      Parameters:
      index - The slot index
      limit - The item limit
      Returns:
      A SUCCESS transaction-result only if all limit items were removed else FAILURE.
      See Also:
    • peekAt

      Optional<ItemStack> peekAt(int index)
      Returns a copy of the stack at given slot index.

      Returns Optional.empty() when there is no Slot at given index

      Parameters:
      index - The slot index
      Returns:
      a copy of the stack at given slot index. Optional.empty() if there is no matching slot.
    • offer

      InventoryTransactionResult offer(int index, ItemStack stack)
      Adds an ItemStack to the slot at given index. Returns a InventoryTransactionResult.Type.SUCCESS only if the entire ItemStack fits the slot.
      Parameters:
      index - The slot index
      stack - The stack to add to this inventory.
      Returns:
      A SUCCESS transaction-result if the entire stack was added and FAILURE when the stack was not or only partially added to the inventory.
    • set

      InventoryTransactionResult set(int index, ItemStack stack)
      Adds the ItemStack to the slot at given index overwriting the existing item.

      Always returns a InventoryTransactionResult.Type.FAILURE when there is no Slot at given index

      Stacks bigger than the max stack size will be partially rejected.

      Parameters:
      index - The slot index
      stack - The stack to add to the slot.
      Returns:
      A SUCCESS transaction-result if the entire stack was added and FAILURE when the stack was not or only partially added to the inventory.
    • slot

      Optional<Slot> slot(int index)
      Gets the Slot at the given index.
      Parameters:
      index - The slot index
      Returns:
      slot at the specified position, or Optional.empty() if no matching slot
    • clear

      void clear()
      Clears this inventory if it is clearable.
    • freeCapacity

      int freeCapacity()
      The number of empty slots in this inventory. Either 1 or 0 for Slots and always 0 for EmptyInventorys.
      Returns:
      the number of empty slots in this inventory
    • totalQuantity

      int totalQuantity()
      Returns the total quantity of items in this inventory.

      This equivalent to summing ItemStack.quantity() for all slots.

      Returns:
      the total quantity of items in this inventory
    • capacity

      int capacity()
      The number of slots in this inventory. Always 1 for Slots and always 0 for EmptyInventorys.
      Returns:
      the number of slots in this inventory
    • contains

      boolean contains(ItemStack stack)
      Checks whether the stacks quantity or more of given stack is contained in this Inventory. To check if an inventory contains any amount use containsAny(ItemStack).
      Parameters:
      stack - The stack to check for
      Returns:
      True if there are at least the given stack's amount of items present in this inventory.
    • contains

      boolean contains(ItemType type)
      Checks whether the given ItemType is contained in this Inventory
      Parameters:
      type - The type to search for
      Returns:
      True if at least one stack in this list has the given type
    • containsAny

      boolean containsAny(ItemStack stack)
      Checks whether the given stack is contained in this Inventory. The stack size is ignored.

      Note this will return true if any amount of the supplied stack is found. To check if an inventory contains at least a given quantity use contains(ItemStack).

      Parameters:
      stack - The stack to check for
      Returns:
      True if the stack is present in this inventory
    • get

      <V> Optional<V> get(Inventory child, Key<? extends Value<V>> key)
      Gets the key defined in this inventory for the specified (immediate) sub-inventory.
      Type Parameters:
      V - The key value type
      Parameters:
      child - The child inventory to inspect
      key - The key to retrieve the value for
      Returns:
      The key value, if available
    • get

      <V> Optional<V> get(Key<? extends Value<V>> key)
      Gets a key defined directly on this Inventory if one is defined. For sub-inventories this is effectively the same as inv.getParent().getProperty(inv, property); but for top-level inventories may include properties defined on the inventory directly.
      Specified by:
      get in interface ValueContainer
      Type Parameters:
      V - The key value type
      Parameters:
      key - The key to retrieve the value for
      Returns:
      The key value, if available
    • query

      Inventory query(Query query)
      Query this inventory with given Query
      Parameters:
      query - The query
      Returns:
      The queried inventory
    • query

      default <P> Inventory query(Supplier<QueryType.OneParam<P>> queryType, P param)
      Query this inventory with given QueryType.OneParam and one parameter.
      Type Parameters:
      P - The parameter type
      Parameters:
      queryType - The queryType
      param - The parameter
      Returns:
      The queried inventory
    • query

      default <P> Inventory query(Supplier<QueryType.OneParam<P>> queryType, Supplier<P> param)
      Query this inventory with given QueryType.OneParam and one parameter.
      Type Parameters:
      P - The parameter type
      Parameters:
      queryType - The queryType
      param - The parameter
      Returns:
      The queried inventory
    • query

      default <P1, P2> Inventory query(Supplier<QueryType.TwoParam<P1,P2>> queryType, P1 param1, P2 param2)
      Query this inventory with given QueryType.TwoParam and two parameters.
      Type Parameters:
      P1 - The first parameter type
      P2 - The second parameter type
      Parameters:
      queryType - The queryType
      param1 - The first parameter
      param2 - The second parameter
      Returns:
      The queried inventory
    • query

      default Inventory query(KeyValueMatcher<?> matcher)
      Query this inventory for inventories matching the supplied KeyValueMatcher.
      Parameters:
      matcher - The key value matcher
      Returns:
      The query result
    • query

      <T extends Inventory> Optional<T> query(Class<T> inventoryType)
      Query this inventory for a single inventory matching the supplied inventory type. This query will return Optional.empty() when the query does not return a single inventory matching the supplied inventory type.
      Type Parameters:
      T - The Type of inventory
      Parameters:
      inventoryType - The inventory type to query for
      Returns:
      the query result
    • intersect

      Inventory intersect(Inventory inventory)
      Intersects the slots of both inventories. The resulting inventory will only contain slots that are present in both inventories.
      Parameters:
      inventory - the other inventory
      Returns:
      an inventory wrapping all slots that are present in both inventories
    • union

      Inventory union(Inventory inventory)
      Constructs a union of the slots in both inventories.

      The resulting inventory will contain all slots from both inventories.

      The slots of this inventory are ordered before the slots of the given inventory.

      If the same slot is contained in both inventories the duplicate in the second one is removed.

      Parameters:
      inventory - the other inventory
      Returns:
      an inventory wrapping all slots of both inventories.
    • containsInventory

      boolean containsInventory(Inventory inventory)
      Returns true if the given inventory is a descendant of this one. This method will check for deeply nested inventories but will only return true if the entire inventory structure is contained. This means that e.g. for a query result of multiple slots the inventory will not return true even if all slots are contained. If you want to check if all slots of an inventory are contained in another one use intersect(Inventory) instead.

      You can use this if you want to check if a single Slot is contained in an inventory or an entire row is contained in a Grid.

      Parameters:
      inventory - the other inventory
      Returns:
      whether the given inventory is contained in this one.
    • containsChild

      boolean containsChild(Inventory child)
      Returns true if the given inventory is a direct child of this one.
      Parameters:
      child - the child inventory to check for.
      Returns:
      whether the given inventory is a direct child of this one.
    • asViewable

      Returns this inventory as a viewable inventory if possible.

      Not all inventories are viewable (e.g. a custom inventory of size 5x5)

      Returns:
      This inventory as an viewable inventory if possible.