Skip to content

PlayerInventory

Represents the item inventory of a specific player.

In vanilla Minecraft, the inventory has 41 slots, indexed from 0 to 40:

  • Slots 0–8: 9 hotbar slots

  • Slots 9–35: 27 main inventory slots

  • Slots 36–39: 4 armor slots (feet, legs, chest, head)

  • Slot 40: offhand slot

Slots are accessed through inventory.slots, which behaves like a Lua array starting at index 0. Each slot returns a copy of the Item in that slot, or nil if empty. To change an item, modify the copy and write it back to the same slot. Assigning nil to a slot clears it.

Example: Create and modify an item in a slot

local player = spell.owner
---@cast player Player

local inv = player.inventory
if inv.slots[20] == nil then
  inv.slots[20] = Item:new("emerald")
end

local item = inv.slots[20]
---@cast item Item
item.count = 3
inv.slots[20] = item

Example: Clear a slot and set offhand

local inv = spell.owner.inventory
inv.slots[0] = nil                       -- clears hotbar slot 0
inv.slots[40] = Item:new("torch")        -- places a torch in the offhand

Fields

selected: number — Read/Write

The index of the currently selected hotbar slot (0–8). The corresponding item is in the player's main hand.

slots: table<integer, Item|nil> — Read/Write

The item slots of this inventory, indexed from 0 to 40. Each slot holds a copy of the Item in that slot, or nil if empty.

Methods

clear()

Clears the entire inventory.

Example

spell.owner.inventory:clear()