Custom Enchantment API
API Tutorial
What is it?
The Custom Enchantment API is available for developers who think the normal config isn't advanced enough.
With the API you can do anything Bukkit can handle. So let's start with importing the library.
Adding the library
To add the library, just import it like the Bukkit library, but with the plugin jar instead.
In the plugin.yml you then need to add:
softdepend: [Enchantment Control]
If you already have a plugin there, you can add Enchantment Control like this:
softdepend: [OtherPlugin, Enchantment Control]
When you have done that, you are ready to continue to the next section.
Code
Now we can start coding. To add a custom enchantment you can do like this:
ItemStack sword = new ItemStack(Material.IRON_SWORD, 1); CustomEnchantment ench = new CustomEnchantment("custom0"); EnchantmentAPI.addEnchant(sword, ench);
This adds the enchantment custom0 from the config.
To check if the sword has an enchantment you can do this:
if (EnchantmentAPI.hasEnchant(sword, ench)){ //Do something }
Let's unleash the true power of the API, by adding some custom features. First make a class that can handle the CustomEnchantHitEvent:
public class HitListener implements Listener{ @EventHandler public void onHit(CustomEnchantHitEvent event){ //Do some cool stuff } }
Also, in the onEnable(), you need to register the listener as usual.
public void onEnable(){ //Your normal stuff here //Check if the plugin is loaded, then register events if (getServer().getPluginManager().getPlugin("Enchantment Control") != null){ Bukkit.getServer().getPluginManager().registerEvents(new HitListener(), this); } }
Now when you know the basics, we can start doing stuff. The CustomEnchantHitEvent and CustomEnchantment classes have some methods that I'll cover now:
@EventHandler public void onHit(CustomEnchantHitEvent event){ Player p = event.getPlayer(); LivingEntity e = event.getEntity(); for (CustomEnchantment ench : event.getEnchantments()){ String name = ench.getName(); String tier = ench.getTier(); if (name.equals("Troll")){ if (tier.equals("I")) e.setVelocity(new Vector(0, 1, 0)); else if (tier.equals("II")) e.setVelocity(new Vector(0, 2, 0)); p.sendMessage("Watch your enemy fall to their doom."); } } }
Now you should have an understanding of how this API works. You can now start making lots of crazy enchantments!
Just remember to define your enchantment in the config first!
Comments