Experimental

An early preview: docs, the API and the dashboard may change or break.

Report a problem

types.PlayerInventory

A player's inventory. Calls raise once the player has left.

Signals

playerInventory.changed: types.Signal<>
#
read only

Fires after a slot of the inventory changed or the player picked another hotbar slot, once a tick at most.

local players = require("@ember/players")
local ui = require("@ember/ui")

-- `changed` fires after any slot changes or the player scrolls to another hotbar slot, at most
-- once a tick. Here it keeps the held item's name above the hotbar.
players.joined:connect(function(player)
	local inventory = player.inventory
	inventory.changed:connect(function()
		local held = inventory:held()
		if held then
			ui.actionBar(player, held.name or held.id, 2)
		end
	end)
end)

Fields

playerInventory.heldSlot: number
#
read only

The selected hotbar slot, 1 to 9; it follows the player's scrolling.

Methods

playerInventory:add(id: string, count: number?) -> numberplayerInventory:add(item: types.ItemStack, count: number?) -> number
#

Adds the stack, or count of it, wherever it fits; returns how many fit.

Range: count: 1..2304

local players = require("@ember/players")
local items = require("@ember/items")

players.joined:connect(function(player)
	-- By id, with a count.
	player.inventory:add("minecraft:arrow", 64)

	-- Or a stack you made. `add` returns how many fit.
	local fit = player.inventory:add(items.create("minecraft:snowball", { count = 16, name = "<white>Snowball" }))
	if fit < 16 then
		player:send("<red>Your inventory is full!")
	end
end)
playerInventory:clear()
#

Empties every slot, armour and offhand included.

playerInventory:count(id: string) -> number
#

Returns how many of the item the player carries.

local world = require("@ember/world")

-- A shop counter: right-click the "shop" region's buttons to trade 10 emeralds for a sword.
world.regions.single("shop").interacting:intercept(function(event)
	local inventory = event.player.inventory
	if inventory:count("minecraft:emerald") < 10 then
		event.player:send("<red>You need 10 emeralds.")
		return false
	end
	inventory:remove("minecraft:emerald", 10)
	inventory:add("minecraft:diamond_sword")
	return true
end)
playerInventory:get(row: types.SlotRow, index: number) -> types.ItemStack?playerInventory:get(slot: types.SlotName) -> types.ItemStack?
#

Returns the stack in a row's slot, 1 being the leftmost, or nil when it's empty.

Range: index: 1..27

playerInventory:held(hand: types.Hand?) -> types.ItemStack?
#

Returns the stack in a hand, the main hand if nil, or nil when it's empty.

playerInventory:remove(id: string, count: number?) -> number
#

Removes up to count of the item, all of it if nil; returns how many were removed.

Range: count: 1..2304

playerInventory:restore(slots: { [string]: types.ItemStack })
#

Puts every slot back from a table shaped like snapshot's; a slot it has no key for is emptied. Raises for an unknown slot name.

playerInventory:set(row: types.SlotRow, index: number, item: types.ItemStack?)playerInventory:set(slot: types.SlotName, item: types.ItemStack?)
#

Puts a stack in a row's slot, replacing what was there; nil empties it.

Range: index: 1..27

local items = require("@ember/items")
local players = require("@ember/players")

-- A fixed kit: every item in a chosen slot, armour included. Rows are numbered from 1 at the left;
-- armour and the offhand go by name.
players.joined:connect(function(player)
	local inventory = player.inventory
	inventory:clear()
	inventory:set("hotbar", 1, items.create("minecraft:iron_sword"))
	inventory:set("hotbar", 2, items.create("minecraft:bow"))
	inventory:set("hotbar", 9, items.create("minecraft:cooked_beef", { count = 8 }))
	inventory:set("main", 1, items.create("minecraft:arrow", { count = 32 }))
	inventory:set("head", items.create("minecraft:iron_helmet"))
	inventory:set("chest", items.create("minecraft:iron_chestplate"))
	inventory:set("offhand", items.create("minecraft:shield"))
	inventory:setHeldSlot(1)
end)
playerInventory:setHeld(item: types.ItemStack?, hand: types.Hand?)
#

Puts a stack in a hand, the main hand if nil, replacing what was there; nil empties it.

playerInventory:setHeldSlot(index: number)
#

Selects a hotbar slot, 1 being the leftmost.

Range: index: 1..9

playerInventory:snapshot() -> { [string]: types.ItemStack }
#

Returns every filled slot keyed by name: "hotbar.1" to "hotbar.9", "main.1" to "main.27", then "head", "chest", "legs", "feet" and "offhand". Empty slots have no key.

local items = require("@ember/items")
local types = require("@ember/types")
local world = require("@ember/world")

-- What each player carried before the arena, keyed by slot name like "hotbar.1" or "head".
local before: { [types.Player]: { [string]: types.ItemStack } } = {}

-- Swap their inventory for the arena kit on the way in.
world.regions.single("arena").entered:connect(function(player)
	before[player] = player.inventory:snapshot()
	player.inventory:restore({
		["hotbar.1"] = items.create("minecraft:stone_sword"),
		["hotbar.2"] = items.create("minecraft:bow"),
		["main.1"] = items.create("minecraft:arrow", { count = 32 }),
	})
end)

-- And give it back on the way out.
world.regions.single("arena").left:connect(function(player)
	local saved = before[player]
	if saved then
		player.inventory:restore(saved)
		before[player] = nil
	end
end)