Class VirtualInventory

java.lang.Object
de.studiocode.invui.virtualinventory.VirtualInventory

public class VirtualInventory extends Object
  • Constructor Details

    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable UUID uuid, int size, @Nullable @Nullable org.bukkit.inventory.ItemStack[] items, int[] stackSizes)
      Constructs a new VirtualInventory
      Parameters:
      uuid - The UUID of this VirtualInventory. Can be null, only used for serialization.
      size - The amount of slots this VirtualInventory has.
      items - A predefined array of content. Can be null. Will not get copied!
      stackSizes - An array of maximum allowed stack sizes for each slot in the VirtualInventory.
    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable UUID uuid, int size)
      Constructs a new VirtualInventory
      Parameters:
      uuid - The UUID of this VirtualInventory. Can be null, only used for serialization.
      size - The amount of slots this VirtualInventory has.
  • Method Details

    • toByteArray

      public byte[] toByteArray()
    • getWindows

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

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

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

      public void notifyWindows()
      Notifies all Windows displaying this VirtualInventory 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.
    • resize

      public void resize(int size)
      Changes the size of the VirtualInventory. ItemStacks in slots which are no longer valid will be removed from the VirtualInventory. This method does not call an event.
      Parameters:
      size - The new size of the VirtualInventory
    • setItemUpdateHandler

      public void setItemUpdateHandler(Consumer<ItemUpdateEvent> itemUpdateHandler)
      Sets a handler which is called every time something gets updated in the VirtualInventory.
      Parameters:
      itemUpdateHandler - The new item update handler
    • setInventoryUpdatedHandler

      public void setInventoryUpdatedHandler(Consumer<InventoryUpdatedEvent> inventoryUpdatedHandler)
      Sets a handler which is called every time after something has been updated in the VirtualInventory.
      Parameters:
      inventoryUpdatedHandler - The new handler
    • getGuiShiftPriority

      public int getGuiShiftPriority()
      Gets the priority for shift-clicking ItemStacks into a GUI
      Returns:
      The priority for shift-clicking, VirtualInventories with a higher priority get prioritized.
    • setGuiShiftPriority

      public void setGuiShiftPriority(int guiShiftPriority)
      Sets the priority for shift-clicking ItemStacks into a GUI with multiple VirtualInventory. Not serialized with VirtualInventoryManager.serializeInventory(VirtualInventory, OutputStream).
      Parameters:
      guiShiftPriority - The priority for shift-clicking, VirtualInventories with a higher priority get prioritized.
    • getUuid

      @NotNull public @NotNull UUID getUuid()
      Gets the UUID of this VirtualInventory.
      Returns:
      The UUID
    • getSize

      public int getSize()
      Gets the size of this VirtualInventory.
      Returns:
      How many slots this VirtualInventory has.
    • getStackSizes

      public int[] getStackSizes()
      Gets the array of max stack sizes for this VirtualInventory.
      Returns:
      The array defining the max stack sizes for this VirtualInventory
    • getItems

      public org.bukkit.inventory.ItemStack[] getItems()
      Gets a copy of the contents of this VirtualInventory.
      Returns:
      A deep copy of the ItemStacks this VirtualInventory contains.
    • getUnsafeItems

      public org.bukkit.inventory.ItemStack[] getUnsafeItems()
      Gets the same ItemStack[] that backs this VirtualInventory.
      As it is not clone, it should be handled carefully as changes done on that array will not call any Window updates (and create inconsistency between server and client).
    • getItemStack

      public org.bukkit.inventory.ItemStack getItemStack(int slot)
      Gets a clone of the ItemStack on that slot.
      Parameters:
      slot - The slot
      Returns:
      The ItemStack on the given slot
    • getUnsafeItemStack

      public org.bukkit.inventory.ItemStack getUnsafeItemStack(int slot)
      Returns the actual ItemStack on that slot.
      Not a clone, should be handled carefully as changes done on that item will not call any Window updates (and create inconsistency between server and client), in which case a manual call of notifyWindows() is needed.
      Modifying this ItemStack will not call an ItemUpdateEvent.
      Parameters:
      slot - The slot
      Returns:
      The actual ItemStack on that slot
    • 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.
    • getAmount

      public int getAmount(int slot)
      Gets the amount of items on a slot.
      Parameters:
      slot - The slot
      Returns:
      The amount of items on that slot
    • isEmpty

      public boolean isEmpty()
      Checks if the VirtualInventory is empty.
      Returns:
      If there are no ItemStacks in this VirtualInventory
    • 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 limit and Material.getMaxStackSize().
      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.
    • getMaxSlotStackSize

      public int getMaxSlotStackSize(int slot)
      Gets the maximum stack size for a specific slot while ignoring the ItemStack on it and its own maximum stack size.
      Parameters:
      slot - The slot
      Returns:
      The maximum stack size on that slo
    • setMaxStackSizes

      public void setMaxStackSizes(int[] maxStackSizes)
      Sets all the maximum allowed stack sizes
      Parameters:
      maxStackSizes - All max stack sizes
    • setMaxStackSize

      public void setMaxStackSize(int slot, int maxStackSize)
      Sets the maximum allowed stack size on a specific slot.
      Parameters:
      slot - The slot
      maxStackSize - The max stack size
    • callPreUpdateEvent

      public ItemUpdateEvent callPreUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack)
      Creates an ItemUpdateEvent and calls the itemUpdateHandler 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 ItemUpdateEvent after it has been handled by the itemUpdateHandler
    • callAfterUpdateEvent

      public InventoryUpdatedEvent callAfterUpdateEvent(@Nullable @Nullable UpdateReason updateReason, int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack previousItemStack, @Nullable @Nullable org.bukkit.inventory.ItemStack newItemStack)
      Creates an InventoryUpdatedEvent and calls the inventoryUpdatedHandler 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.
      Returns:
      The InventoryUpdatedEvent after it has been handled by the inventoryUpdatedHandler
    • 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
    • setItemStackSilently

      public void setItemStackSilently(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 ItemUpdateEvent 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.
    • forceSetItemStack

      public boolean forceSetItemStack(@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 ItemUpdateEvent.
      slot - The slot
      itemStack - The ItemStack to set.
      Returns:
      If the action was successful
    • setItemStack

      public boolean setItemStack(@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 inside because of the maximum allowed stack size.
      Parameters:
      updateReason - The reason used in the ItemUpdateEvent.
      slot - The slot
      itemStack - The ItemStack to set.
      Returns:
      If the action was successful
    • putItemStack

      public int putItemStack(@Nullable @Nullable UpdateReason updateReason, int slot, @NotNull @NotNull org.bukkit.inventory.ItemStack itemStack)
      Adds an ItemStack on a specific slot.
      Parameters:
      updateReason - The reason used in the ItemUpdateEvent.
      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 ItemUpdateEvent.
      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 ItemUpdateEvent.
      slot - The slot
      amount - The amount to add
      Returns:
      The amount that was actually added.
    • addItem

      public int addItem(@Nullable @Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack)
      Adds an ItemStack to the VirtualInventory. 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 ItemUpdateEvent.
      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 itemStack, @NotNull @NotNull org.bukkit.inventory.ItemStack... itemStacks)
      Simulates adding ItemStacks to this VirtualInventory and returns the amount of ItemStacks that did not fit.
      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 VirtualInventory and returns the amount of ItemStacks that did not fit.
      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 itemStack, @NotNull @NotNull org.bukkit.inventory.ItemStack... itemStacks)
      Simulates adding ItemStacks to this VirtualInventory and then returns if all ItemStacks would fit.
      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 VirtualInventory and then returns if all ItemStacks would fit.
      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 VirtualInventory.
      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 VirtualInventory 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.
    • collectToCursor

      public int collectToCursor(@Nullable @Nullable UpdateReason updateReason, org.bukkit.inventory.ItemStack itemStack)
      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 ItemUpdateEvent.
      itemStack - The ItemStack to find matches to
      Returns:
      The amount of collected items
    • toString

      public String toString()
      Overrides:
      toString in class Object