The Inventory API provides Lua scripts with the ability to create and manage inventories, including custom GUI inventories, item manipulation, and inventory operations.
The API is accessed via the global InventoryApi table in Lua scripts.
Creates a new inventory.
Syntax:
local inventory = InventoryApi:createInventory(size, title)
-- or
local inventory = InventoryApi:createInventory(holder, size, title)Parameters:
size(number): Inventory size (must be a multiple of 9 between 9 and 54)title(userdata): A Component object for the inventory title (from AdventureApi)holder(userdata, optional): An InventoryHolder (e.g., a Player) to own the inventory
Returns:
inventory(userdata): The Inventory object, or nil on failure
Example:
local title = AdventureApi:parseMiniMessage("<gold>My Inventory</gold>")
local inv = InventoryApi:createInventory(27, title)Creates an ItemStack.
Syntax:
local itemStack = InventoryApi:createItem(material, [amount], [displayName])Parameters:
material(string): The material name (e.g., "DIAMOND", "STONE")amount(number, optional): The stack size (defaults to 1, clamped to material's max stack size)displayName(userdata, optional): A Component object for the item's display name
Returns:
itemStack(userdata): The ItemStack object, or nil on failure
Example:
local item = InventoryApi:createItem("DIAMOND", 64)
local namedItem = InventoryApi:createItem("DIAMOND_SWORD", 1, AdventureApi:parseMiniMessage("<red>Excalibur</red>"))Fills empty slots in an inventory with a filler material.
Syntax:
InventoryApi:fillBackground(inventory, fillerMaterial)Parameters:
inventory(userdata): The Inventory to fillfillerMaterial(string): The material name for the filler (e.g., "GRAY_STAINED_GLASS_PANE")
Returns:
inventory(userdata): The modified Inventory object
Example:
InventoryApi:fillBackground(inv, "GRAY_STAINED_GLASS_PANE")Sets an item in a specific slot.
Syntax:
InventoryApi:setItem(inventory, slot, itemStack)Parameters:
inventory(userdata): The Inventory to modifyslot(number): The slot index (0-based)itemStack(userdata): The ItemStack to set
Returns:
inventory(userdata): The modified Inventory object
Example:
local item = InventoryApi:createItem("DIAMOND", 1)
InventoryApi:setItem(inv, 0, item)Gets the item in a specific slot.
Syntax:
local itemStack = InventoryApi:getItem(inventory, slot)Parameters:
inventory(userdata): The Inventory to queryslot(number): The slot index (0-based)
Returns:
itemStack(userdata): The ItemStack in the slot, or nil if empty
Example:
local item = InventoryApi:getItem(inv, 0)Adds an item to the inventory.
Syntax:
local fullyAdded = InventoryApi:addItem(inventory, itemStack)Parameters:
inventory(userdata): The Inventory to add toitemStack(userdata): The ItemStack to add
Returns:
fullyAdded(boolean): true if the entire stack was added, false if the inventory is full
Example:
local item = InventoryApi:createItem("DIAMOND", 64)
local success = InventoryApi:addItem(inv, item)Clears a specific slot.
Syntax:
InventoryApi:clearSlot(inventory, slot)Parameters:
inventory(userdata): The Inventory to modifyslot(number): The slot index (0-based)
Example:
InventoryApi:clearSlot(inv, 0)Clears all items from the inventory.
Syntax:
InventoryApi:clear(inventory)Parameters:
inventory(userdata): The Inventory to clear
Example:
InventoryApi:clear(inv)Gets the size of the inventory.
Syntax:
local size = InventoryApi:getSize(inventory)Parameters:
inventory(userdata): The Inventory to query
Returns:
size(number): The number of slots in the inventory
Example:
local size = InventoryApi:getSize(inv)
print("Inventory size: " .. size)Gets the first empty slot in the inventory.
Syntax:
local slot = InventoryApi:firstEmpty(inventory)Parameters:
inventory(userdata): The Inventory to query
Returns:
slot(number): The first empty slot index, or -1 if the inventory is full
Example:
local slot = InventoryApi:firstEmpty(inv)
if slot >= 0 then
InventoryApi:setItem(inv, slot, item)
endChecks if the inventory contains a specific material.
Syntax:
local contains = InventoryApi:contains(inventory, material)Parameters:
inventory(userdata): The Inventory to querymaterial(string): The material name to check for
Returns:
contains(boolean): true if the inventory contains the material
Example:
if InventoryApi:contains(inv, "DIAMOND") then
print("Inventory contains diamonds")
endOpens an inventory for a player.
Syntax:
InventoryApi:open(player, inventory)Parameters:
player(userdata): The Player to open the inventory forinventory(userdata): The Inventory to open
Example:
InventoryApi:open(player, inv)Closes a player's currently open inventory.
Syntax:
InventoryApi:close(player)Parameters:
player(userdata): The Player whose inventory should be closed
Example:
InventoryApi:close(player)- Inventory sizes must be multiples of 9 between 9 and 54 (standard chest sizes)
- Slot indices are 0-based (0 to size-1)
- Material names must be valid Bukkit material names
- Amount values are automatically clamped to the material's maximum stack size
- Display names use Adventure API Components for formatting
- The
fillBackgroundmethod only fills empty slots, preserving existing items - The
addItemmethod returns false if the inventory is full and some items couldn't be added