Experimental

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

Report a problem

types.ItemStack

A stack of items. Stacks are values: with returns a changed copy and two equal stacks are ==.

Fields

itemStack.count: number
#
read only

How many items the stack holds.

itemStack.damage: number
#
read only

How worn the stack is: 0 when new, and always 0 for an item without durability.

itemStack.data: any
#
read only

The data table set on the stack, or nil when it has none.

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

-- Stack data travels with the item: read it back when the item is used.
local wand = items.type("wand")

wand.used:connect(function(event)
	local charges = if event.item.data then event.item.data.charges else 0
	if charges <= 0 then
		event.player:send("<gray>The wand is out of charges.")
		return
	end
	-- Put back a copy with one charge fewer.
	event.player.inventory:setHeld(event.item:with({ data = { charges = charges - 1 } }), event.hand)
	event.player:send(`<light_purple>Zap! {charges - 1} charges left.`)
end)
itemStack.glint: boolean?
#
read only

Whether the stack forces the enchantment shimmer on (true) or off (false), or nil when it shows the item's own.

itemStack.id: string
#
read only

The item's id, such as "minecraft:diamond".

itemStack.lore: { types.MiniMessage }?
#
read only

The lore lines set on the stack, or nil when it has none.

itemStack.maxCount: number
#
read only

How many of the item one slot holds.

itemStack.maxDamage: number?
#
read only

The damage at which the item breaks, or nil when it has no durability.

itemStack.name: types.MiniMessage?
#
read only

The name set on the stack, or nil when it shows the item's own.

itemStack.type: types.ItemType
#
read only

The stack's item type, whose signals fire for it.

Methods

itemStack:is(type: types.ItemType) -> boolean
#

Returns whether the stack is of the item type, or of a type in the item tag.

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

-- An item tag covers many types at once; `is` checks a stack against a type or a tag.
local logs = items.tag("minecraft:logs")
local apple = items.type("minecraft:apple")

players.joined:connect(function(player)
	local held = player.inventory:held()
	if held and held:is(logs) then
		player:send("<green>That's wood.")
	elseif held and held:is(apple) then
		player:send(`<red>An apple; a full stack is {held.maxCount}.`)
	end
end)
itemStack:matches(other: types.ItemStack) -> boolean
#

Returns whether the stacks are the same item with the same name, lore and data, whatever their counts.

itemStack:with(changes: types.ItemChanges) -> types.ItemStack
#

Returns a copy of the stack with the changes.

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

-- Stacks are values: `with` returns a changed copy and leaves the original alone.
local medal = items.create("minecraft:gold_nugget", { name = "<gold>Medal" })

players.joined:connect(function(player)
	local mine = medal:with({ lore = { `<gray>Awarded to {player.name}` } })
	player.inventory:setHeld(mine)
end)
itemStack:without(...: types.ItemField) -> types.ItemStack
#

Returns a copy of the stack with the fields put back to the item's own, such as item:without("name", "lore").

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

-- A worn, renamed sword.
local relic = items.create("minecraft:iron_sword", { name = "<gold>Relic", lore = { "<gray>Found in the ruins" }, damage = 200 })

players.joined:connect(function(player)
	-- `with` can't clear a field, since nil keeps it; `without` puts fields back to the item's own.
	local repaired = relic:without("damage")
	local plain = relic:without("name", "lore", "damage")
	player.inventory:add(repaired)
	player.inventory:add(plain)
end)