Class Inventory

java.lang.Object
xyz.xenondevs.invui.inventory.Inventory
Direct Known Subclasses:
CompositeInventory, ObscuredInventory, ReferencingInventory, VirtualInventory

public abstract class Inventory extends Object
An inventory that can be embedded in InvUI's Guis.

Provides several utility methods to easily add and remove items from the inventory, as well as an advanced event system allowing to listen for and affect changes in the inventory.

General contracts of this class and all of its implementations are:

  • There are no ItemStacks of type Material.AIR or ItemStack.getAmount() == 0.
    Empty ItemStacks are represented by null.
  • Unless otherwise specified, all methods will return a clone of the actual backing ItemStack.
    Changes to returned ItemStacks will never affect the Inventory.
  • Unless otherwise specified, all methods accepting ItemStacks will always clone them before putting them in the backing array / inventory.
    Changes to the passed ItemStacks after calling a method will never affect the Inventory.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    addItem(@Nullable UpdateReason updateReason, @NotNull org.bukkit.inventory.ItemStack itemStack)
    Adds an ItemStack to the Inventory.
    int
    addItemAmount(@Nullable UpdateReason updateReason, int slot, int amount)
    Adds a specific amount to an ItemStack on a slot while respecting the maximum allowed stack size on that slot.
    void
    Adds an AbstractWindow to the set of AbstractWindows, telling the Inventory that its contents are now being displayed in that AbstractWindow.
    void
    callPostUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable org.bukkit.inventory.ItemStack newItemStack)
    Creates an ItemPostUpdateEvent and calls the postUpdateHandler to handle it.
    callPreUpdateEvent(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable org.bukkit.inventory.ItemStack newItemStack)
    Creates an ItemPreUpdateEvent and calls the preUpdateHandler to handle it.
    boolean
    canHold(@NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
    Simulates adding ItemStacks to this Inventory and then returns if all ItemStacks would fit.
    boolean
    canHold(@NotNull org.bukkit.inventory.ItemStack first, @NotNull org.bukkit.inventory.ItemStack @NotNull ... rest)
    Simulates adding ItemStacks to this Inventory and then returns if all ItemStacks would fit.
    int
    collectSimilar(@Nullable UpdateReason updateReason, @NotNull org.bukkit.inventory.ItemStack itemStack)
    Finds all ItemStacks similar to the provided ItemStack and removes them from their slot until the amount of the given ItemStack reaches its maximum stack size.
    int
    collectSimilar(@Nullable UpdateReason updateReason, @NotNull org.bukkit.inventory.ItemStack template, int baseAmount)
    Finds all ItemStacks similar to the provided ItemStack and removes them from their slot until the maximum stack size of the Material is reached.
    boolean
    contains(Predicate<org.bukkit.inventory.ItemStack> predicate)
    Checks if there is any ItemStack in this Inventory matching the given Predicate.
    boolean
    containsSimilar(org.bukkit.inventory.ItemStack itemStack)
    Checks if there is any ItemStack in this Inventory similar to the given ItemStack.
    int
    count(Predicate<org.bukkit.inventory.ItemStack> predicate)
    Counts the amount of ItemStacks in this Inventory matching the given Predicate.
    int
    countSimilar(org.bukkit.inventory.ItemStack itemStack)
    Counts the amount of ItemStacks in this Inventory similar to the given ItemStack.
    boolean
    forceSetItem(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack itemStack)
    Changes the ItemStack on a specific slot to that one, regardless of what was previously on that slot.
    int
    Gets the priority for click actions in a Gui, such as shift clicking or cursor collection with multiple VirtualInventories.
    abstract @Nullable org.bukkit.inventory.ItemStack
    getItem(int slot)
    Gets a clone of the ItemStack on that slot.
    int
    getItemAmount(int slot)
    Gets the ItemStack amount of the ItemStack on the given slot.
    abstract @Nullable org.bukkit.inventory.ItemStack @NotNull []
    Gets a copy of the contents of this Inventory.
    abstract int
    Gets the maximum stack size for a specific slot while ignoring max stack size of the ItemStack on it.
    int
    getMaxSlotStackSize(int slot, int alternative)
    Gets the maximum stack size for a specific slot while ignoring the ItemStack on it.
    int
    getMaxSlotStackSize(int slot, @Nullable org.bukkit.inventory.ItemStack alternativeFrom)
    Gets the maximum stack size for a specific slot while ignoring the ItemStack on it.
    int
    getMaxStackSize(int slot)
    Gets the maximum stack size for a specific slot.
    int
    getMaxStackSize(int slot, int alternative)
    Gets the maximum stack size for a specific slot.
    int
    getMaxStackSize(int slot, @Nullable org.bukkit.inventory.ItemStack alternativeFrom)
    Gets the maximum stack size for a specific slot.
    abstract int @NotNull []
    Gets the array of max stack sizes for this Inventory.
    @Nullable Consumer<@NotNull ItemPostUpdateEvent>
    Gets the configured post update handler.
    @Nullable Consumer<@NotNull ItemPreUpdateEvent>
    Gets the configured pre update handler.
    abstract int
    Gets the size of this Inventory.
    abstract @Nullable org.bukkit.inventory.ItemStack
    getUnsafeItem(int slot)
    Gets the ItemStack on that slot.
    abstract @Nullable org.bukkit.inventory.ItemStack @NotNull []
    Gets the ItemStacks this Inventory contains.
    @NotNull Set<@NotNull Window>
    Gets a set of Windows that display this Inventory.
    boolean
    Checks whether this Inventory has at least one empty slot.
    boolean
    Whether this Inventory has any event handlers.
    boolean
    hasItem(int slot)
    Checks if there is an ItemStack on that slot.
    boolean
    Checks if there are no ItemStacks in this Inventory.
    boolean
    Checks if all slots have an ItemStack with their max stack size on them.
    boolean
    isSynced(int slot, org.bukkit.inventory.ItemStack assumedStack)
    Checks if the ItemStack on that slot is the same as the assumed ItemStack provided as parameter.
    boolean
    modifyItem(@Nullable UpdateReason updateReason, int slot, @NotNull Consumer<@Nullable org.bukkit.inventory.ItemStack> modifier)
    Changes the ItemStack on a specific slot based on the current ItemStack on that slot, using a modifier consumer.
    void
    Notifies all Windows displaying this Inventory to update their representative ItemStacks.
    int
    putItem(@Nullable UpdateReason updateReason, int slot, @NotNull org.bukkit.inventory.ItemStack itemStack)
    Adds an ItemStack on a specific slot and returns the amount of items that did not fit on that slot.
    int
    removeFirst(@Nullable UpdateReason updateReason, int amount, @NotNull Predicate<@NotNull org.bukkit.inventory.ItemStack> predicate)
    Removes the first n ItemStacks matching the given Predicate.
    int
    removeFirstSimilar(@Nullable UpdateReason updateReason, int amount, @NotNull org.bukkit.inventory.ItemStack itemStack)
    Removes the first n ItemStacks that are similar to the specified ItemStack.
    int
    removeIf(@Nullable UpdateReason updateReason, @NotNull Predicate<@NotNull org.bukkit.inventory.ItemStack> predicate)
    Removes all ItemStacks matching the given Predicate.
    int
    removeSimilar(@Nullable UpdateReason updateReason, @NotNull org.bukkit.inventory.ItemStack itemStack)
    Removes all ItemStacks that are similar to the specified ItemStack.
    void
    Removes an AbstractWindow from the set of AbstractWindows, telling the Inventory that its contents are no longer being displayed in that AbstractWindow.
    boolean
    replaceItem(@Nullable UpdateReason updateReason, int slot, @NotNull Function<@Nullable org.bukkit.inventory.ItemStack,@Nullable org.bukkit.inventory.ItemStack> function)
    Replaces the ItemStack on a specific slot based on the current ItemStack on that slot, using a replace function.
    protected abstract void
    setCloneBackingItem(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack)
    Clones the given ItemStack and sets in the backing array of this Inventory.
    protected abstract void
    setDirectBackingItem(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack)
    Sets the ItemStack in the backing array of this Inventory without explicitly cloning it.
    void
    setGuiPriority(int guiPriority)
    Sets the priority for click actions in a Gui, such as shift-clicking or cursor collection with multiple VirtualInventories.
    boolean
    setItem(@Nullable UpdateReason updateReason, int slot, @Nullable org.bukkit.inventory.ItemStack itemStack)
    Changes the ItemStack on a specific slot to the given one, regardless of what previously was on that slot.
    int
    setItemAmount(@Nullable UpdateReason updateReason, int slot, int amount)
    Sets the amount of an ItemStack on a slot to the given value while respecting the max allowed stack size on that slot.
    void
    setItemSilently(int slot, @Nullable org.bukkit.inventory.ItemStack itemStack)
    Changes the ItemStack on a specific slot to that one, regardless of what was previously on that slot.
    void
    setPostUpdateHandler(@Nullable Consumer<@NotNull ItemPostUpdateEvent> inventoryUpdatedHandler)
    Sets a handler which is called every time after something has been updated in the Inventory.
    void
    setPreUpdateHandler(@Nullable Consumer<@NotNull ItemPreUpdateEvent> preUpdateHandler)
    Sets a handler which is called every time something gets updated in the Inventory.
    int[]
    simulateAdd(@NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
    Simulates adding ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
    int[]
    simulateAdd(@NotNull org.bukkit.inventory.ItemStack first, @NotNull org.bukkit.inventory.ItemStack @NotNull ... rest)
    Simulates adding ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
    int[]
    simulateMultiAdd(@NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
    Simulates adding multiple ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
    int
    simulateSingleAdd(@NotNull org.bukkit.inventory.ItemStack itemStack)
    Returns the amount of items that wouldn't fit in the inventory when added.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Inventory

      public Inventory()
  • Method Details

    • getSize

      public abstract int getSize()
      Gets the size of this Inventory.
      Returns:
      How many slots this Inventory has.
    • getMaxStackSizes

      public abstract int @NotNull [] getMaxStackSizes()
      Gets the array of max stack sizes for this Inventory.
      Returns:
      The array defining the max stack sizes for this Inventory
    • getMaxSlotStackSize

      public abstract int getMaxSlotStackSize(int slot)
      Gets the maximum stack size for a specific slot while ignoring max stack size of the ItemStack on it.
      Parameters:
      slot - The slot
      Returns:
      The maximum stack size on that slo
    • getItems

      @Nullable public abstract @Nullable org.bukkit.inventory.ItemStack @NotNull [] getItems()
      Gets a copy of the contents of this Inventory.

      It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.

      Returns:
      A deep copy of the ItemStacks this Inventory contains.
    • getUnsafeItems

      @Nullable public abstract @Nullable org.bukkit.inventory.ItemStack @NotNull [] getUnsafeItems()
      Gets the ItemStacks this Inventory contains. Depending on the implementation, this method may return a copy, a deep copy, or the actual backing item stack array. Modifying the returned array might or might not reflect in this Inventory.

      It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.

      Returns:
      The ItemStacks this Inventory contains.
    • getItem

      @Nullable public abstract @Nullable org.bukkit.inventory.ItemStack getItem(int slot)
      Gets a clone of the ItemStack on that slot.

      It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.

      Parameters:
      slot - The slot
      Returns:
      The ItemStack on the given slot
    • getUnsafeItem

      @Nullable public abstract @Nullable org.bukkit.inventory.ItemStack getUnsafeItem(int slot)
      Gets the ItemStack on that slot. Depending on the implementation, this method may a copy of or the actual backing ItemStack.

      It is guaranteed that this method will never return an air / empty item stack. Those are always represented by null.

      Parameters:
      slot - The slot
      Returns:
      The ItemStack on the given slot.
    • setCloneBackingItem

      protected abstract void setCloneBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Clones the given ItemStack and sets in the backing array of this Inventory.

      This method should never be invoked with an air / empty item stack. Those should always be represented by null.

      Parameters:
      slot - The slot
      itemStack - The ItemStack to be set
    • setDirectBackingItem

      protected abstract void setDirectBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Sets the ItemStack in the backing array of this Inventory without explicitly cloning it. Depending on the implementation, the ItemStack might still be cloned.

      This method should never be invoked with an air / empty item stack. Those should always be represented by null.

      Parameters:
      slot - The slot
      itemStack - The ItemStack to be set
    • getWindows

      @NotNull public @NotNull Set<@NotNull Window> getWindows()
      Gets a set of Windows that display this Inventory.
      Returns:
      An unmodifiable view of the set that contains all Windows that display content of this Inventory.
    • addWindow

      public void addWindow(AbstractWindow window)
      Adds an AbstractWindow to the set of AbstractWindows, telling the Inventory that its contents are now being displayed in that AbstractWindow.
      Parameters:
      window - The Window to be added.
    • removeWindow

      public void removeWindow(AbstractWindow window)
      Removes an AbstractWindow from the set of AbstractWindows, telling the Inventory that its contents are no longer being displayed in that AbstractWindow.
      Parameters:
      window - The AbstractWindow to be removed.
    • notifyWindows

      public void notifyWindows()
      Notifies all Windows displaying this Inventory to update their representative ItemStacks. This method should only be called manually in very specific cases like when the ItemMeta of an ItemStack in this inventory has changed.
    • getPreUpdateHandler

      @Nullable public @Nullable Consumer<@NotNull ItemPreUpdateEvent> getPreUpdateHandler()
      Gets the configured pre update handler.
      Returns:
      The pre update handler
    • setPreUpdateHandler

      public void setPreUpdateHandler(@Nullable @Nullable Consumer<@NotNull ItemPreUpdateEvent> preUpdateHandler)
      Sets a handler which is called every time something gets updated in the Inventory.
      Parameters:
      preUpdateHandler - The new item update handler
    • getPostUpdateHandler

      @Nullable public @Nullable Consumer<@NotNull ItemPostUpdateEvent> getPostUpdateHandler()
      Gets the configured post update handler.
      Returns:
      The post update handler
    • setPostUpdateHandler

      public void setPostUpdateHandler(@Nullable @Nullable Consumer<@NotNull ItemPostUpdateEvent> inventoryUpdatedHandler)
      Sets a handler which is called every time after something has been updated in the Inventory.
      Parameters:
      inventoryUpdatedHandler - The new handler
    • hasEventHandlers

      public boolean hasEventHandlers()
      Whether this Inventory has any event handlers.
      Returns:
      `true` if this Inventory has a pre- or post-update handler.
    • callPreUpdateEvent

      public ItemPreUpdateEvent callPreUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack)
      Creates an ItemPreUpdateEvent and calls the preUpdateHandler to handle it.
      Parameters:
      updateReason - The UpdateReason.
      slot - The slot of the affected ItemStack.
      previousItemStack - The ItemStack that was previously on that slot.
      newItemStack - The ItemStack that will be on that slot.
      Returns:
      The ItemPreUpdateEvent after it has been handled by the preUpdateHandler.
    • callPostUpdateEvent

      public void callPostUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack)
      Creates an ItemPostUpdateEvent and calls the postUpdateHandler to handle it.
      Parameters:
      updateReason - The UpdateReason.
      slot - The slot of the affected ItemStack.
      previousItemStack - The ItemStack that was on that slot previously.
      newItemStack - The ItemStack that is on that slot now.
    • getGuiPriority

      public int getGuiPriority()
      Gets the priority for click actions in a Gui, such as shift clicking or cursor collection with multiple VirtualInventories.
      Returns:
      The priority for click actions, VirtualInventories with a higher priority get prioritized.
    • setGuiPriority

      public void setGuiPriority(int guiPriority)
      Sets the priority for click actions in a Gui, such as shift-clicking or cursor collection with multiple VirtualInventories.
      Parameters:
      guiPriority - The priority for click actions, VirtualInventories with a higher priority get prioritized.
    • getMaxStackSize

      public int getMaxStackSize(int slot)
      Gets the maximum stack size for a specific slot.

      If there is an ItemStack on that slot, the returned value will be the minimum of both the slot's max stack size and the ItemStack's max stack size retrieved using InventoryUtils.stackSizeProvider.

      Parameters:
      slot - The slot
      Returns:
      The current maximum allowed stack size on the specific slot.
    • getMaxStackSize

      public int getMaxStackSize(int slot, int alternative)
      Gets the maximum stack size for a specific slot.

      If there is an ItemStack on that slot, the returned value will be the minimum of both the slot's max stack size and the ItemStack's max stack size retrieved using InventoryUtils.stackSizeProvider.

      If there is no ItemStack on that slot, the alternative parameter will be used as a potential maximum stack size.

      Parameters:
      slot - The slot
      alternative - The alternative maximum stack size if no ItemStack is placed on that slot. Should probably be the max stack size of the Material that will be added.
      Returns:
      The current maximum allowed stack size on the specific slot.
    • getMaxStackSize

      public int getMaxStackSize(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack alternativeFrom)
      Gets the maximum stack size for a specific slot. If there is an ItemStack on that slot, the returned value will be the minimum of both the slot's and the ItemStack's max stack size retrieved using InventoryUtils.stackSizeProvider. If there is no ItemStack on that slot, the alternativeFrom parameter will be used to determine a potential maximum stack size.
      Parameters:
      slot - The slot
      alternativeFrom - The alternative ItemStack to determine the potential maximum stack size. Uses 64 if null.
      Returns:
      The current maximum allowed stack size on the specific slot.
    • getMaxSlotStackSize

      public int getMaxSlotStackSize(int slot, int alternative)
      Gets the maximum stack size for a specific slot while ignoring the ItemStack on it. The returned value will be a minimum of the slot's maximum stack size and the alternative parameter.
      Parameters:
      slot - The slot
      alternative - The alternative maximum stack size. Should probably be the max stack size of the Material that will be added.
      Returns:
      The maximum stack size on that slot
    • getMaxSlotStackSize

      public int getMaxSlotStackSize(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack alternativeFrom)
      Gets the maximum stack size for a specific slot while ignoring the ItemStack on it. The returned value will be a minimum of the maximum stack size of both the slot and the alternativeFrom parameter.
      Parameters:
      slot - The slot
      alternativeFrom - The alternative ItemStack to determine the potential maximum stack size. Uses 64 if null.
      Returns:
      The maximum stack size on that slot
    • isSynced

      public boolean isSynced(int slot, org.bukkit.inventory.ItemStack assumedStack)
      Checks if the ItemStack on that slot is the same as the assumed ItemStack provided as parameter.
      Parameters:
      slot - The slot
      assumedStack - The assumed ItemStack
      Returns:
      If the ItemStack on that slot is the same as the assumed ItemStack
    • isFull

      public boolean isFull()
      Checks if all slots have an ItemStack with their max stack size on them.
      Returns:
      Whether this Inventory is full.
    • isEmpty

      public boolean isEmpty()
      Checks if there are no ItemStacks in this Inventory.
      Returns:
      Whether this Inventory is empty.
    • hasEmptySlot

      public boolean hasEmptySlot()
      Checks whether this Inventory has at least one empty slot.
      Returns:
      Whether this Inventory has at least one empty slot.
    • contains

      public boolean contains(Predicate<org.bukkit.inventory.ItemStack> predicate)
      Checks if there is any ItemStack in this Inventory matching the given Predicate.
      Parameters:
      predicate - The Predicate to check.
      Returns:
      Whether there is any ItemStack in this Inventory matching the given Predicate.
    • containsSimilar

      public boolean containsSimilar(org.bukkit.inventory.ItemStack itemStack)
      Checks if there is any ItemStack in this Inventory similar to the given ItemStack.
      Parameters:
      itemStack - The ItemStack to match against.
      Returns:
      Whether there is any ItemStack in this Inventory similar to the given ItemStack.
    • count

      public int count(Predicate<org.bukkit.inventory.ItemStack> predicate)
      Counts the amount of ItemStacks in this Inventory matching the given Predicate.
      Parameters:
      predicate - The Predicate to check.
      Returns:
      The amount of ItemStacks in this Inventory matching the given Predicate.
    • countSimilar

      public int countSimilar(org.bukkit.inventory.ItemStack itemStack)
      Counts the amount of ItemStacks in this Inventory similar to the given ItemStack.
      Parameters:
      itemStack - The ItemStack to match against.
      Returns:
      The amount of ItemStacks in this Inventory similar to the given ItemStack.
    • hasItem

      public boolean hasItem(int slot)
      Checks if there is an ItemStack on that slot.
      Parameters:
      slot - The Slot
      Returns:
      If there is an ItemStack on that slot.
    • getItemAmount

      public int getItemAmount(int slot)
      Gets the ItemStack amount of the ItemStack on the given slot.
      Parameters:
      slot - The slot
      Returns:
      The amount of items on that slot
    • setItemSilently

      public void setItemSilently(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Changes the ItemStack on a specific slot to that one, regardless of what was previously on that slot.

      This method does not call an ItemPreUpdateEvent and ignores the maximum allowed stack size of both the Material and the slot.

      This method will always be successful.

      Parameters:
      slot - The slot
      itemStack - The ItemStack to set.
    • forceSetItem

      public boolean forceSetItem(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Changes the ItemStack on a specific slot to that one, regardless of what was previously on that slot.
      This method ignores the maximum allowed stack size of both the Material and the slot.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot
      itemStack - The ItemStack to set.
      Returns:
      If the action was successful
    • setItem

      public boolean setItem(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Changes the ItemStack on a specific slot to the given one, regardless of what previously was on that slot.
      This method will fail if the given ItemStack does not completely fit because of the maximum allowed stack size.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot
      itemStack - The ItemStack to set.
      Returns:
      If the action was successful
    • modifyItem

      public boolean modifyItem(@Nullable @Nullable UpdateReason updateReason, int slot, @NotNull @NotNull Consumer<@Nullable org.bukkit.inventory.ItemStack> modifier)
      Changes the ItemStack on a specific slot based on the current ItemStack on that slot, using a modifier consumer.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot.
      modifier - The modifier consumer. Accepts the current ItemStack and modifies it.
      Returns:
      If the action was successful.
    • replaceItem

      public boolean replaceItem(@Nullable @Nullable UpdateReason updateReason, int slot, @NotNull @NotNull Function<@Nullable org.bukkit.inventory.ItemStack,@Nullable org.bukkit.inventory.ItemStack> function)
      Replaces the ItemStack on a specific slot based on the current ItemStack on that slot, using a replace function.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot.
      function - The replace function. The argument is the current ItemStack, the return value is the new ItemStack.
      Returns:
      If the action was successful.
    • putItem

      public int putItem(@Nullable @Nullable UpdateReason updateReason, int slot, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Adds an ItemStack on a specific slot and returns the amount of items that did not fit on that slot.

      This method will fail if there is an ItemStack on that slot that is not similar to the given one.

      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot
      itemStack - The ItemStack to add.
      Returns:
      The amount of items that did not fit on that slot.
    • setItemAmount

      public int setItemAmount(@Nullable @Nullable UpdateReason updateReason, int slot, int amount)
      Sets the amount of an ItemStack on a slot to the given value while respecting the max allowed stack size on that slot.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot
      amount - The amount to change to.
      Returns:
      The amount that it actually changed to.
      Throws:
      IllegalStateException - If there is no ItemStack on that slot.
    • addItemAmount

      public int addItemAmount(@Nullable @Nullable UpdateReason updateReason, int slot, int amount)
      Adds a specific amount to an ItemStack on a slot while respecting the maximum allowed stack size on that slot. Returns 0 if there is no ItemStack on that slot.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      slot - The slot
      amount - The amount to add
      Returns:
      The amount that was actually added.
    • addItem

      public int addItem(@Nullable @Nullable UpdateReason updateReason, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Adds an ItemStack to the Inventory. This method does not work the same way as Bukkit's addItem method as it respects the max stack size of the item type.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      itemStack - The ItemStack to add
      Returns:
      The amount of items that didn't fit
      See Also:
    • simulateAdd

      public int[] simulateAdd(@NotNull @NotNull org.bukkit.inventory.ItemStack first, @NotNull @NotNull org.bukkit.inventory.ItemStack @NotNull ... rest)
      Simulates adding ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
      Parameters:
      first - The first ItemStack to use.
      rest - The rest of the ItemStacks to use.
      Returns:
      An array of integers representing the leftover amount for each ItemStack provided. The size of this array is always equal to the amount of ItemStacks provided as method parameters.
    • simulateAdd

      public int[] simulateAdd(@NotNull @NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
      Simulates adding ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
      Parameters:
      itemStacks - The ItemStack to use.
      Returns:
      An array of integers representing the leftover amount for each ItemStack provided. The size of this array is always equal to the amount of ItemStacks provided as method parameters.
    • canHold

      public boolean canHold(@NotNull @NotNull org.bukkit.inventory.ItemStack first, @NotNull @NotNull org.bukkit.inventory.ItemStack @NotNull ... rest)
      Simulates adding ItemStacks to this Inventory and then returns if all ItemStacks would fit.
      Parameters:
      first - The first ItemStack to use.
      rest - The rest of the ItemStacks to use.
      Returns:
      If all provided ItemStacks would fit if added.
    • canHold

      public boolean canHold(@NotNull @NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
      Simulates adding ItemStacks to this Inventory and then returns if all ItemStacks would fit.
      Parameters:
      itemStacks - The ItemStack to use.
      Returns:
      If all provided ItemStacks would fit if added.
    • simulateSingleAdd

      public int simulateSingleAdd(@NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Returns the amount of items that wouldn't fit in the inventory when added.
      Note: This method does not add any ItemStacks to the Inventory.
      Parameters:
      itemStack - The ItemStack to use
      Returns:
      How many items wouldn't fit in the inventory when added
    • simulateMultiAdd

      public int[] simulateMultiAdd(@NotNull @NotNull List<@NotNull org.bukkit.inventory.ItemStack> itemStacks)
      Simulates adding multiple ItemStacks to this Inventory and returns the amount of ItemStacks that did not fit.
      Parameters:
      itemStacks - The ItemStack to be used in the simulation
      Returns:
      An array of integers representing the leftover amount for each ItemStack provided. The size of this array is always equal to the amount of ItemStacks provided as method parameters.
    • collectSimilar

      public int collectSimilar(@Nullable @Nullable UpdateReason updateReason, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Finds all ItemStacks similar to the provided ItemStack and removes them from their slot until the amount of the given ItemStack reaches its maximum stack size.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      itemStack - The ItemStack to match against and to use for the base amount.
      Returns:
      The amount of collected items plus the amount of the provided ItemStack. At most the max stack size of the given ItemStack.
    • collectSimilar

      public int collectSimilar(@Nullable @Nullable UpdateReason updateReason, @NotNull @NotNull org.bukkit.inventory.ItemStack template, int baseAmount)
      Finds all ItemStacks similar to the provided ItemStack and removes them from their slot until the maximum stack size of the Material is reached.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      template - The ItemStack to match against.
      baseAmount - The base item amount to assume. For example, with a base amount of 32 and a max stack size of 64, this method will at most collect 32 other items.
      Returns:
      The amount of collected items plus the base amount. At most the max stack size of the template ItemStack.
    • removeIf

      public int removeIf(@Nullable @Nullable UpdateReason updateReason, @NotNull @NotNull Predicate<@NotNull org.bukkit.inventory.ItemStack> predicate)
      Removes all ItemStacks matching the given Predicate.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      predicate - The Predicate to use.
      Returns:
      The amount of items that were removed.
    • removeFirst

      public int removeFirst(@Nullable @Nullable UpdateReason updateReason, int amount, @NotNull @NotNull Predicate<@NotNull org.bukkit.inventory.ItemStack> predicate)
      Removes the first n ItemStacks matching the given Predicate.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      amount - The maximum amount of ItemStacks to remove.
      predicate - The Predicate to use.
      Returns:
      The amount of items that were removed.
    • removeSimilar

      public int removeSimilar(@Nullable @Nullable UpdateReason updateReason, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Removes all ItemStacks that are similar to the specified ItemStack.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      itemStack - The ItemStack to match against.
      Returns:
      The amount of items that were removed.
    • removeFirstSimilar

      public int removeFirstSimilar(@Nullable @Nullable UpdateReason updateReason, int amount, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Removes the first n ItemStacks that are similar to the specified ItemStack.
      Parameters:
      updateReason - The reason used in the ItemPreUpdateEvent and ItemPostUpdateEvent.
      amount - The maximum amount of ItemStacks to remove.
      itemStack - The ItemStack to match against.
      Returns:
      The amount of items that were removed.