For Developers
Custom Features
Custom Enchantments

Custom Enchantments

In PNX, you can create your own custom enchantments. You can define their behavior, rarity, type, and maximum level.
Custom enchantments allow you to introduce new gameplay mechanics or modify existing ones.

Registering a Custom Enchantment

It is recommended to register your enchantment while your plugin is loaded, but before it is enabled.

You can register your enchantment like this:

import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.registry.RegisterException;
 
try {
    Enchantment.register(new MyEnchantment());
} catch (RegisterException e) {
    throw new RuntimeException(e);
}

Defining Your Enchantment

You may add more logic by overriding additional methods such as getDamageBonus(), etc.

package cn.powernukkitx.exampleplugin.customench;
 
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.item.enchantment.EnchantmentType;
import cn.nukkit.utils.Identifier;
 
public class MyEnchantment extends Enchantment {
 
    public MyEnchantment() {
        super(new Identifier("powernukkitx:foo"), "Test2", Rarity.COMMON, EnchantmentType.ALL);
    }
 
    @Override
    public int getMaxLevel() {
        return 3;
    }
}

Your Identifier must be unique. Follow the recommended format: namespace:enchantment_name.


Registering the Enchantment

After defining your class, register it inside onEnable():

try {
    Enchantment.register(new MyEnchantment());
} catch (RegisterException e) {
    throw new RuntimeException(e);
}

Need Help?

If you need assistance, check out the Example Plugin or join our Discord server for support.