Class ReferencingInventory

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

public class ReferencingInventory extends Inventory
A Inventory which is backed by a bukkit Inventory.

Changes in this inventory are applied in the referenced inventory and changes in the bukkit inventory are visible in this inventory.

Changes done using the methods provided by Inventory will cause displaying Windows to be notified, but changes done directly in the bukkit inventory will not. Therefore, if embedded in a Gui, it is necessary to call Inventory.notifyWindows() manually in order for changes to be displayed.

  • Field Details

    • inventory

      @NotNull protected final @NotNull org.bukkit.inventory.Inventory inventory
    • itemsGetter

      @NotNull protected final @NotNull Function<@NotNull org.bukkit.inventory.Inventory,@Nullable org.bukkit.inventory.ItemStack @NotNull []> itemsGetter
    • itemGetter

      @NotNull protected final @NotNull BiFunction<@NotNull org.bukkit.inventory.Inventory,@NotNull Integer,@Nullable org.bukkit.inventory.ItemStack> itemGetter
    • itemSetter

      @NotNull protected final @NotNull TriConsumer<@NotNull org.bukkit.inventory.Inventory,@NotNull Integer,@Nullable org.bukkit.inventory.ItemStack> itemSetter
    • size

      protected final int size
    • maxStackSizes

      protected final int @NotNull [] maxStackSizes
  • Constructor Details

    • ReferencingInventory

      public ReferencingInventory(@NotNull @NotNull org.bukkit.inventory.Inventory inventory, @NotNull @NotNull Function<@NotNull org.bukkit.inventory.Inventory,@Nullable org.bukkit.inventory.ItemStack @NotNull []> itemsGetter, @NotNull @NotNull BiFunction<@NotNull org.bukkit.inventory.Inventory,@NotNull Integer,@Nullable org.bukkit.inventory.ItemStack> itemGetter, @NotNull @NotNull TriConsumer<@NotNull org.bukkit.inventory.Inventory,@NotNull Integer,@Nullable org.bukkit.inventory.ItemStack> itemSetter)
      Constructs a new ReferencingInventory.
      Parameters:
      inventory - The Inventory to reference.
      itemsGetter - A Function which returns a copy of the ItemStacks of the Inventory.
      itemGetter - A BiFunction which returns a copy of the ItemStack on the specified slot.
      itemSetter - A TriConsumer which copies and then sets the ItemStack on the specified slot.
  • Method Details

    • fromStorageContents

      @NotNull public static @NotNull ReferencingInventory fromStorageContents(@NotNull @NotNull org.bukkit.inventory.Inventory inventory)
      Creates a new ReferencingInventory which references only the storage contents of the specified Inventory.
      Parameters:
      inventory - The Inventory to reference.
      Returns:
      The new ReferencingInventory.
    • fromContents

      @NotNull public static @NotNull ReferencingInventory fromContents(@NotNull @NotNull org.bukkit.inventory.Inventory inventory)
      Creates a new ReferencingInventory which references all ItemStacks of the specified Inventory.
      Parameters:
      inventory - The Inventory to reference.
      Returns:
      The new ReferencingInventory.
    • fromReversedPlayerStorageContents

      @NotNull public static @NotNull ReferencingInventory fromReversedPlayerStorageContents(@NotNull @NotNull org.bukkit.inventory.PlayerInventory inventory)
      Creates a new ReferencingInventory with a reversed view of the PlayerInventory's storage contents, where the last hotbar slot is the first slot and the top left slot is the last slot.
      Parameters:
      inventory - The PlayerInventory to reference.
      Returns:
      The new ReferencingInventory.
    • 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

      @Nullable public @Nullable 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