public interface Inventory extends Iterable<ItemStack>
Material.AIR
is unspecified.
iterator()
deals with the entire inventory, add
/ contains / remove methods deal only with the storage contents.
getContents()
and getStorageContents()
for
specific iteration.getContents()
,
getStorageContents()
Modifier and Type | Method and Description |
---|---|
@NotNull HashMap<Integer,ItemStack> |
addItem(ItemStack... items)
Stores the given ItemStacks in the inventory.
|
@NotNull HashMap<Integer,? extends ItemStack> |
all(@Nullable ItemStack item)
Finds all slots in the inventory containing any ItemStacks with the
given ItemStack.
|
@NotNull HashMap<Integer,? extends ItemStack> |
all(@NotNull Material 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.
|
int |
close()
Closes the inventory for all viewers.
|
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 |
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 |
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[] |
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
|
@NotNull ItemStack[] |
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(ItemStack... items)
Removes the given ItemStacks from the inventory.
|
@NotNull HashMap<Integer,ItemStack> |
removeItemAnySlot(ItemStack... items)
Searches all possible inventory slots in order to remove the given ItemStacks.
|
void |
setContents(@NotNull ItemStack[] 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(@NotNull ItemStack[] items)
Put the given ItemStacks into the storage slots
|
forEach, spliterator
int getSize()
int getMaxStackSize()
void setMaxStackSize(int size)
Caveats:
size
- The new maximum stack size for items in this inventory.@Nullable @Nullable ItemStack getItem(int index)
index
- The index of the Slot's ItemStack to returnvoid setItem(int index, @Nullable @Nullable ItemStack item)
index
- The index where to put the ItemStackitem
- The ItemStack to set@NotNull @NotNull HashMap<Integer,ItemStack> addItem(@NotNull ItemStack... items) throws IllegalArgumentException
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.
items
- The ItemStacks to addIllegalArgumentException
- if items or any element in it is null@NotNull @NotNull HashMap<Integer,ItemStack> removeItem(@NotNull ItemStack... items) throws IllegalArgumentException
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.
items
- The ItemStacks to removeIllegalArgumentException
- if items is null@NotNull @NotNull HashMap<Integer,ItemStack> removeItemAnySlot(@NotNull ItemStack... items) throws IllegalArgumentException
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.
items
- The ItemStacks to removeIllegalArgumentException
- if items is null@Nullable ItemStack[] getContents()
void setContents(@NotNull @NotNull ItemStack[] items) throws IllegalArgumentException
items
- A complete replacement for the contents; the length must
be less than or equal to getSize()
.IllegalArgumentException
- If the array has more items than the
inventory.@NotNull @NotNull ItemStack[] getStorageContents()
void setStorageContents(@NotNull @NotNull ItemStack[] items) throws IllegalArgumentException
items
- The ItemStacks to use as storage contentsIllegalArgumentException
- If the array has more items than the
inventory.boolean contains(@NotNull @NotNull Material material) throws IllegalArgumentException
material
- The material to check forIllegalArgumentException
- if material is null@Contract(value="null -> false") boolean contains(@Nullable @Nullable ItemStack item)
This will only return true if both the type and the amount of the stack match.
item
- The ItemStack to match againstboolean contains(@NotNull @NotNull Material material, int amount) throws IllegalArgumentException
material
- The material to check foramount
- The minimum amountIllegalArgumentException
- if material is null@Contract(value="null, _ -> false") boolean contains(@Nullable @Nullable ItemStack item, int amount)
An ItemStack only counts if both the type and the amount of the stack match.
item
- the ItemStack to match againstamount
- how many identical stacks to check forcontainsAtLeast(ItemStack, int)
@Contract(value="null, _ -> false") boolean containsAtLeast(@Nullable @Nullable ItemStack item, int amount)
item
- the ItemStack to match againstamount
- the minimum amount@NotNull @NotNull HashMap<Integer,? extends ItemStack> all(@NotNull @NotNull Material material) throws IllegalArgumentException
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.
material
- The material to look forIllegalArgumentException
- if material is null@NotNull @NotNull HashMap<Integer,? extends ItemStack> all(@Nullable @Nullable ItemStack item)
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.
item
- The ItemStack to match againstint first(@NotNull @NotNull Material material) throws IllegalArgumentException
material
- The material to look forIllegalArgumentException
- if material is nullint first(@NotNull @NotNull ItemStack item)
item
- The ItemStack to match againstint firstEmpty()
boolean isEmpty()
void remove(@NotNull @NotNull Material material) throws IllegalArgumentException
material
- The material to removeIllegalArgumentException
- if material is nullvoid remove(@NotNull @NotNull ItemStack item)
This will only match a slot if both the type and the amount of the stack match
item
- The ItemStack to match againstvoid clear(int index)
index
- The index to empty.void clear()
int close()
@NotNull @NotNull List<HumanEntity> getViewers()
@NotNull @NotNull InventoryType getType()
@Nullable @Nullable InventoryHolder getHolder()
@Nullable @Nullable InventoryHolder getHolder(boolean useSnapshot)
useSnapshot
- Create a snapshot if the holder is a tile entity@NotNull @NotNull ListIterator<ItemStack> iterator()
@NotNull @NotNull ListIterator<ItemStack> iterator(int index)
index
- The index.Copyright © 2021. All rights reserved.