Creating Custom Enchantments
Creating an Enchantment
The complete enchantment class will look like this:
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.item.enchantment.EnchantmentRarity;
import cn.nukkit.item.enchantment.EnchantmentType;
import cn.nukkit.utils.Identifier;
public class EnchantmentCustomExample extends Enchantment {
public EnchantmentCustomExample() {
super(new Identifier("lumi:custom_enchant"), "Example enchantment", EnchantmentRarity.COMMON, EnchantmentType.ALL);
}
}Specify the enchantment’s string identifier, name, rarity, and applicable item type:
public class EnchantmentCustomExample extends Enchantment {
public EnchantmentCustomExample() {
super(new Identifier("lumi:custom_enchant"), "Example enchantment", EnchantmentRarity.COMMON, EnchantmentType.ALL);
}The identifier must strictly follow this format: namespace:identifier. Example: custom:myenchantment.
Registering an Enchantment
@Override
public void onLoad() {
Registries.ENCHANTMENT.registerCustom(new EnchantmentCustomExample(), true);
}Set this value to true to register the enchanted book. Set it to false if the enchanted book should not be registered.
Handling an Enchantment
Use the Item#hasCustomEnchantment(String identifier) method to check whether an item has a specific enchantment. Example:
this.getServer().getEventBus().subscribe(BlockBreakEvent.class, event -> {
if (event.getItem().hasCustomEnchantment("lumi:custom_enchant")) {
// Write your code here
}
});Adding an Enchantment to an Item
A custom enchantment is applied to an item in the same way as a standard enchantment:
item.addEnchantment(new EnchantmentCustomExample());or:
item.addEnchantment(Enchantment.get("lumi:custom_enchant"));Enchantment Rarities (EnchantmentRarity)
EnchantmentRarity.COMMON — Common
EnchantmentRarity.UNCOMMON — Uncommon
EnchantmentRarity.RARE — Rare
EnchantmentRarity.VERY_RARE — Very rare
Enchantment Types (EnchantmentType)
EnchantmentType.ALL — All items
EnchantmentType.ARMOR — Any armor
EnchantmentType.ARMOR_HEAD — Helmets
EnchantmentType.ARMOR_TORSO — Chestplates
EnchantmentType.ARMOR_LEGS — Leggings
EnchantmentType.ARMOR_FEET — Boots
EnchantmentType.SWORD — Swords
EnchantmentType.DIGGER — Mining tools
EnchantmentType.FISHING_ROD — Fishing rods
EnchantmentType.BREAKABLE — Items with durability
EnchantmentType.BOW — Bows
EnchantmentType.WEARABLE — Wearable items
EnchantmentType.TRIDENT — Tridents
EnchantmentType.CROSSBOW — Crossbows
EnchantmentType.MACE — Maces