ItemEnchantableComponent Class

Caution

This class is still in pre-release. Its signature may change or it may be removed in future releases.

Extends

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

Examples

givePlayerIronFireSword.ts
// 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);
}

Properties

slots

read-only slots: EnchantmentSlot[];

Type: EnchantmentSlot[]

Warning

This property can throw errors when used.

Methods

addEnchantment

addEnchantment(enchantment: Enchantment): void

Adds an enchantment to the item stack.

Parameters

  • enchantment: Enchantment

    The enchantment interface to be added.

Important

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

addEnchantments

addEnchantments(enchantments: Enchantment[]): void

Adds a list of enchantments to the item stack.

Parameters

  • enchantments: Enchantment[]

    The list of enchantments to be added.

Important

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

canAddEnchantment

canAddEnchantment(enchantment: Enchantment): boolean

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

Parameters

  • enchantment: Enchantment

    The enchantment interface to be added.

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

Warning

This function can throw errors.

Throws EnchantmentLevelOutOfBoundsError, EnchantmentTypeUnknownIdError

getEnchantment

getEnchantment(enchantmentType: EnchantmentType | string): Enchantment | undefined

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

Parameters

Returns Enchantment | undefined - Returns the enchantment if it exists on the item stack.

Warning

This function can throw errors.

Throws EnchantmentTypeUnknownIdError

getEnchantments

getEnchantments(): Enchantment[]

Gets all enchantments on the item stack.

Returns Enchantment[] - Returns a list of enchantments on the item stack.

Warning

This function can throw errors.

hasEnchantment

hasEnchantment(enchantmentType: EnchantmentType | string): boolean

Checks whether an item stack has a given enchantment type.

Parameters

  • enchantmentType: EnchantmentType | string

    The enchantment type to check for.

Returns boolean - Returns true if the item stack has the enchantment type.

Warning

This function can throw errors.

Throws EnchantmentTypeUnknownIdError

removeAllEnchantments

removeAllEnchantments(): void

Removes all enchantments applied to this item stack.

Important

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

Warning

This function can throw errors.

removeEnchantment

removeEnchantment(enchantmentType: EnchantmentType | string): void

Removes an enchantment of the given type.

Parameters

  • enchantmentType: EnchantmentType | string

    The enchantment type to remove.

Important

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

Warning

This function can throw errors.

Throws EnchantmentTypeUnknownIdError, Error

Constants

componentId

static read-only componentId = "minecraft:enchantable";

Type: string

Examples

givePlayerIronFireSword.ts
// 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);
}