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

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[20] == nil then
  inv[20] = Item:new("emerald")
end

local item = inv[20]
---@cast item Item
item.count = 3 -- Increases the stack size to 3
inv[20] = item

Example: Clear a slot and set offhand

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

Fields

selectedSlot: number Read/Write

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