Class VirtualInventory

java.lang.Object
xyz.xenondevs.invui.inventory.Inventory
xyz.xenondevs.invui.inventory.VirtualInventory

public class VirtualInventory extends Inventory
A serializable Inventory implementation that is identified by a UUID and backed by a simple ItemStack array.
See Also:
  • Constructor Details

    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable UUID uuid, int size, @Nullable @Nullable org.bukkit.inventory.ItemStack @Nullable [] items, int @Nullable [] maxStackSizes)
      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!
      maxStackSizes - An array of maximum allowed stack sizes for each slot in the VirtualInventory. Can be null for 64.
      Throws:
      IllegalArgumentException - If the given size does not match the length of the items array or the length of the stackSizes array.
      IllegalArgumentException - If the items array contains air ItemStacks.
    • VirtualInventory

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

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

      public VirtualInventory(@Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items, int @Nullable [] maxStackSizes)
      Constructs a new VirtualInventory
      Parameters:
      items - A predefined array of content. Will not get copied!
      maxStackSizes - An array of maximum allowed stack sizes for each slot in the VirtualInventory. Can be null for 64.
    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable UUID uuid, @Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items)
      Constructs a new VirtualInventory
      Parameters:
      uuid - The UUID of this VirtualInventory. Can be null, only used for serialization.
      items - A predefined array of content. Will not get copied!
    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable org.bukkit.inventory.ItemStack @NotNull [] items)
      Constructs a new VirtualInventory
      Parameters:
      items - A predefined array of content. Will not get copied!
    • VirtualInventory

      public VirtualInventory(@Nullable @Nullable UUID uuid, int @NotNull [] maxStackSizes)
      Constructs a new VirtualInventory
      Parameters:
      uuid - The UUID of this VirtualInventory. Can be null, only used for serialization.
      maxStackSizes - An array of maximum allowed stack sizes for each slot in the VirtualInventory.
    • VirtualInventory

      public VirtualInventory(int @NotNull [] maxStackSizes)
      Constructs a new VirtualInventory
      Parameters:
      maxStackSizes - 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.
    • VirtualInventory

      public VirtualInventory(int size)
      Constructs a new VirtualInventory
      Parameters:
      size - The amount of slots this VirtualInventory has.
    • VirtualInventory

      public VirtualInventory(VirtualInventory inventory)
      Creates a copy of the given VirtualInventory.
      Parameters:
      inventory - The VirtualInventory to copy.
  • Method Details

    • deserialize

      public static VirtualInventory deserialize(byte[] bytes)
      Deserializes a VirtualInventory from a byte array.
      Parameters:
      bytes - The byte array to deserialize from.
      Returns:
      The deserialized VirtualInventory.
    • deserialize

      public static VirtualInventory deserialize(InputStream in)
      Deserializes a VirtualInventory from an InputStream.
      Parameters:
      in - The InputStream to deserialize from.
      Returns:
      The deserialized VirtualInventory.
    • serialize

      public byte[] serialize()
      Serializes this VirtualInventory to a byte array.

      This method only serializes the UUID and ItemStacks.

      Returns:
      The serialized data.
    • serialize

      public void serialize(OutputStream out)
      Serializes this VirtualInventory to an OutputStream.

      This method only serializes the UUID and ItemStacks.

      Parameters:
      out - The OutputStream to write serialized data to.
    • setResizeHandlers

      public void setResizeHandlers(@Nullable @Nullable List<@NotNull BiConsumer<@NotNull Integer,@NotNull Integer>> resizeHandlers)
      Sets the handlers that are called every time this VirtualInventory is resized.
      Parameters:
      resizeHandlers - The handlers to set.
    • getResizeHandlers

      @Nullable public @Nullable List<@NotNull BiConsumer<@NotNull Integer,@NotNull Integer>> getResizeHandlers()
      Gets the handlers that are called every time this VirtualInventory is resized.
      Returns:
      The handlers.
    • addResizeHandler

      public void addResizeHandler(@NotNull @NotNull BiConsumer<@NotNull Integer,@NotNull Integer> resizeHandler)
      Adds a handler that is called every time this VirtualInventory is resized.
      Parameters:
      resizeHandler - The handler to add.
    • removeResizeHandler

      public void removeResizeHandler(@NotNull @NotNull BiConsumer<@NotNull Integer,@NotNull Integer> resizeHandler)
      Removes a handler that is called every time this VirtualInventory is resized.
      Parameters:
      resizeHandler - The handler to remove.
    • 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
    • setMaxStackSizes

      public void setMaxStackSizes(int @NotNull [] stackSizes)
      Sets the array of max stack sizes for this Inventory.
      Parameters:
      stackSizes - The array defining the max stack sizes for this Inventory.
    • 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
    • getUuid

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

      public int getSize()
      Description copied from class: Inventory
      Gets the size of this Inventory.
      Specified by:
      getSize in class Inventory
      Returns:
      How many slots this Inventory has.
    • getMaxStackSizes

      public int @NotNull [] getMaxStackSizes()
      Description copied from class: Inventory
      Gets the array of max stack sizes for this Inventory.
      Specified by:
      getMaxStackSizes in class Inventory
      Returns:
      The array defining the max stack sizes for this Inventory
    • getMaxSlotStackSize

      public int getMaxSlotStackSize(int slot)
      Description copied from class: Inventory
      Gets the maximum stack size for a specific slot while ignoring max stack size of the ItemStack on it.
      Specified by:
      getMaxSlotStackSize in class Inventory
      Parameters:
      slot - The slot
      Returns:
      The maximum stack size on that slo
    • getItems

      @Nullable public @Nullable org.bukkit.inventory.ItemStack @NotNull [] getItems()
      Description copied from class: Inventory
      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.

      Specified by:
      getItems in class Inventory
      Returns:
      A deep copy of the ItemStacks this Inventory contains.
    • getUnsafeItems

      @Nullable public @Nullable org.bukkit.inventory.ItemStack @NotNull [] getUnsafeItems()
      Description copied from class: Inventory
      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.

      Specified by:
      getUnsafeItems in class Inventory
      Returns:
      The ItemStacks this Inventory contains.
    • getItem

      @Nullable public @Nullable org.bukkit.inventory.ItemStack getItem(int slot)
      Description copied from class: Inventory
      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.

      Specified by:
      getItem in class Inventory
      Parameters:
      slot - The slot
      Returns:
      The ItemStack on the given slot
    • getUnsafeItem

      public org.bukkit.inventory.ItemStack getUnsafeItem(int slot)
      Description copied from class: Inventory
      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.

      Specified by:
      getUnsafeItem in class Inventory
      Parameters:
      slot - The slot
      Returns:
      The ItemStack on the given slot.
    • setCloneBackingItem

      protected void setCloneBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Description copied from class: Inventory
      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.

      Specified by:
      setCloneBackingItem in class Inventory
      Parameters:
      slot - The slot
      itemStack - The ItemStack to be set
    • setDirectBackingItem

      protected void setDirectBackingItem(int slot, @Nullable @Nullable org.bukkit.inventory.ItemStack itemStack)
      Description copied from class: Inventory
      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.

      Specified by:
      setDirectBackingItem in class Inventory
      Parameters:
      slot - The slot
      itemStack - The ItemStack to be set