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
public interface Inventory extends ValueContainer
Base interface for queryable inventories.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
Inventory.Builder
A builder for free-form Inventories.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description java.util.Optional<ViewableInventory>
asViewable()
Returns this inventory as a viewable inventory if possible.static Inventory.Builder
builder()
Creates a newInventory.Builder
to build a basicInventory
.boolean
canFit(ItemStack stack)
Returns true if the entire stack can fit in this inventory.int
capacity()
The number of slots in this inventory.java.util.List<Inventory>
children()
Returns a list of all direct child inventories.void
clear()
Clears this inventory if it is clearable.boolean
contains(ItemStack stack)
Checks whether the stacks quantity or more of given stack is contained in this Inventory.boolean
contains(ItemType type)
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
freeCapacity()
The number of empty slots in this inventory.<V> java.util.Optional<V>
get(Key<? extends Value<V>> key)
Gets a key defined directly on this Inventory if one is defined.<V> java.util.Optional<V>
get(Inventory child, Key<? extends Value<V>> key)
Gets the key defined in this inventory for the specified (immediate) sub-inventory.boolean
hasChildren()
Returns true if this Inventory contains children.Inventory
intersect(Inventory inventory)
Intersects the slots of both inventories.InventoryTransactionResult
offer(int index, ItemStack stack)
Adds an ItemStack to the slot at given index.InventoryTransactionResult
offer(ItemStack... stacks)
Adds one or more ItemStacks to this inventory.Inventory
parent()
ItemStack
peek()
Returns a copy of the first non empty stack from this Inventory.java.util.Optional<ItemStack>
peekAt(int index)
Returns a copy of the stack at given slot index.InventoryTransactionResult.Poll
poll()
Gets and removes the first non empty stack from this Inventory.InventoryTransactionResult.Poll
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.InventoryTransactionResult.Poll
pollFrom(int index)
Gets and removes the stack at the supplied index in this Inventory.InventoryTransactionResult.Poll
pollFrom(int index, int limit)
Gets and removes the stack at the supplied index in this Inventory.<T extends Inventory>
java.util.Optional<T>query(java.lang.Class<T> inventoryType)
Query this inventory for a single inventory matching the supplied inventory type.default <P> Inventory
query(java.util.function.Supplier<QueryType.OneParam<P>> queryType, java.util.function.Supplier<P> param)
Query this inventory with givenQueryType.OneParam
and one parameter.default <P> Inventory
query(java.util.function.Supplier<QueryType.OneParam<P>> queryType, P param)
Query this inventory with givenQueryType.OneParam
and one parameter.default <P1,P2>
Inventoryquery(java.util.function.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
.Inventory
query(Query query)
Query this inventory with givenQuery
Inventory
root()
InventoryTransactionResult
set(int index, ItemStack stack)
Adds the ItemStack to the slot at given index overwriting the existing item.java.util.Optional<Slot>
slot(int index)
Gets theSlot
at the given index.java.util.List<Slot>
slots()
Returns a list of allSlot
s (leaf nodes) in this Inventory.int
totalQuantity()
Returns the total quantity of items in this inventory.Inventory
union(Inventory inventory)
Constructs a union of the slots in both inventories.
-
-
-
Method Detail
-
builder
static Inventory.Builder 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
java.util.List<Slot> slots()
Returns a list of allSlot
s (leaf nodes) in this Inventory.- Returns:
- a list of all Slots (leaf nodes) in this inventory
-
children
java.util.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
InventoryTransactionResult.Poll 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
InventoryTransactionResult.Poll 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.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
InventoryTransactionResult offer(ItemStack... stacks)
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:
poll()
-
pollFrom
InventoryTransactionResult.Poll pollFrom(int index, int limit)
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:
poll()
-
peekAt
java.util.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 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
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 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
java.util.Optional<Slot> slot(int index)
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
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 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
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> java.util.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 inspectkey
- The key to retrieve the value for- Returns:
- The key value, if available
-
get
<V> java.util.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 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
Inventory query(Query query)
Query this inventory with givenQuery
- Parameters:
query
- The query- Returns:
- The queried inventory
-
query
default <P> Inventory query(java.util.function.Supplier<QueryType.OneParam<P>> queryType, P param)
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 <P> Inventory query(java.util.function.Supplier<QueryType.OneParam<P>> queryType, java.util.function.Supplier<P> param)
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(java.util.function.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
default Inventory query(KeyValueMatcher<?> matcher)
Query this inventory for inventories matching the suppliedKeyValueMatcher
.- Parameters:
matcher
- The key value matcher- Returns:
- The query result
-
query
<T extends Inventory> java.util.Optional<T> query(java.lang.Class<T> inventoryType)
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
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 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
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
java.util.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.
-
-