Skip to content

Enchantment Table

LLytho edited this page Oct 21, 2022 · 3 revisions

Enchantment Table

Enchanting

/**
 * Gets called when a player enchant an item. This can be cancelled.
 */
// for 1.18 pls use: onEvent("morejs.enchantment_table.enchant", (event) => { ... })
MoreJSEvents.enchantmentTableEnchant((event) => {
    /**
     * `event.position`: Position of the enchantment table
     * `event.player`: Player that enchant the item
     * `event.level`: The level (Overworld, etc.)
     * `event.item`: The item that will be enchanted
     * `event.secondItem`: The second item. Mostly lapis lazuli.
     * `event.get(slot)`: Get the enchantment data from given slot (0, 1 or 2)
     */

    let firstSlot = event.get(0);
    firstSlot.requiredLevel; // The required level to enchant the item
    firstSlot.enchantmentCount; // The amount of enchantments the item will have
    firstSlot.forEachEnchantments((enchantment, level) => {
        // `enchantment` -> The enchantment
        // `level` -> The level of the enchantment
    });
});

Slot changed

/**
 * Gets called when a player changed the item in the table.
 */
// for 1.18 pls use: onEvent("morejs.enchantment_table.changed", (event) => { ... })
MoreJSEvents.enchantmentTableChanged((event) => {
    /**
     * `event.position`: Position of the enchantment table
     * `event.player`: Player that enchant the item
     * `event.level`: The level (Overworld, etc.)
     * `event.item`: The item that will be enchanted
     * `event.secondItem`: The second item. Mostly lapis lazuli.
     * `event.get(slot)`: Get the enchantment data from given slot (0, 1 or 2).
     */

    let firstSlot = event.get(0);
    firstSlot.requiredLevel; // The required level to enchant the item
    firstSlot.enchantmentCount; // The amount of enchantments the item will have
    firstSlot.forEachEnchantments((enchantment, level) => {
        // `enchantment` -> The enchantment
        // `level` -> The level of the enchantment
    });

    // In this event we can also mutate the data of the slot.
    firstSlot.requiredLevel = 50; // Set the required level to 50
    firstSlot.clearEnchantments(); // Remove all enchantments in this slot
    firstSlot.removeEnchantments((enchantment, level) => {
        // `enchantment` -> The enchantment
        // `level` -> The level of the enchantment
        // return true if the enchantment should be removed
        return true / false;
    });
    firstSlot.addEnchantment(enchantment, level); // Add an enchantment to this slot

    // After mutating the enchantments you should call `updateClue()` to update the table.
    firstSlot.updateClue();
});

Tooltip

This events triggers when the player hovers over an enchantment slot.

// for 1.18 pls use: onEvent("morejs.enchantment_table.tooltip", (event) => { ... })
MoreJSEvents.enchantmentTableTooltip((event) => {
    /*
     * `event.item`: The item in the enchanting slot
     * `event.secondItem`: Mostly lapis lazuli
     * `event.lines`: The tooltip lines
     * `event.player`: The player that is hovering over the slot
     * `event.level`: The player's level (overworld, etc.)
     * `event.slot`: The slot that is hovered over (0, 1 or 2)
     * `event.requiredLevel`: The required experience level to enchant
     * `event.clue`: The clue that is shown in the tooltip
     **/
    event.lines.add("You cannot enchant this. Please pay 4.99$ to get rid of this message.");
});
Clone this wiki locally