Applying Enchantments Programmatically

So you want to apply one of the custom enchantments to an item without the use of an enchanting table? Not a problem! The method on each of your enchantments: addToItem(ItemStack, int) allows for easy applying to any item in the game (regardless of what items you said to limit the enchanting table to). An example:

    ItemStack item = new ItemStack(Material.DIAMOND_SWORD); // Create a diamond sword
    EnchantmentAPI.getEnchantment("Lifesteal").addToItem(item, 10); // Retrieve the 'Lifesteal' enchantment and apply it to the sword with a level 10 enchantment
    player.getInventory().addItem(item); // Give the player the enchanted sword

Now if you want to remove an enchantment of an item, you use this:

    EnchantmentAPI.getEnchantment(enchantmentName).removeFromItem(item);

Or to remove all of the enchantments from an item:

    for (CustomEnchantment enchantment : EnchantmentAPI.getEnchantments(item)) {
            enchantment.removeFromItem(item);
    }

Simple, no?


Comments

Posts Quoted:
Reply
Clear All Quotes