1.16.5 Why can't my enchantment register?

Discussion in 'Spigot Plugin Development' started by CrossedLivid, Jun 11, 2021 at 1:35 AM.

  1. So I'm working on a enchantment called smelting, and i cant register the enchantment. here's my code: (i've tried to directly call registerEnchantment(), but doesn't work either) my other enchant, telepathy, works fine. It turns out there was some exception
    CustomEnchants.java
    Code (Java):
    package net.wardensmp.plugins.enchantments;

    import org.bukkit.enchantments.Enchantment;

    import java.lang.reflect.Field;
    import java.util.Arrays;
    import java.util.stream.Collectors;

    public class CustomEnchants {

        public static final Enchantment TELEPATHY = new EnchantmentWrapper("telepathy", "Telepathy", 1);
        public static final Enchantment SMELTING = new EnchantmentWrapper("smelting", "Smelting", 1);

        public static void register() {
            boolean registered =
                    Arrays.stream(Enchantment.values()).collect(Collectors.toList()).contains(CustomEnchants.TELEPATHY);
            boolean registered1 =
                    Arrays.stream(Enchantment.values()).collect(Collectors.toList()).contains(CustomEnchants.SMELTING);
            if (!registered) {
                registerEnchantment(TELEPATHY);
            }
            if (!registered1) {
                registerEnchantment(SMELTING);
            }
        }

        public static void registerEnchantment(Enchantment enchantment) {
            boolean registered = true;
            try {
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null, true);
                Enchantment.registerEnchantment(enchantment);
            } catch (Exception e) {
                registered = false;
                System.out.println("?");
            }
            if (registered) {
                System.out.println("Registered enchantment" + enchantment.getName());
            }
        }
    }
     
    Main.java
    package net.wardensmp.plugins;

    import net.wardensmp.plugins.commands.Telepathy;
    import net.wardensmp.plugins.enchantments.CustomEnchants;
    import net.wardensmp.plugins.events.AsyncPlayerChat;
    import net.wardensmp.plugins.events.BlockBreakEv;
    import net.wardensmp.plugins.events.PlayerJoin;
    import net.wardensmp.plugins.recipes.CustomRecipes;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    public final class WardenSMP extends JavaPlugin {

    private final CustomRecipes recipes = new CustomRecipes(this);
    static WardenSMP plugin;

    @Override
    public void onEnable() {
    System.out.println("WardenSMP enabled");
    plugin = this;
    CustomEnchants.register();
    CustomEnchants.registerEnchantment(CustomEnchants.SMELTING);
    // getCommand("invulnerable").setExecutor(new Invulnerable());
    getCommand("telepathy").setExecutor(new Telepathy());
    getServer().getPluginManager().registerEvents(new PlayerJoin(), this);
    getServer().getPluginManager().registerEvents(new AsyncPlayerChat(), this);
    getServer().getPluginManager().registerEvents(new BlockBreakEv(), this);
    Bukkit.addRecipe(recipes.thorAxeRecipe());
    Bukkit.addRecipe(recipes.teleportCompassRecipe());
    }

    @Override
    public void onDisable() {
    System.out.println("WardenSMP disabled");
    }

    public static WardenSMP getPlugin() {
    return plugin;
    }
    }
    my event that detects the smelting thing, if you need it:
    Code (Java):
    package net.wardensmp.plugins.events;

    import net.wardensmp.plugins.enchantments.CustomEnchants;
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.Container;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.FurnaceRecipe;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;

    import java.util.Collection;
    import java.util.Iterator;

    public class BlockBreakEv implements Listener {

        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            //Telepathy
            Player player = e.getPlayer();
            Block block = e.getBlock();
            Collection<ItemStack> drops = block.getDrops(player.getInventory().getItemInMainHand());
            if (player.getInventory().getItemInMainHand() != null) {
                if (player.getInventory().getItemInMainHand().hasItemMeta()) {
                    if (player.getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchants.TELEPATHY)) {
                        if (!(player.getGameMode() == GameMode.CREATIVE) || !(player.getGameMode() == GameMode.SPECTATOR)) {
                            if (!(player.getInventory().firstEmpty() == -1)) {
                                if (!(e.getBlock().getState() instanceof Container)) {
                                    e.setDropItems(false);
                                    if (drops.isEmpty()) {
                                        return;
                                    }
                                    player.getInventory().addItem(drops.iterator().next());
                                }
                            }
                        }
                    }
                }
            }
            //Smelting
            if (player.getInventory().getItemInMainHand() != null) {
                if (player.getInventory().getItemInMainHand().hasItemMeta()) {
                    if (player.getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchants.SMELTING)) {
                        if (!(player.getGameMode() == GameMode.CREATIVE) || !(player.getGameMode() == GameMode.SPECTATOR)) {
                            Iterator<Recipe> recipes = Bukkit.recipeIterator();
                            ItemStack result = null;
                            while (recipes.hasNext()) {
                                Recipe recipe = recipes.next();
                                if (!(recipe instanceof FurnaceRecipe)) {
                                    continue;
                                }
                                if (((FurnaceRecipe) recipe).getInput().getType() != e.getBlock().getType()) {
                                    continue;
                                }
                                result = recipe.getResult();
                                break;
                            }
                            if (player.getInventory().getItemInMainHand().getItemMeta().hasEnchant(CustomEnchants.TELEPATHY)) {
                                player.getInventory().addItem(result);
                            } else {
                                player.getWorld().dropItem(player.getLocation(), result);
                            }
                            if (result.getType() == Material.DIAMOND || result.getType() == Material.REDSTONE
                                    || result.getType() == Material.EMERALD) {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
     
     
    #1 CrossedLivid, Jun 11, 2021 at 1:35 AM
    Last edited: Jun 11, 2021 at 1:52 AM
  2. Whats the exception?

    Edit: Please send the whole exception.
     
  3. No errors at console it was in a try block

    Ill try removing it
     
  4. Yea don't ever catch errors, unless you know what your doing.