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
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
A builder for free-form Inventories. -
Method Summary
Modifier and TypeMethodDescriptionReturns this inventory as a viewable inventory if possible.static Inventory.Builder
builder()
Creates a newInventory.Builder
to build a basicInventory
.boolean
Returns true if the entire stack can fit in this inventory.int
capacity()
The number of slots in this inventory.children()
Returns a list of all direct child inventories.void
clear()
Clears this inventory if it is clearable.boolean
Checks whether the stacks quantity or more of given stack is contained in this Inventory.boolean
Checks whether the given ItemType is contained in this Inventoryboolean
containsAny
(ItemStack stack) Checks whether the given stack is contained in this Inventory.boolean
containsChild
(Inventory child) Returns true if the given inventory is a direct child of this one.boolean
containsInventory
(Inventory inventory) Returns true if the given inventory is a descendant of this one.int
The number of empty slots in this inventory.<V> Optional<V>
Gets a key defined directly on this Inventory if one is defined.<V> Optional<V>
Gets the key defined in this inventory for the specified (immediate) sub-inventory.boolean
Returns true if this Inventory contains children.Intersects the slots of both inventories.Adds an ItemStack to the slot at given index.Adds one or more ItemStacks to this inventory.parent()
peek()
Returns a copy of the first non empty stack from this Inventory.peekAt
(int index) Returns a copy of the stack at given slot index.poll()
Gets and removes the first non empty stack from this Inventory.poll
(int limit) Gets and removes up tolimit
items of the type in the first non empty stack in this Inventory from all stacks in this Inventory.pollFrom
(int index) Gets and removes the stack at the supplied index in this Inventory.pollFrom
(int index, int limit) Gets and removes the stack at the supplied index in this Inventory.Query this inventory for a single inventory matching the supplied inventory type.default <P> Inventory
query
(Supplier<QueryType.OneParam<P>> queryType, Supplier<P> param) Query this inventory with givenQueryType.OneParam
and one parameter.default <P> Inventory
query
(Supplier<QueryType.OneParam<P>> queryType, P param) Query this inventory with givenQueryType.OneParam
and one parameter.default <P1,
P2> Inventory query
(Supplier<QueryType.TwoParam<P1, P2>> queryType, P1 param1, P2 param2) Query this inventory with givenQueryType.TwoParam
and two parameters.default Inventory
query
(KeyValueMatcher<?> matcher) Query this inventory for inventories matching the suppliedKeyValueMatcher
.Query this inventory with givenQuery
root()
Adds the ItemStack to the slot at given index overwriting the existing item.slot
(int index) Gets theSlot
at the given index.slots()
Returns a list of allSlot
s (leaf nodes) in this Inventory.int
Returns the total quantity of items in this inventory.Constructs a union of the slots in both inventories.
-
Method Details
-
builder
Creates a newInventory.Builder
to build a basicInventory
.Inventories created by this builder cannot be opened.
If you want to show the inventory to a
Player
useViewableInventory.builder()
- Returns:
- The builder
-
parent
Inventory parent()- Returns:
- the parent inventory, returns this inventory if there is no parent (this is a top-level inventory)
-
root
Inventory root()Gets the rootInventory
of thisInventory
. This is equivalent to callingparent()
until it returns itself.- Returns:
- the root inventory, returns this inventory if there is no parent (this is a top-level inventory)
-
slots
Returns a list of allSlot
s (leaf nodes) in this Inventory.- Returns:
- a list of all Slots (leaf nodes) in this 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 tolimit
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
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
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
Gets and removes the stack at the supplied index in this Inventory.- Parameters:
index
- The slot indexlimit
- The item limit- Returns:
- A SUCCESS transaction-result only if all
limit
items were removed else FAILURE. - See Also:
-
peekAt
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
Adds an ItemStack to the slot at given index. Returns aInventoryTransactionResult.Type.SUCCESS
only if the entireItemStack
fits the slot.- Parameters:
index
- The slot indexstack
- 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
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 indexStacks bigger than the max stack size will be partially rejected.
- Parameters:
index
- The slot indexstack
- 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
Gets theSlot
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 forSlot
s and always 0 forEmptyInventory
s.- 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 forSlot
s and always 0 forEmptyInventory
s.- Returns:
- the number of slots in this inventory
-
contains
Checks whether the stacks quantity or more of given stack is contained in this Inventory. To check if an inventory contains any amount usecontainsAny(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
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
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
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 inspectkey
- The key to retrieve the value for- Returns:
- The key value, if available
-
get
Gets a key defined directly on this Inventory if one is defined. For sub-inventories this is effectively the same asinv.getParent().getProperty(inv, property);
but for top-level inventories may include properties defined on the inventory directly.- Specified by:
get
in interfaceValueContainer
- Type Parameters:
V
- The key value type- Parameters:
key
- The key to retrieve the value for- Returns:
- The key value, if available
-
query
Query this inventory with givenQuery
- Parameters:
query
- The query- Returns:
- The queried inventory
-
query
Query this inventory with givenQueryType.OneParam
and one parameter.- Type Parameters:
P
- The parameter type- Parameters:
queryType
- The queryTypeparam
- The parameter- Returns:
- The queried inventory
-
query
Query this inventory with givenQueryType.OneParam
and one parameter.- Type Parameters:
P
- The parameter type- Parameters:
queryType
- The queryTypeparam
- 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 givenQueryType.TwoParam
and two parameters.- Type Parameters:
P1
- The first parameter typeP2
- The second parameter type- Parameters:
queryType
- The queryTypeparam1
- The first parameterparam2
- The second parameter- Returns:
- The queried inventory
-
query
Query this inventory for inventories matching the suppliedKeyValueMatcher
.- Parameters:
matcher
- The key value matcher- Returns:
- The query result
-
query
Query this inventory for a single inventory matching the supplied inventory type. This query will returnOptional.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
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
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
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 useintersect(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
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
Optional<ViewableInventory> 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.
-