Class CustomInventory

java.lang.Object
de.Keyle.MyPet.api.util.inventory.CustomInventory
All Implemented Interfaces:
InventoryHolder

public class CustomInventory extends Object implements InventoryHolder
API-only inventory implementation

This class provides methods for sizing, naming, persisting (to K-NBT), and opening/closing the GUI for players. It avoids direct CraftBukkit/NMS usage and should work across modern Bukkit/Paper versions.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new CustomInventory instance and ensures the shared shutdown listener is registered.
    CustomInventory(int size, Component name)
    Convenience constructor to initialize size and name.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    addItem(ItemStack itemAdd)
    Attempts to add the provided item stack to the inventory.
    void
    Closes this inventory for all current viewers, if any.
    void
    Drops all non-air contents of this inventory into the world at the given location and clears the slots.
    @Nullable Inventory
    Returns the underlying Bukkit Inventory instance for integration points that require direct access.
    Returns a copy of the current contents of the inventory.
    @NotNull Inventory
    InventoryHolder contract.
    getItem(int i)
    Gets the item currently stored at the given slot index.
    int
    Gets the maximum stack size used by the backing Bukkit inventory.
    Current display name (title) of the inventory GUI.
    Returns the InventoryHolder for the backing inventory.
    int
    Logical size of the inventory in slots.
    Returns the current viewers of this inventory.
    boolean
    Checks whether the inventory contains no items (all slots null or air).
    void
    load(CompoundBinaryTag nbtTagCompound)
    Restores inventory contents from the provided NBT compound.
    void
    Should be called when a player closes this inventory to remove them from the tracked viewer list.
    void
    Should be called when a player opens this inventory to keep the viewer list in sync while the underlying inventory may be lazily created.
    void
    open(Player player)
    Opens this inventory GUI for the specified player.
    removeItem(int slot, int subtract)
    Removes up to the requested amount from the specified slot and returns the removed items as a separate stack.
    Removes and returns the item currently in the specified slot without triggering any additional logic.
    Serializes the inventory contents into an Adventure NBT compound.
    void
    setItem(int i, ItemStack itemStack)
    Sets the item for the specified slot.
    void
    Sets the maximum stack size applied to the backing Bukkit inventory and updates the cached value used before initialization.
    void
    Sets the inventory GUI title from a String.
    void
    Sets the inventory GUI title from a Component.
    void
    setSize(int newSize)
    Sets the inventory size in slots.

    Methods inherited from class java.lang.Object

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

    • CustomInventory

      public CustomInventory()
      Creates a new CustomInventory instance and ensures the shared shutdown listener is registered. The instance is also tracked for lifecycle management so that all open inventories can be closed on plugin disable.
    • CustomInventory

      public CustomInventory(int size, Component name)
      Convenience constructor to initialize size and name.
      Parameters:
      size - initial size in slots (will be normalized to a multiple of 9 and clamped [9,54])
      name - initial GUI title (will be truncated to 64 chars if longer)
  • Method Details

    • setSize

      public void setSize(int newSize)
      Sets the inventory size in slots.

      The value is clamped to a multiple of 9 between [9, 54]

      Parameters:
      newSize - desired slot count
    • setName

      public void setName(String name)
      Sets the inventory GUI title from a String.

      Renaming reconstructs the underlying Bukkit inventory to apply the new title while preserving contents and max stack size.

      Parameters:
      name - new title (null is ignored)
    • setName

      public void setName(Component name)
      Sets the inventory GUI title from a Component.

      Renaming reconstructs the underlying Bukkit inventory to apply the new title while preserving contents and max stack size.

      Parameters:
      name - new title Component (null is ignored)
    • getItem

      public ItemStack getItem(int i)
      Gets the item currently stored at the given slot index.
      Parameters:
      i - zero-based slot index
      Returns:
      the item at the slot or null if out of bounds, uninitialized, or empty
    • setItem

      public void setItem(int i, ItemStack itemStack)
      Sets the item for the specified slot.
      Parameters:
      i - zero-based slot index
      itemStack - the item to set (can be null to clear)
    • addItem

      public int addItem(ItemStack itemAdd)
      Attempts to add the provided item stack to the inventory.

      Delegates to Bukkit's Inventory#addItem and returns the number of items that could not be inserted due to lack of space.

      Parameters:
      itemAdd - the stack to add (null or air results in 0 leftover)
      Returns:
      the amount that could not be added (0 if fully inserted)
    • getBukkitInventory

      @Nullable public @Nullable Inventory getBukkitInventory()
      Returns the underlying Bukkit Inventory instance for integration points that require direct access.
      Returns:
      the backing Bukkit inventory, or null if not yet initialized
    • getInventory

      @NotNull public @NotNull Inventory getInventory()
      InventoryHolder contract. Returns the held Bukkit Inventory. Lazily creates a default-sized inventory if one has not been set yet.
      Specified by:
      getInventory in interface InventoryHolder
    • dropContentAt

      public void dropContentAt(Location loc)
      Drops all non-air contents of this inventory into the world at the given location and clears the slots.
      Parameters:
      loc - the world location where items should be spawned (ignored if null or world missing)
    • removeItem

      public ItemStack removeItem(int slot, int subtract)
      Removes up to the requested amount from the specified slot and returns the removed items as a separate stack.
      Parameters:
      slot - zero-based slot index
      subtract - the maximum number of items to take from the slot
      Returns:
      a stack representing the removed items, or null if slot was empty/out of bounds
    • getContents

      public List<ItemStack> getContents()
      Returns a copy of the current contents of the inventory.
      Returns:
      a mutable list containing the current slot contents (may include nulls)
    • save

      public CompoundBinaryTag save(CompoundBinaryTag compound)
      Serializes the inventory contents into an Adventure NBT compound.

      Each non-empty slot is stored as a Base64-encoded ItemStack using Paper's native serializeAsBytes() method under the key "PaperItem".

      Parameters:
      compound - destination compound to merge into (must not be null)
      Returns:
      a new compound with the "Items" list added
    • load

      public void load(CompoundBinaryTag nbtTagCompound)
      Restores inventory contents from the provided NBT compound.

      Supports the current "PaperItem" format (Paper's serializeAsBytes()). Old formats ("BukkitItem" or K-NBT/NMS) are incompatible with MyPet v4 and will be skipped with a warning logged.

      Parameters:
      nbtTagCompound - source compound containing an "Items" list
    • onOpen

      public void onOpen(HumanEntity who)
      Should be called when a player opens this inventory to keep the viewer list in sync while the underlying inventory may be lazily created.
      Parameters:
      who - the human entity opening the inventory
    • onClose

      public void onClose(HumanEntity who)
      Should be called when a player closes this inventory to remove them from the tracked viewer list.
      Parameters:
      who - the human entity closing the inventory
    • close

      public void close()
      Closes this inventory for all current viewers, if any.
    • open

      public void open(Player player)
      Opens this inventory GUI for the specified player.
      Parameters:
      player - the player who should see the inventory
    • getViewers

      public List<HumanEntity> getViewers()
      Returns the current viewers of this inventory. If the backing inventory has not been created yet, falls back to the internally tracked openers.
      Returns:
      a list of human entities currently viewing this inventory
    • getOwner

      public InventoryHolder getOwner()
      Returns the InventoryHolder for the backing inventory. Always this.
      Returns:
      this instance
    • getMaxStackSize

      public int getMaxStackSize()
      Gets the maximum stack size used by the backing Bukkit inventory.
      Returns:
      the max stack size (defaults to 64 before initialization)
    • setMaxStackSize

      public void setMaxStackSize(int i)
      Sets the maximum stack size applied to the backing Bukkit inventory and updates the cached value used before initialization.
      Parameters:
      i - the new max stack size
    • removeItemNoUpdate

      public ItemStack removeItemNoUpdate(int i)
      Removes and returns the item currently in the specified slot without triggering any additional logic.
      Parameters:
      i - zero-based slot index
      Returns:
      the previous item in the slot or null if empty/out of bounds
    • isEmpty

      public boolean isEmpty()
      Checks whether the inventory contains no items (all slots null or air).
      Returns:
      true if inventory has no meaningful items; false otherwise
    • getName

      public Component getName()
      Current display name (title) of the inventory GUI. Defaults to "Inventory".
      Returns:
      the inventory's title
    • getSize

      public int getSize()
      Logical size of the inventory in slots. Always a multiple of 9 in the inclusive range [9, 54]. Defaults to null.
      Returns:
      the size in slots