Class ItemStack

Defines a collection of items.

Hierarchy

  • ItemStack

Constructors

  • Parameters

    • itemType: string | ItemType

      Type of item to create. See the @minecraft/vanilla-data.MinecraftItemTypes enumeration for a list of standard item types in Minecraft experiences.

    • Optional amount: number

      Number of items to place in the stack, between 1-255. The provided value will be clamped to the item's maximum stack size. Note that certain items can only have one item in the stack.

    Returns ItemStack

    Remarks

    Creates a new instance of a stack of items for use in the world.

    Throws

    Throws if itemType is invalid, or if amount is outside the range of 1-255.

Properties

amount: number

Remarks

Number of the items in the stack. Valid values range between 1-255. The provided value will be clamped to the item's maximum stack size.

Throws

Throws if the value is outside the range of 1-255.

isStackable: boolean

Remarks

Returns whether the item is stackable. An item is considered stackable if the item's maximum stack size is greater than 1 and the item does not contain any custom data or properties.

keepOnDeath: boolean

Remarks

Gets or sets whether the item is kept on death.

lockMode: ItemLockMode

Remarks

Gets or sets the item's lock mode. The default value is ItemLockMode.none.

maxAmount: number

Remarks

The maximum stack size. This value varies depending on the type of item. For example, torches have a maximum stack size of 64, while eggs have a maximum stack size of 16.

nameTag?: string

Remarks

Given name of this stack of items. The name tag is displayed when hovering over the item. Setting the name tag to an empty string or undefined will remove the name tag.

Throws

Throws if the length exceeds 255 characters.

type: ItemType

Remarks

The type of the item.

typeId: string

Remarks

Identifier of the type of items for the stack. If a namespace is not specified, 'minecraft:' is assumed. Examples include 'wheat' or 'apple'.

Methods

  • Parameters

    • componentId: string

      The identifier of the component (e.g., 'minecraft:food') to retrieve. If no namespace prefix is specified, 'minecraft:' is assumed. If the component is not present on the item stack, undefined is returned.

    Returns undefined | ItemComponent

    Remarks

    Gets a component (that represents additional capabilities) for an item stack.

    Example

    durability.ts

    // Get the maximum durability of a custom sword item
    const itemStack = new ItemStack("custom:sword");
    const durability = itemStack.getComponent("minecraft:durability") as ItemDurabilityComponent;
    const maxDurability = durability.maxDurability;
  • Returns ItemComponent[]

    Remarks

    Returns all components that are both present on this item stack and supported by the API.

  • Parameters

    • componentId: string

      The identifier of the component (e.g., 'minecraft:food') to retrieve. If no namespace prefix is specified, 'minecraft:' is assumed.

    Returns boolean

    Remarks

    Returns true if the specified component is present on this item stack.

  • Parameters

    Returns boolean

    Remarks

    Returns whether this item stack can be stacked with the given itemStack. This is determined by comparing the item type and any custom data and properties associated with the item stacks. The amount of each item stack is not taken into consideration.