Class AbstractInventory
- Direct Known Subclasses:
AnvilInventory
,CustomInventory
,PlayerInventory
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AtomicReferenceArray<ItemStack>
protected final InventoryHolder
protected final InventoryType
protected final IntUnaryOperator
protected final InventoryUpdateListener
protected int
protected final IntUnaryOperator
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractInventory
(int size, InventoryHolder inventoryHolder, InventoryType inventoryType, IntUnaryOperator slotConvertor, IntUnaryOperator inverseSlotConvertor) -
Method Summary
Modifier and TypeMethodDescriptionStores the given ItemStacks in the inventory.Finds all slots in the inventory containing any ItemStacks with the given ItemStack.all
(net.kyori.adventure.key.Key material) Returns a HashMap with all slots and ItemStacks in the inventory with the given Material.void
clear()
Clears out the whole Inventory.void
clear
(int index) Clears out a particular slot in the index.boolean
Checks if the inventory contains any ItemStacks matching the given ItemStack.boolean
Checks if the inventory contains at least the minimum amount specified of exactly matching ItemStacks.boolean
contains
(net.kyori.adventure.key.Key material) Checks if the inventory contains any ItemStacks with the given material.boolean
contains
(net.kyori.adventure.key.Key material, int amount) Checks if the inventory contains any ItemStacks with the given material, adding to at least the minimum amount specified.boolean
containsAtLeast
(ItemStack item, int amount) Checks if the inventory contains ItemStacks matching the given ItemStack whose amounts sum to at least the minimum amount specified.int
Returns the first slot in the inventory containing an ItemStack with the given stack.int
first
(net.kyori.adventure.key.Key material) Finds the first slot in the inventory containing an ItemStack with the given materialint
Returns the first empty Slot.int
firstPartial
(net.kyori.adventure.key.Key material) Returns all ItemStacks from the inventoryGets the block or entity belonging to the open inventorygetItem
(int index) Returns the ItemStack found in the slot at the given indexGet the location of the block or entity which corresponds to this inventory.int
Returns the maximum stack size for an ItemStack in this inventory.int
getSize()
Returns the size of the inventoryReturn the contents from the section of the inventory where items can reasonably be expected to be stored.getType()
Returns what type of inventory this is.Deprecated.Gets a list of players viewing the inventory.boolean
isEmpty()
Check whether or not this inventory is empty.iterator()
iterator
(int index) Returns an iterator starting at the given index.void
Removes all stacks in the inventory matching the given stack.void
remove
(net.kyori.adventure.key.Key material) Removes all stacks in the inventory matching the given material.removeItem
(ItemStack... items) Removes the given ItemStacks from the inventory.void
setContents
(ItemStack[] items) Completely replaces the inventory's contents.void
Stores the ItemStack at the given index of the inventory.void
setMaxStackSize
(int size) This method allows you to change the maximum stack size for an inventory.void
setStorageContents
(ItemStack[] items) Put the given ItemStacks into the storage slotsvoid
void
updateInventory
(Player player) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
inventoryHolder
-
viewers
-
inventoryType
-
inventory
-
listener
-
slotConvertor
-
inverseSlotConvertor
-
maxStackSize
protected int maxStackSize
-
-
Constructor Details
-
AbstractInventory
public AbstractInventory(int size, InventoryHolder inventoryHolder, InventoryType inventoryType, IntUnaryOperator slotConvertor, IntUnaryOperator inverseSlotConvertor)
-
-
Method Details
-
updateInventory
- Specified by:
updateInventory
in interfaceInventory
-
updateInventory
public void updateInventory()- Specified by:
updateInventory
in interfaceInventory
-
getLocation
Description copied from interface:Inventory
Get the location of the block or entity which corresponds to this inventory. May return null if this container was custom created or is a virtual / subcontainer.- Specified by:
getLocation
in interfaceInventory
- Returns:
- location or null if not applicable.
-
getSize
public int getSize()Description copied from interface:Inventory
Returns the size of the inventory -
getMaxStackSize
public int getMaxStackSize()Description copied from interface:Inventory
Returns the maximum stack size for an ItemStack in this inventory.- Specified by:
getMaxStackSize
in interfaceInventory
- Returns:
- The maximum size for an ItemStack in this inventory.
-
setMaxStackSize
public void setMaxStackSize(int size) Description copied from interface:Inventory
This method allows you to change the maximum stack size for an inventory.Caveats:
- Not all inventories respect this value.
- Stacks larger than 127 may be clipped when the world is saved.
- This value is not guaranteed to be preserved; be sure to set it before every time you want to set a slot over the max stack size.
- Stacks larger than the default max size for this type of inventory may not display correctly in the client.
- Specified by:
setMaxStackSize
in interfaceInventory
- Parameters:
size
- The new maximum stack size for items in this inventory.
-
getItem
Description copied from interface:Inventory
Returns the ItemStack found in the slot at the given index -
setItem
Description copied from interface:Inventory
Stores the ItemStack at the given index of the inventory. -
firstPartial
public int firstPartial(net.kyori.adventure.key.Key material) -
addItem
Description copied from interface:Inventory
Stores the given ItemStacks in the inventory. This will try to fill existing stacks and empty slots as well as it can.The returned HashMap contains what it couldn't store, where the key is the index of the parameter, and the value is the ItemStack at that index of the varargs parameter. If all items are stored, it will return an empty HashMap.
If you pass in ItemStacks which exceed the maximum stack size for the Material, first they will be added to partial stacks where Material.getMaxStackSize() is not exceeded, up to Material.getMaxStackSize(). When there are no partial stacks left stacks will be split on Inventory.getMaxStackSize() allowing you to exceed the maximum stack size for that material.
It is known that in some implementations this method will also set the inputted argument amount to the number of that item not placed in slots.
- Specified by:
addItem
in interfaceInventory
- Parameters:
items
- The ItemStacks to add- Returns:
- A HashMap containing items that didn't fit.
- Throws:
IllegalArgumentException
- if items or any element in it is null
-
removeItem
Description copied from interface:Inventory
Removes the given ItemStacks from the inventory.It will try to remove 'as much as possible' from the types and amounts you give as arguments.
The returned HashMap contains what it couldn't remove, where the key is the index of the parameter, and the value is the ItemStack at that index of the varargs parameter. If all the given ItemStacks are removed, it will return an empty HashMap.
It is known that in some implementations this method will also set the inputted argument amount to the number of that item not removed from slots.
- Specified by:
removeItem
in interfaceInventory
- Parameters:
items
- The ItemStacks to remove- Returns:
- A HashMap containing items that couldn't be removed.
- Throws:
IllegalArgumentException
- if items is null
-
getContents
Description copied from interface:Inventory
Returns all ItemStacks from the inventory- Specified by:
getContents
in interfaceInventory
- Returns:
- An array of ItemStacks from the inventory. Individual items may be null.
-
setContents
Description copied from interface:Inventory
Completely replaces the inventory's contents. Removes all existing contents and replaces it with the ItemStacks given in the array.- Specified by:
setContents
in interfaceInventory
- Parameters:
items
- A complete replacement for the contents; the length must be less than or equal toInventory.getSize()
.- Throws:
IllegalArgumentException
- If the array has more items than the inventory.
-
getStorageContents
Description copied from interface:Inventory
Return the contents from the section of the inventory where items can reasonably be expected to be stored. In most cases this will represent the entire inventory, but in some cases it may exclude armor or result slots.
It is these contents which will be used for add / contains / remove methods which look for a specific stack.- Specified by:
getStorageContents
in interfaceInventory
- Returns:
- inventory storage contents. Individual items may be null.
-
setStorageContents
Description copied from interface:Inventory
Put the given ItemStacks into the storage slots- Specified by:
setStorageContents
in interfaceInventory
- Parameters:
items
- The ItemStacks to use as storage contents- Throws:
IllegalArgumentException
- If the array has more items than the inventory.
-
contains
Description copied from interface:Inventory
Checks if the inventory contains any ItemStacks with the given material.- Specified by:
contains
in interfaceInventory
- Parameters:
material
- The material to check for- Returns:
- true if an ItemStack is found with the given Material
- Throws:
IllegalArgumentException
- if material is null
-
contains
Description copied from interface:Inventory
Checks if the inventory contains any ItemStacks matching the given ItemStack.This will only return true if both the type and the amount of the stack match.
-
contains
public boolean contains(net.kyori.adventure.key.Key material, int amount) throws IllegalArgumentException Description copied from interface:Inventory
Checks if the inventory contains any ItemStacks with the given material, adding to at least the minimum amount specified.- Specified by:
contains
in interfaceInventory
- Parameters:
material
- The material to check foramount
- The minimum amount- Returns:
- true if amount is less than 1, true if enough ItemStacks were found to add to the given amount
- Throws:
IllegalArgumentException
- if material is null
-
contains
Description copied from interface:Inventory
Checks if the inventory contains at least the minimum amount specified of exactly matching ItemStacks.An ItemStack only counts if both the type and the amount of the stack match.
-
containsAtLeast
Description copied from interface:Inventory
Checks if the inventory contains ItemStacks matching the given ItemStack whose amounts sum to at least the minimum amount specified.- Specified by:
containsAtLeast
in interfaceInventory
- Parameters:
item
- the ItemStack to match againstamount
- the minimum amount- Returns:
- false if item is null, true if amount less than 1, true if enough ItemStacks were found to add to the given amount
-
all
public HashMap<Integer,? extends ItemStack> all(net.kyori.adventure.key.Key material) throws IllegalArgumentException Description copied from interface:Inventory
Returns a HashMap with all slots and ItemStacks in the inventory with the given Material.The HashMap contains entries where, the key is the slot index, and the value is the ItemStack in that slot. If no matching ItemStack with the given Material is found, an empty map is returned.
- Specified by:
all
in interfaceInventory
- Parameters:
material
- The material to look for- Returns:
- A HashMap containing the slot index, ItemStack pairs
- Throws:
IllegalArgumentException
- if material is null
-
all
Description copied from interface:Inventory
Finds all slots in the inventory containing any ItemStacks with the given ItemStack. This will only match slots if both the type and the amount of the stack matchThe HashMap contains entries where, the key is the slot index, and the value is the ItemStack in that slot. If no matching ItemStack with the given Material is found, an empty map is returned.
-
first
Description copied from interface:Inventory
Finds the first slot in the inventory containing an ItemStack with the given material- Specified by:
first
in interfaceInventory
- Parameters:
material
- The material to look for- Returns:
- The slot index of the given Material or -1 if not found
- Throws:
IllegalArgumentException
- if material is null
-
first
Description copied from interface:Inventory
Returns the first slot in the inventory containing an ItemStack with the given stack. This will only match a slot if both the type and the amount of the stack match -
firstEmpty
public int firstEmpty()Description copied from interface:Inventory
Returns the first empty Slot.- Specified by:
firstEmpty
in interfaceInventory
- Returns:
- The first empty Slot found, or -1 if no empty slots.
-
isEmpty
public boolean isEmpty()Description copied from interface:Inventory
Check whether or not this inventory is empty. An inventory is considered to be empty if there are no ItemStacks in any slot of this inventory. -
remove
Description copied from interface:Inventory
Removes all stacks in the inventory matching the given material.- Specified by:
remove
in interfaceInventory
- Parameters:
material
- The material to remove- Throws:
IllegalArgumentException
- if material is null
-
remove
Description copied from interface:Inventory
Removes all stacks in the inventory matching the given stack.This will only match a slot if both the type and the amount of the stack match
-
clear
public void clear(int index) Description copied from interface:Inventory
Clears out a particular slot in the index. -
clear
public void clear()Description copied from interface:Inventory
Clears out the whole Inventory. -
getViewers
Description copied from interface:Inventory
Gets a list of players viewing the inventory. Note that a player is considered to be viewing their own inventory and internal crafting screen even when said inventory is not open. They will normally be considered to be viewing their inventory even when they have a different inventory screen open, but it's possible for customized inventory screens to exclude the viewer's inventory, so this should never be assumed to be non-empty.- Specified by:
getViewers
in interfaceInventory
- Returns:
- A set of HumanEntities who are viewing this Inventory.
-
getType
Description copied from interface:Inventory
Returns what type of inventory this is. -
getHolder
Description copied from interface:Inventory
Gets the block or entity belonging to the open inventory -
iterator
-
iterator
Description copied from interface:Inventory
Returns an iterator starting at the given index. If the index is positive, then the first call to next() will return the item at that index; if it is negative, the first call to previous will return the item at index (getSize() + index). -
getUnsafe
Deprecated.
-