Class CustomInventory
- All Implemented Interfaces:
InventoryHolder
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
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptionintAttempts to add the provided item stack to the inventory.voidclose()Closes this inventory for all current viewers, if any.voiddropContentAt(Location loc) Drops all non-air contents of this inventory into the world at the given location and clears the slots.@Nullable InventoryReturns the underlying Bukkit Inventory instance for integration points that require direct access.Returns a copy of the current contents of the inventory.@NotNull InventoryInventoryHolder contract.getItem(int i) Gets the item currently stored at the given slot index.intGets the maximum stack size used by the backing Bukkit inventory.getName()Current display name (title) of the inventory GUI.getOwner()Returns the InventoryHolder for the backing inventory.intgetSize()Logical size of the inventory in slots.Returns the current viewers of this inventory.booleanisEmpty()Checks whether the inventory contains no items (all slots null or air).voidload(CompoundBinaryTag nbtTagCompound) Restores inventory contents from the provided NBT compound.voidonClose(HumanEntity who) Should be called when a player closes this inventory to remove them from the tracked viewer list.voidonOpen(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.voidOpens 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.removeItemNoUpdate(int i) Removes and returns the item currently in the specified slot without triggering any additional logic.save(CompoundBinaryTag compound) Serializes the inventory contents into an Adventure NBT compound.voidSets the item for the specified slot.voidsetMaxStackSize(int i) Sets the maximum stack size applied to the backing Bukkit inventory and updates the cached value used before initialization.voidSets the inventory GUI title from a String.voidSets the inventory GUI title from a Component.voidsetSize(int newSize) Sets the inventory size in slots.
-
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
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
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
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
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
Sets the item for the specified slot.- Parameters:
i- zero-based slot indexitemStack- the item to set (can be null to clear)
-
addItem
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
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
InventoryHolder contract. Returns the held Bukkit Inventory. Lazily creates a default-sized inventory if one has not been set yet.- Specified by:
getInventoryin interfaceInventoryHolder
-
dropContentAt
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
Removes up to the requested amount from the specified slot and returns the removed items as a separate stack.- Parameters:
slot- zero-based slot indexsubtract- 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
Returns a copy of the current contents of the inventory.- Returns:
- a mutable list containing the current slot contents (may include nulls)
-
save
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
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
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
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
Opens this inventory GUI for the specified player.- Parameters:
player- the player who should see the inventory
-
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
Returns the InventoryHolder for the backing inventory. Alwaysthis.- 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
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
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
-