API reference / @jsprismarine/items / ItemStack
Class: ItemStack
Defined in: packages/items/src/ItemStack.ts:76
One stack of items: a type reference, a count, and a component patch over the type's default components.
This is Java's model. The type holds the defaults, the stack holds only what differs, and reading a component means reading the patch first and the defaults second. A patch entry can remove a default outright, which is what makes "this sword is not a weapon" expressible without a second item type.
The type reference is a Holder, so a content reload points every live stack at the new type without walking the world, and a stack whose identifier the reload dropped reports that instead of reading a stale type.
Extends
Constructors
Constructor
new ItemStack(
holder,count?,patch?):ItemStack
Defined in: packages/items/src/ItemStack.ts:82
Parameters
holder
Holder<Type<ItemHooks, ItemFlag>>
A holder from the registry of resolved item types.
count?
number = 1
How many items, defaulting to one.
patch?
ComponentPatch = ...
The component patch, copied so later edits do not alias it.
Returns
ItemStack
Overrides
Properties
holder
Defined in: packages/content/src/instances.ts:23
A holder from the registry of resolved types.
Inherited from
state
state:
ItemStackState
Defined in: packages/content/src/instances.ts:24
Whatever the domain keeps per instance, such as an item count.
Inherited from
Accessors
count
Get Signature
get count():
number
Defined in: packages/items/src/ItemStack.ts:116
How many items the stack holds.
Returns
number
Set Signature
set count(
value):void
Defined in: packages/items/src/ItemStack.ts:120
Parameters
value
number
Returns
void
empty
Get Signature
get empty():
boolean
Defined in: packages/items/src/ItemStack.ts:130
Whether the stack holds nothing.
Returns
boolean
identifier
Get Signature
get identifier():
string
Defined in: packages/items/src/ItemStack.ts:87
The Bedrock item identifier this stack is of.
Returns
string
patch
Get Signature
get patch():
ComponentPatch
Defined in: packages/items/src/ItemStack.ts:125
The component patch, as a read-only view of the stack's own map.
Returns
type
Get Signature
get type():
Type<H,F>
Defined in: packages/content/src/instances.ts:32
The type this instance is an instance of.
Throws
When the current content has no type under the holder's key.
Returns
Type<H, F>
Inherited from
Methods
components()
components():
ReadonlyMap<string,unknown>
Defined in: packages/items/src/ItemStack.ts:193
Every component the stack has after the patch is applied, keyed by name.
Returns
ReadonlyMap<string, unknown>
copy()
copy(
count?):ItemStack
Defined in: packages/items/src/ItemStack.ts:204
A copy with the same type, count and patch, sharing no mutable state.
Parameters
count?
number = ...
Returns
ItemStack
dispatch()
dispatch<
K>(hook, ...args):void
Defined in: packages/content/src/instances.ts:41
Calls every handler the type binds for the hook, in dispatch order.
Type Parameters
K
K extends "onUse" | "onUseOn" | "onMineBlock" | "onHitEntity" | "onBeforeDurabilityDamage" | "onCompleteUse" | "onConsume"
Parameters
hook
K
args
...Parameters<ItemHooks[K]>
Returns
void
Throws
When the current content has no type under the holder's key.
Inherited from
dispatchFirst()
dispatchFirst<
K>(hook, ...args):ReturnType<ItemHooks[K]> |undefined
Defined in: packages/content/src/instances.ts:51
Calls handlers in dispatch order until one returns a value other than undefined.
Type Parameters
K
K extends "onUse" | "onUseOn" | "onMineBlock" | "onHitEntity" | "onBeforeDurabilityDamage" | "onCompleteUse" | "onConsume"
Parameters
hook
K
args
...Parameters<ItemHooks[K]>
Returns
ReturnType<ItemHooks[K]> | undefined
That handler's result, or undefined when every handler declines.
Throws
When the current content has no type under the holder's key.
Inherited from
get()
get(
name):unknown
Defined in: packages/items/src/ItemStack.ts:140
The value of one component, patch first and the type's default second.
Parameters
name
string
Returns
unknown
The value, or undefined when neither the patch nor the type has the component, and also when the patch removes it.
getJavaTags()
getJavaTags(): readonly
string[]
Defined in: packages/items/src/ItemStack.ts:104
The Java datapack tags attached to this stack's item type.
Returns
readonly string[]
getTags()
getTags(): readonly
string[]
Defined in: packages/items/src/ItemStack.ts:92
The Bedrock tags attached to this stack's item type.
Returns
readonly string[]
has()
has(
name):boolean
Defined in: packages/items/src/ItemStack.ts:164
Whether the stack has the component after the patch is applied.
Parameters
name
string
Returns
boolean
hasJavaTag()
hasJavaTag(
tag):boolean
Defined in: packages/items/src/ItemStack.ts:111
Whether a Java datapack tag includes this stack's item type.
Parameters
tag
string
Returns
boolean
hasTag()
hasTag(
tag):boolean
Defined in: packages/items/src/ItemStack.ts:99
Whether this stack's item type carries one of its Bedrock tags.
Parameters
tag
string
Returns
boolean
params()
params<
P>(factory):P|undefined
Defined in: packages/items/src/ItemStack.ts:155
The parsed parameters of one component, narrowed against the factory whose schema produced them.
A patched value went through the same factory's schema when it was set through ItemStack.set, so the narrowing holds for both layers.
Type Parameters
P
P
Parameters
factory
ComponentFactory<ItemHooks, P>
Returns
P | undefined
The parameters, or undefined when the stack has no such component.
remove()
remove(
name):this
Defined in: packages/items/src/ItemStack.ts:181
Removes one of the type's default components from this stack, which is Java's !component marker. The entry stays in the patch, because removing a default is a difference from the default.
Parameters
name
string
Returns
this
reset()
reset(
name):this
Defined in: packages/items/src/ItemStack.ts:187
Drops a patch entry so the component falls back to the type's default.
Parameters
name
string
Returns
this
set()
set(
name,value):this
Defined in: packages/items/src/ItemStack.ts:171
Sets one component on this stack, overriding the type's default.
Parameters
name
string
value
unknown
Returns
this