Package org.bukkit.inventory

# Interface Inventory

All Superinterfaces:
  `Iterable<ItemStack>`

All Known Subinterfaces:
  `AbstractHorseInventory`, `AnvilInventory`, `ArmoredHorseInventory`, `BeaconInventory`, `BrewerInventory`, `CartographyInventory`, `ChiseledBookshelfInventory`, `CraftingInventory`, `DoubleChestInventory`, `EnchantingInventory`, `FurnaceInventory`, `GrindstoneInventory`, `HorseInventory`, `LecternInventory`, `LlamaInventory`, `LoomInventory`, `MerchantInventory`, `PlayerInventory`, `SaddledHorseInventory`, `SmithingInventory`, `StonecutterInventory`

public interface Inventory extends Iterable<ItemStack>
Interface to the various inventories. Behavior relating to `Material.AIR` is unspecified.
Note that whilst `iterator()` deals with the entire inventory, add / contains / remove methods deal only with the storage contents.
Consider using `getContents()` and `getStorageContents()` for specific iteration.

See Also:
  - `getContents()`
  - `getStorageContents()`

## Method Summary

Modifier and Type
Method
Description
`@NotNull HashMap<Integer,ItemStack>`
`addItem(@NotNull ItemStack... items)`
Stores the given ItemStacks in the inventory.
`@NotNull HashMap<Integer,? extends ItemStack>`
`all(@NotNull Material material)`
Returns a HashMap with all slots and ItemStacks in the inventory with the given Material.
`@NotNull HashMap<Integer,? extends ItemStack>`
`all(@Nullable ItemStack item)`
Finds all slots in the inventory containing any ItemStacks with the given ItemStack.
`void`
`clear()`
Clears out the whole Inventory.
`void`
`clear(int index)`
Clears out a particular slot in the index.
`int`
`close()`
Closes the inventory for all viewers.
`boolean`
`contains(@NotNull Material material)`
Checks if the inventory contains any ItemStacks with the given material.
`boolean`
`contains(@NotNull Material material, int amount)`
Checks if the inventory contains any ItemStacks with the given material, adding to at least the minimum amount specified.
`boolean`
`contains(@Nullable ItemStack item)`
Checks if the inventory contains any ItemStacks matching the given ItemStack.
`boolean`
`contains(@Nullable ItemStack item, int amount)`
Checks if the inventory contains at least the minimum amount specified of exactly matching ItemStacks.
`boolean`
`containsAtLeast(@Nullable 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`
`first(@NotNull ItemStack item)`
Returns the first slot in the inventory containing an ItemStack with the given stack.
`int`
`first(@NotNull Material material)`
Finds the first slot in the inventory containing an ItemStack with the given material
`int`
`firstEmpty()`
Returns the first empty Slot.
`@Nullable ItemStack @NotNull []`
`getContents()`
Returns all ItemStacks from the inventory
`@Nullable InventoryHolder`
`getHolder()`
Gets the block or entity belonging to the open inventory
`@Nullable InventoryHolder`
`getHolder(boolean useSnapshot)`
Gets the block or entity belonging to the open inventory
`@Nullable ItemStack`
`getItem(int index)`
Returns the ItemStack found in the slot at the given index
`@Nullable Location`
`getLocation()`
Get the location of the block or entity which corresponds to this inventory.
`int`
`getMaxStackSize()`
Returns the maximum stack size for an ItemStack in this inventory.
`int`
`getSize()`
Returns the size of the inventory
`@Nullable ItemStack @NotNull []`
`getStorageContents()`
Return the contents from the section of the inventory where items can reasonably be expected to be stored.
`@NotNull InventoryType`
`getType()`
Returns what type of inventory this is.
`@NotNull List<HumanEntity>`
`getViewers()`
Gets a list of players viewing the inventory.
`boolean`
`isEmpty()`
Check whether or not this inventory is empty.
`@NotNull ListIterator<ItemStack>`
`iterator()`
`@NotNull ListIterator<ItemStack>`
`iterator(int index)`
Returns an iterator starting at the given index.
`void`
`remove(@NotNull ItemStack item)`
Removes all stacks in the inventory matching the given stack.
`void`
`remove(@NotNull Material material)`
Removes all stacks in the inventory matching the given material.
`@NotNull HashMap<Integer,ItemStack>`
`removeItem(@NotNull ItemStack... items)`
Removes the given ItemStacks from the storage contents of the inventory.
`@NotNull HashMap<Integer,ItemStack>`
`removeItemAnySlot(@NotNull ItemStack... items)`
Searches all possible inventory slots in order to remove the given ItemStacks.
`void`
`setContents(@Nullable ItemStack @NotNull [] items)`
Completely replaces the inventory's contents.
`void`
`setItem(int index, @Nullable ItemStack item)`
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(@Nullable ItemStack @NotNull [] items)`
Put the given ItemStacks into the storage slots

### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`

## Method Details

### getSize

int getSize()
Returns the size of the inventory

Returns:
  The size of the inventory

### getMaxStackSize

int getMaxStackSize()
Returns the maximum stack size for an ItemStack in this inventory.

Returns:
  The maximum size for an ItemStack in this inventory.

### setMaxStackSize

void setMaxStackSize(int size)
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.

Parameters:
  `size` - The new maximum stack size for items in this inventory.

### getItem

@Nullable @Nullable ItemStack getItem(int index)
Returns the ItemStack found in the slot at the given index

Parameters:
  `index` - The index of the Slot's ItemStack to return
Returns:
  The ItemStack in the slot

### setItem

void setItem(int index, @Nullable @Nullable ItemStack item)
Stores the ItemStack at the given index of the inventory.

Parameters:
  `index` - The index where to put the ItemStack
  `item` - The ItemStack to set

### addItem

@NotNull @NotNull HashMap<Integer,ItemStack> addItem(@NotNull @NotNull ItemStack... items) throws IllegalArgumentException
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.

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

@NotNull @NotNull HashMap<Integer,ItemStack> removeItem(@NotNull @NotNull ItemStack... items) throws IllegalArgumentException
Removes the given ItemStacks from the storage contents of the inventory. For removing ItemStacks from the inventories that have other content groups, like Player inventories, see `removeItemAnySlot(ItemStack...)`.

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.

Parameters:
  `items` - The ItemStacks to remove
Returns:
  A HashMap containing items that couldn't be removed.
Throws:
  `IllegalArgumentException` - if items is null
See Also:
  - `removeItemAnySlot(ItemStack...)`

### removeItemAnySlot

@NotNull @NotNull HashMap<Integer,ItemStack> removeItemAnySlot(@NotNull @NotNull ItemStack... items) throws IllegalArgumentException
Searches all possible inventory slots in order to remove the given ItemStacks.

Similar to `removeItem(ItemStack...)` in behavior, except this method will check all possible slots in the inventory, rather than just the main storage contents.

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.

Parameters:
  `items` - The ItemStacks to remove
Returns:
  A HashMap containing items that couldn't be removed.
Throws:
  `IllegalArgumentException` - if items is null

### getContents

@Nullable @Nullable ItemStack @NotNull [] getContents()
Returns all ItemStacks from the inventory

Returns:
  An array of ItemStacks from the inventory. Individual items may be null.

### setContents

void setContents(@Nullable @Nullable ItemStack @NotNull [] items) throws IllegalArgumentException
Completely replaces the inventory's contents. Removes all existing contents and replaces it with the ItemStacks given in the array.

Parameters:
  `items` - A complete replacement for the contents; the length must be less than or equal to `getSize()`.
Throws:
  `IllegalArgumentException` - If the array has more items than the inventory.

### getStorageContents

@Nullable @Nullable ItemStack @NotNull [] getStorageContents()
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.

Returns:
  inventory storage contents. Individual items may be null.

### setStorageContents

void setStorageContents(@Nullable @Nullable ItemStack @NotNull [] items) throws IllegalArgumentException
Put the given ItemStacks into the storage slots

Parameters:
  `items` - The ItemStacks to use as storage contents
Throws:
  `IllegalArgumentException` - If the array has more items than the inventory.

### contains

boolean contains(@NotNull @NotNull Material material) throws IllegalArgumentException
Checks if the inventory contains any ItemStacks with the given material.

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

@Contract("null -> false") boolean contains(@Nullable @Nullable ItemStack item)
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.

Parameters:
  `item` - The ItemStack to match against
Returns:
  false if item is null, true if any exactly matching ItemStacks were found

### contains

boolean contains(@NotNull @NotNull Material material, int amount) throws IllegalArgumentException
Checks if the inventory contains any ItemStacks with the given material, adding to at least the minimum amount specified.

Parameters:
  `material` - The material to check for
  `amount` - 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

@Contract("null, _ -> false") boolean contains(@Nullable @Nullable ItemStack item, int amount)
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.

Parameters:
  `item` - the ItemStack to match against
  `amount` - how many identical stacks to check for
Returns:
  false if item is null, true if amount less than 1, true if amount of exactly matching ItemStacks were found
See Also:
  - `containsAtLeast(ItemStack, int)`

### containsAtLeast

@Contract("null, _ -> false") boolean containsAtLeast(@Nullable @Nullable ItemStack item, int amount)
Checks if the inventory contains ItemStacks matching the given ItemStack whose amounts sum to at least the minimum amount specified.

Parameters:
  `item` - the ItemStack to match against
  `amount` - 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

@NotNull @NotNull HashMap<Integer,? extends ItemStack> all(@NotNull @NotNull Material material) throws IllegalArgumentException
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.

Parameters:
  `material` - The material to look for
Returns:
  A HashMap containing the slot index, ItemStack pairs
Throws:
  `IllegalArgumentException` - if material is null

### all

@NotNull @NotNull HashMap<Integer,? extends ItemStack> all(@Nullable @Nullable ItemStack item)
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 match

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.

Parameters:
  `item` - The ItemStack to match against
Returns:
  A map from slot indexes to item at index

### first

int first(@NotNull @NotNull Material material) throws IllegalArgumentException
Finds the first slot in the inventory containing an ItemStack with the given material

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

int first(@NotNull @NotNull ItemStack item)
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

Parameters:
  `item` - The ItemStack to match against
Returns:
  The slot index of the given ItemStack or -1 if not found

### firstEmpty

int firstEmpty()
Returns the first empty Slot.

Returns:
  The first empty Slot found, or -1 if no empty slots.

### isEmpty

boolean isEmpty()
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.

Returns:
  true if empty, false otherwise

### remove

void remove(@NotNull @NotNull Material material) throws IllegalArgumentException
Removes all stacks in the inventory matching the given material.

Parameters:
  `material` - The material to remove
Throws:
  `IllegalArgumentException` - if material is null

### remove

void remove(@NotNull @NotNull ItemStack item)
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

Parameters:
  `item` - The ItemStack to match against

### clear

void clear(int index)
Clears out a particular slot in the index.

Parameters:
  `index` - The index to empty.

### clear

void clear()
Clears out the whole Inventory.

### close

int close()
Closes the inventory for all viewers.

Returns:
  the number if viewers the inventory was closed for

### getViewers

@NotNull @NotNull List<HumanEntity> getViewers()
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.

Returns:
  A list of HumanEntities who are viewing this Inventory.

### getType

@NotNull @NotNull InventoryType getType()
Returns what type of inventory this is.

Returns:
  The InventoryType representing the type of inventory.

### getHolder

@Nullable @Nullable InventoryHolder getHolder()
Gets the block or entity belonging to the open inventory

Returns:
  The holder of the inventory; null if it has no holder.

### getHolder

@Nullable @Nullable InventoryHolder getHolder(boolean useSnapshot)
Gets the block or entity belonging to the open inventory

Parameters:
  `useSnapshot` - Create a snapshot if the holder is a tile entity
Returns:
  The holder of the inventory; null if it has no holder.

### iterator

@NotNull @NotNull ListIterator<ItemStack> iterator()

Specified by:
  `iterator` in interface `Iterable<ItemStack>`

### iterator

@NotNull @NotNull ListIterator<ItemStack> iterator(int index)
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).

Parameters:
  `index` - The index.
Returns:
  An iterator.

### getLocation

@Nullable @Nullable Location getLocation()
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.

Returns:
  location or null if not applicable.