Class in @minecraft/server
A stack of items, held by the script. Changes to it change only it, until it’s given to a player or put in a slot; lore, enchantments and the rest travel with it, and a stack read back from a player has everything it was given.
import { ItemStack } from '@minecraft/server';Example
Section titled “Example”import { ItemStack, world } from '@minecraft/server';
const sword = new ItemStack('minecraft:diamond_sword', 1);sword.nameTag = 'Basalt Blade';sword.setLore(['Forged in the nether']);
// Hand one to every player.for (const player of world.getAllPlayers()) { player.getComponent('minecraft:inventory')?.container?.addItem(sword);}import { ItemStack, world } from '@minecraft/server';
const sword = new ItemStack('minecraft:diamond_sword', 1);sword.nameTag = 'Basalt Blade';sword.setLore(['Forged in the nether']);
// Hand one to every player.for (const player of world.getAllPlayers()) { player.getComponent('minecraft:inventory')?.container?.addItem(sword);}Constructor
Section titled “Constructor”constructor(itemType: string | { id: string }, amount?: number);itemTypestring | { id: string }The item’s id, asminecraft:diamond(ordiamond).amountoptionalnumberFrom 1 to 255, kept to the item’smaxAmount.
- TypeError for an item that doesn’t exist; RangeError for an amount out of range.
Properties
Section titled “Properties”typeId
Section titled “typeId”read-only
readonly typeId: string;The item, as minecraft:diamond_sword.
read-only
readonly type: { readonly id: string };The item’s type, whose id is typeId.
amount
Section titled “amount”amount: number;How many, from 1 to 255, kept to maxAmount.
- RangeError outside 1 to 255.
nameTag
Section titled “nameTag”optional
nameTag?: string;The item’s custom name, or undefined for none.
maxAmount
Section titled “maxAmount”read-only
readonly maxAmount: number;How many of the item stack in one slot.
isStackable
Section titled “isStackable”read-only
readonly isStackable: boolean;Whether maxAmount is above 1.
localizationKey
Section titled “localizationKey”read-only
readonly localizationKey: string;The item’s name key in the language files, as item.diamond_sword.name or tile.stone.name.
weight
Section titled “weight”read-only
readonly weight: number;What the stack weighs in a bundle: 64 over maxAmount for each item.
keepOnDeath
Section titled “keepOnDeath”keepOnDeath: boolean;Whether the item stays with a player who dies.
lockMode
Section titled “lockMode”lockMode: ItemLockMode;How the item is held in place in an inventory.
Methods
Section titled “Methods”getLore
Section titled “getLore”getLore(): string[];The lines shown under the item’s name.
string[]getRawLore
Section titled “getRawLore”getRawLore(): RawMessage[];The same as raw messages.
RawMessage[]setLore
Section titled “setLore”setLore(loreList?: (RawMessage | string)[]): void;Sets the lines shown under the item’s name; none takes them away.
loreListoptional(RawMessage | string)[]
getCanDestroy
Section titled “getCanDestroy”getCanDestroy(): string[];The blocks an adventure-mode player may break with the item.
string[]setCanDestroy
Section titled “setCanDestroy”setCanDestroy(blockIdentifiers?: string[]): void;blockIdentifiersoptionalstring[]
- Error if one isn’t a block.
getCanPlaceOn
Section titled “getCanPlaceOn”getCanPlaceOn(): string[];The blocks an adventure-mode player may place the item on.
string[]setCanPlaceOn
Section titled “setCanPlaceOn”setCanPlaceOn(blockIdentifiers?: string[]): void;blockIdentifiersoptionalstring[]
- Error if one isn’t a block.
getTags
Section titled “getTags”getTags(): string[];The item type’s tags, as minecraft:is_sword.
string[]hasTag
Section titled “hasTag”hasTag(tag: string): boolean;Whether the item has this item tag, as minecraft:is_sword.
tagstring
booleanmatches
Section titled “matches”matches(itemName: string, states?: Record<string, boolean | number | string>): boolean;Whether the stack is of this item; states aren’t compared.
itemNamestringstatesoptionalRecord<string, boolean | number | string>
booleanisStackableWith
Section titled “isStackableWith”isStackableWith(itemStack: ItemStack): boolean;Whether the two could share a slot: the same item, name, lore, enchantments and so on.
itemStackItemStack
booleangetComponent
Section titled “getComponent”getComponent<T extends keyof ItemComponentTypeMap>(componentId: T): ItemComponentTypeMap[T] | undefined;getComponent(componentId: string): ItemComponent | undefined;minecraft:durability for items that wear, minecraft:enchantable for those that take
enchantments, minecraft:cooldown for those with a cooldown, minecraft:food for food,
minecraft:dyeable for leather, minecraft:potion for potions and minecraft:inventory
for shulker boxes.
getComponents
Section titled “getComponents”getComponents(): ItemComponent[];Every component the item has.
ItemComponent[]hasComponent
Section titled “hasComponent”hasComponent(componentId: string): boolean;Whether the item has the component.
componentIdstring
booleangetDynamicProperty
Section titled “getDynamicProperty”getDynamicProperty(identifier: string): boolean | number | string | Vector3 | undefined;One of this plugin’s dynamic properties on the stack.
identifierstring
boolean | number | string | Vector3 | undefinedgetDynamicPropertyIds
Section titled “getDynamicPropertyIds”getDynamicPropertyIds(): string[];The identifiers of every property this plugin set on the stack.
string[]getDynamicPropertyTotalByteCount
Section titled “getDynamicPropertyTotalByteCount”getDynamicPropertyTotalByteCount(): number;The bytes this plugin’s properties on the stack take.
numbersetDynamicProperty
Section titled “setDynamicProperty”setDynamicProperty(identifier: string, value?: boolean | number | string | Vector3): void;Sets one of this plugin’s dynamic properties, or with undefined takes it away. They’re
kept in the item’s data, so they go wherever it goes.
identifierstringvalueoptionalboolean | number | string | Vector3
- Error for an item that stacks.
setDynamicProperties
Section titled “setDynamicProperties”setDynamicProperties(values: Record<string, boolean | number | string | Vector3>): void;Sets several of this plugin’s properties on the stack at once; undefined values take them away.
valuesRecord<string, boolean | number | string | Vector3>
clearDynamicProperties
Section titled “clearDynamicProperties”clearDynamicProperties(): void;Takes away every property this plugin set on the stack.
clone(): ItemStack;A copy of the stack, everything on it included.
ItemStackUsed by
Section titled “Used by”BlockPermutation.getItemStack, Block.getItemStack, Container.getItem, Container.setItem, Container.addItem, Container.contains, Container.find, Container.findLast, Container.transferItem, EntityItemComponent.itemStack, EntityEquippableComponent.getEquipment, EntityEquippableComponent.setEquipment, ContainerSlot.getItem, ContainerSlot.setItem, ContainerSlot.isStackableWith, Entity.addItem, ItemEntity.itemStack, Player.eatItem, Dimension.spawnItem, ItemSpawnAfterEvent.itemStack, PlayerInteractWithBlockAfterEvent.itemStack, PlayerInteractWithEntityAfterEvent.itemStack, ItemUseAfterEvent.itemStack, PlayerPickupItemAfterEvent.itemStack and 35 more