Registering your enchantment allow Custom Anvil to use your enchantment.
To register your enchantment, you need into a CAEnchantment instance and then register it.
This is so Custom Anvil know how to handle action with your enchantment like adding/removing and getting level of it from an item.
Creating a Custom Anvil enchantment
If you were able to register your enchantment to the bukkit registry.
Then you should use or extend CABukkitEnchantment
CAEnchantment enchantment = new CABukkitEnchantment(bukkitEnchant);
If your enchantment is not registered into the bukkit registry
For example, if you store via persistent data:
Then you should preferably create a class extending CAEnchantmentBase and implement the required functions:
getLevel: To get the enchantment level using item and item meta.isEnchantmentPresent: To test is an enchantment is present in an item or an item meta.addEnchantmentUnsafe: To add the enchantment with the specified level using item or an item meta.removeFrom: To remove the enchantment from the specified item.
You can add optional features by implementing other function:
isCleanOptimisedandisCleanOptimised: Indicate if a bulk operation exists for this enchantment.isAllowed: Allow the enchantment to be combined by only certain players. Useful for example if you like to allow only player with a specific permission.
You also need 3 objects for the constructor:
key: Namespaced key of your enchantment.defaultRarity: Default rarity of your enchantment. Please note "rarity" is how custom anvil name the concept of "Multiplier by book/item". (minecraft wiki)defaultMaxLevel: Default maximum enchantment level.
You can also directly implement CAEnchantment, but it is not recommended.
Custom Restriction
You may want to implement AdditionalTestEnchantment to your Custom Anvil Enchantment class. It allows you to add external restriction:
isEnchantConflict: Test if the enchantment is compatible with a provided enchantment map. (key = enchantment, value = level)isItemConflict: Test if this enchantment is compatible with a provided item (and its enchantments)
Registering your custom anvil enchantment
EnchantmentApi.registerEnchantment(enchantment);
To register the enchantment present on startup, you should listen to the CAEnchantRegistryReadyEvent event and register your enchantment.
It should look something like this example:
@EventHandler
public void onEnchantmentRegistry(CAEnchantRegistryReadyEvent event){
// you could also get a list of your enchantment object and create CAEnchant object from your object here instead.
List<CAEnchantment> enchantments = getEnchantmentList();
for (CAEnchantment enchantment : enchantments) {
EnchantmentApi.registerEnchantment(enchantment);
}
}
You can register/unregister enchantments at runtime via the EnchantmentApi facade. But every enchantment using previously written conflict should be registered when the CAEnchantRegistryReadyEvent event is called.
If you do not register the enchantment in time, the conflict will not find your enchantment and will cause issue.
It is recommended to register conflict alongside its enchantment when CAConfigReadyEvent is triggered. (wiki in WIP)