Skip to main content

Class: ItemEnchantableComponent

When present on an item, this item can have enchantments applied to it.

Example

// Spawns a bunch of item stacks
import { EnchantmentType, ItemComponentTypes, ItemStack, Player } from '@minecraft/server';
import { MinecraftItemTypes, MinecraftEnchantmentTypes } from '@minecraft/vanilla-data';

function giveFireSword(player: Player) {
const ironFireSword = new ItemStack(MinecraftItemTypes.DiamondSword, 1);

const enchantments = ironFireSword?.getComponent(ItemComponentTypes.Enchantable);
if (enchantments) {
enchantments.addEnchantment({ type: new EnchantmentType(MinecraftEnchantmentTypes.FireAspect), level: 1 });
}

const inventory = player.getComponent('minecraft:inventory');
if (inventory === undefined || inventory.container === undefined) {
return;
}
inventory.container.setItem(0, ironFireSword);
}

Extends

Constructors

new ItemEnchantableComponent()

private new ItemEnchantableComponent(): ItemEnchantableComponent

Returns

ItemEnchantableComponent

Overrides

ItemComponent . constructor

Properties

slots

readonly slots: EnchantmentSlot[]

Throws

This property can throw when used.


typeId

readonly typeId: string

Remarks

Identifier of the component.

Inherited from

ItemComponent . typeId


componentId

static readonly componentId: "minecraft:enchantable" = 'minecraft:enchantable'

Methods

addEnchantment()

addEnchantment(enchantment): void

Parameters

ParameterTypeDescription
enchantmentEnchantmentThe enchantment interface to be added.

Returns

void

Remarks

Adds an enchantment to the item stack.

This function can't be called in read-only mode.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if the enchantment type does not exist.

ScriptItemEnchantmentLevelOutOfBoundsError: Exception thrown if the enchantment level is outside the allowable range for the given enchantment type.

ScriptItemEnchantmentTypeNotCompatibleError: Exception thrown if the enchantment is not compatible with the item stack.

EnchantmentLevelOutOfBoundsError

EnchantmentTypeNotCompatibleError

EnchantmentTypeUnknownIdError

Error


addEnchantments()

addEnchantments(enchantments): void

Parameters

ParameterTypeDescription
enchantmentsEnchantment[]The list of enchantments to be added.

Returns

void

Remarks

Adds a list of enchantments to the item stack.

This function can't be called in read-only mode.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if any enchantment type does not exist.

ScriptItemEnchantmentLevelOutOfBoundsError: Exception thrown if any enchantment level is outside the allowable range for the given enchantment type.

ScriptItemEnchantmentTypeNotCompatibleError: Exception thrown if any enchantment is not compatible with the item stack.

EnchantmentLevelOutOfBoundsError

EnchantmentTypeNotCompatibleError

EnchantmentTypeUnknownIdError

Error


canAddEnchantment()

canAddEnchantment(enchantment): boolean

Parameters

ParameterTypeDescription
enchantmentEnchantmentThe enchantment interface to be added.

Returns

boolean

Returns true if the enchantment can be added to the item stack.

Remarks

Checks whether an enchantment can be added to the item stack.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if the enchantment type does not exist.

ScriptItemEnchantmentLevelOutOfBoundsError: Exception thrown if the enchantment level is outside the allowable range for the given enchantment type.

EnchantmentLevelOutOfBoundsError

EnchantmentTypeUnknownIdError


getEnchantment()

getEnchantment(enchantmentType): Enchantment

Parameters

ParameterTypeDescription
enchantmentTypestring | EnchantmentTypeThe enchantment type to get.

Returns

Enchantment

Returns the enchantment if it exists on the item stack.

Remarks

Gets the enchantment of a given type from the item stack.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if the enchantment type does not exist.

EnchantmentTypeUnknownIdError


getEnchantments()

getEnchantments(): Enchantment[]

Returns

Enchantment[]

Returns a list of enchantments on the item stack.

Remarks

Gets all enchantments on the item stack.

Throws

This function can throw errors.


hasEnchantment()

hasEnchantment(enchantmentType): boolean

Parameters

ParameterTypeDescription
enchantmentTypestring | EnchantmentTypeThe enchantment type to check for.

Returns

boolean

Returns true if the item stack has the enchantment type.

Remarks

Checks whether an item stack has a given enchantment type.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if the enchantment type does not exist.

EnchantmentTypeUnknownIdError


isValid()

isValid(): boolean

Returns

boolean

Whether the component is valid.

Inherited from

ItemComponent . isValid

Remarks

Returns whether the component is valid. A component is considered valid if its owner is valid, in addition to any addition to any additional validation required by the component.


removeAllEnchantments()

removeAllEnchantments(): void

Returns

void

Remarks

Removes all enchantments applied to this item stack.

This function can't be called in read-only mode.

Throws

This function can throw errors.


removeEnchantment()

removeEnchantment(enchantmentType): void

Parameters

ParameterTypeDescription
enchantmentTypestring | EnchantmentTypeThe enchantment type to remove.

Returns

void

Remarks

Removes an enchantment of the given type.

This function can't be called in read-only mode.

Throws

ScriptItemEnchantmentUnknownIdError: Exception thrown if the enchantment type does not exist.

EnchantmentTypeUnknownIdError

Error