to use EAPI you must first import it using maven, to do that you need to add the following repository and dependency
Code (Text):
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Code (Text):
<dependency>
<groupId>com.github.clickinggames</groupId>
<artifactId>EnchantmentAPI</artifactId>
<version>1.2.1</version>
<scope>provided</scope>
</dependency>
after you imported the libary it should all be set, remember to put the plugin in the server's plugin list as the scope is depended on it.
now, to create your first enchantment! first off create your plugin class and import the enchantments
Code (Text):
import com.github.clickinggames.enchantmentapi.CustomEnchant;
import org.bukkit.plugin.java.JavaPlugin;
public final class Example extends JavaPlugin {
@Override
public void onEnable() {
// Plugin startup logic
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}
then we want to add our first enchantment, which will be Telekenesis
Code (Text):
public final class Example extends JavaPlugin {
public static CustomEnchant TELEKENESIS = null;
@Override
public void onEnable() {
//Plugin startup logic
TELEKENESIS = new CustomEnchant(CustomEnchant.generateKey(Bukkit.getPluginManager().getPlugin("Aiepmle"), "TELEKENESIS"), EnchantmentTarget.TOOL,1,"Telekenesis",false,13);
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
}
make sure to replace PLUGIN NAME with the name of your plugin , now to explain the 'new CustomEnchant()' function, the first variable is the NameSpaceKey, which is how minecraft knows what ur enchantment IS, i have made a built in function that generates a key for you! its called CustomEnchant.generateKey() it requires the plugin as the first variable and the ID of the enchantment as the second.
the second variable is the enchantment target, what item it will be available on. refer to this
link for more Enchantment targets. the third is the maximum level of the enchant, for example sharpness is 5 and unbreaking is 3, if you pick 1 the API will not regonize ur enchantment to have levels, but as a singuler enchant, AKA Mending and Flame.
the forth variable is the name that displays on the item itself.
the fith variable defines if the enchantment is a curse, if set as true the color of the enchantment will change to red, such as "curse of vanishing" and "curse of binding"
the sixth and last variable sets the change of it appearing in an enchanting table, 1 is 0.1% and 1000 is 100%, so 13 is 1.3%
now, we registered our enchantment, but does it work? no!
because we now need to program the functionalty of it! create a simple event listener class:
Code (Text):
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
public class MyListener implements Listener {
Plugin plugin;
public MyListener(Plugin plugin){
this.plugin=plugin;
}
}
now we need to register this Listener in the main plugin page
Code (Text):
@Override
public void onEnable() {
TELEKENESIS = new CustomEnchant(CustomEnchant.generateKey(Bukkit.getPluginManager().getPlugin("Aiepmle"), "TELEKENESIS"), EnchantmentTarget.TOOL,1,"Telekenesis",false,13);
// Plugin startup logic
getServer().getPluginManager().registerEvents(new MyListener(this), this);
}
now lets make the simple event:
Code (Text):
@EventHandler
public void OnBlockBreak(BlockBreakEvent e){
}
now lets add the code
Code (Text):
@EventHandler
public void OnBlockBreak(BlockBreakEvent e){
//checking if the player has the telekenesis enchant
if(!e.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasEnchant(Aiepmle.TELEKENESIS))
return;
//check if the inventory is full
if(e.getPlayer().getInventory().firstEmpty()==-1)
return;
//we set it so that the block items don't drop on the ground
e.setDropItems(false);
//now we add the items to the inventory of the player!
e.getPlayer().getInventory().addItem(e.getBlock().getDrops(e.getPlayer().getInventory().getItemInMainHand()).toArray(new ItemStack[0]));
}
now that we got the simple things finished build your project and import it into the plugins folder!
make sure to edit your plugin.yml folder to include this:
Code (Text):
depend:
-EnchantmentAPI
load: POSTWORLD
after that boot the server up, and use /eapi list
to view the list of custom enchantmets, your Enchantment should appear in the list, if it does not. look in the console for errors and make sure your code is copied correctly, if it does show up, hold a tool and use /eapi ench TELEKENESIS 1, that should get your item to have the enchant, then try it out. and ENJOY!
command usage:
Code (Text):
new
CustomEnchant(NameSpaceKey,EnchantID), EnchantmentTarget,Maxlevel,DisplayName,IsCurse,Chance to appear out of 1000);
another usage is:
new
CustomEnchant(NameSpaceKey key,String id, EnchantmentTarget target,int Maxlevel,String DisplayName,boolean IsCurse,int Chance to appear,List<Enchantment> Conflicting enchants);
1 chance to appear is 0.1% 1000 chance to appear is 100%
Code (Text):
CustomEnchantment.addEnchantment(ItemStack itemStack,CustomEnchant enchant,int level,boolean ignoreRestriction)
this adds a custom enchantment to the following item.
Code (Text):
CustomEnchant.generateKey(Plugin plugin,String id)
generates a key for your enchantment
Code (Text):
EnchantmentAPI.integerToRoman(int num)
returns a roman string of your number