Bonjour, je suis en train de m'exercer sur la création d'enchantements custom via l'api de spigot en 1.15.1 mais j'ai quelques problèmes : mon enchantement ne s'affiche pas dans la table d'enchantement :
Voici mon code :
Classe principale :
Code (cpp):
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentOffer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
public class Main extends JavaPlugin implements Listener {
private static Main instance;
Wither wither = new Wither(new NamespacedKey(this, "wither"));
@Override
public void onEnable() {
instance = this;
loadEnchantments();
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getPluginManager().registerEvents(wither, this);
}
public void onDisable() {
try {
Field byKeyField = Enchantment.class.getDeclaredField("byKey");
Field byNameField = Enchantment.class.getDeclaredField("byName");
byKeyField.setAccessible(true);
byNameField.setAccessible(true);
HashMap<NamespacedKey, Enchantment> byKey = (HashMap<NamespacedKey, Enchantment>) byKeyField.get(null);
HashMap<NamespacedKey, Enchantment> byName = (HashMap<NamespacedKey, Enchantment>) byNameField.get(null);
if(byKey.containsKey(wither.getKey())) {
byKey.remove(wither.getKey());
}
if(byName.containsKey(wither.getName())) {
byName.remove(wither.getName());
}
} catch (Exception ignored) {
}
}
private void loadEnchantments() {
try {
try {
Field f = Enchantment.class.getDeclaredField("acceptingNew");
f.setAccessible(true);
f.set(null, true);
} catch (Exception e) {
e.printStackTrace();
}
try {
Enchantment.registerEnchantment(wither);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Main getInstance() {return instance;}
@EventHandler
public void onJoin(PlayerJoinEvent e) {
ItemStack it = new ItemStack(Material.DIAMOND_SWORD, 1);
ItemMeta meta = it.getItemMeta();
ArrayList<String> lores = new ArrayList<>();
lores.add(ChatColor.GRAY +wither.getName() + " I");
meta.setLore(lores);
it.setItemMeta(meta);
it.addEnchantment(wither, 1);
e.getPlayer().getInventory().addItem(it);
}
}
Classe Wither où est stocké mon enchantement :
Code (cpp):
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
public class Wither extends Enchantment implements Listener {
@EventHandler
public void onHit(EntityDamageByEntityEvent e) {
if(e.getDamager() instanceof Player) {
Player p = (Player) e.getDamager();
ItemStack it = p.getInventory().getItemInMainHand();
if(it.getEnchantments().containsKey(this)) {
p.getWorld().createExplosion(e.getEntity().getLocation(), 1, false);
}
}
}
public Wither(NamespacedKey key) {
super(key);
}
@Override
public NamespacedKey getKey() {
return super.getKey();
}
@Override
public String getName() {
return "Wither";
}
@Override
public int getMaxLevel() {
return 2;
}
@Override
public int getStartLevel() {
return 1;
}
@Override
public EnchantmentTarget getItemTarget() {
return EnchantmentTarget.WEAPON;
}
@Override
public boolean isTreasure() {
return false;
}
@Override
public boolean isCursed() {
return false;
}
@Override
public boolean conflictsWith(Enchantment enchantment) {
return false;
}
@Override
public boolean canEnchantItem(ItemStack itemStack) {
return true;
}
}
Voici en jeu le screen de la table d'enchantement :