Solved Custom enchantment error

Discussion in 'Plugin Development' started by valera6666, Jun 6, 2021.

  1. Offline

    valera6666

    Hey! I decided to turn to you after I rummaged through the entire Internet.


    I have a problem, after I created and registered my enchantment, I created an item with this enchantment and I got an error:
    Code:
    java.lang.NullPointerException: null key in entry: null=1
            at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:32) ~[server.jar:git-Bukkit-84f3da3]
            at com.google.common.collect.ImmutableMapEntry.<init>(ImmutableMapEntry.java:49) ~[server.jar:git-Bukkit-84f3da3]
            at com.google.common.collect.ImmutableMap.entryOf(ImmutableMap.java:172) ~[server.jar:git-Bukkit-84f3da3]
            at com.google.common.collect.ImmutableMap$Builder.put(ImmutableMap.java:248) ~[server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.inventory.CraftMetaItem.serializeEnchantments(CraftMetaItem.java:1175) ~[server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.inventory.CraftMetaItem.serialize(CraftMetaItem.java:1115) ~[server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.inventory.CraftMetaItem.serialize(CraftMetaItem.java:1098) ~[server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.inventory.CraftMetaItem.toString(CraftMetaItem.java:1240) ~[server.jar:git-Bukkit-84f3da3]
            at java.lang.String.valueOf(Unknown Source) ~[?:1.8.0_291]
            at java.lang.StringBuilder.append(Unknown Source) ~[?:1.8.0_291]
            at org.bukkit.inventory.ItemStack.toString(ItemStack.java:238) ~[server.jar:git-Bukkit-84f3da3]
            at java.lang.String.valueOf(Unknown Source) ~[?:1.8.0_291]
            at com.menuplugin.Main.onEnable(Main.java:32) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:265) ~[server.jar:git-Bukkit-84f3da3]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:339) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:409) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin(CraftServer.java:423) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins(CraftServer.java:349) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.reload(CraftServer.java:786) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.Bukkit.reload(Bukkit.java:613) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:28) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchCommand(CraftServer.java:684) [server.jar:git-Bukkit-84f3da3]
            at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchServerCommand(CraftServer.java:670) [server.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.handleCommandQueue(DedicatedServer.java:423) [server.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.DedicatedServer.b(DedicatedServer.java:383) [server.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:792) [server.jar:git-Bukkit-84f3da3]
            at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:695) [server.jar:git-Bukkit-84f3da3]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]
    Main class code:
    Code:
    public class Main extends JavaPlugin implements Listener {
        public Items it = new Items(this);
        private Inventory i;
        @Override
        public void onEnable() {
            registerGlow();
            File config = new File(getDataFolder(),"config.yml");
            if(!config.exists()){
                getLogger().info("Creating new config file...");
                getConfig().options().copyDefaults(true);
                saveDefaultConfig();
            }
            getLogger().info(String.valueOf(it.spawnItem()));
            getLogger().info(String.valueOf(getConfig().getIntegerList("spawn")));
            i = Bukkit.createInventory(null,3*9,"INV");
            ItemStack item = new ItemStack(Material.ARROW);
            i.setItem(0,item);
            getLogger().info("MenuPlugin Enabled!");
            Bukkit.getPluginManager().registerEvents((Listener) this,this);
            getCommand("pmenu").setExecutor(this);
        }
    
        @Override
        public void onDisable() {
            getLogger().info("MenuPlugin Disabled!");
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            p.openInventory(i);
            return true;
        }
        @EventHandler
        public void handle(InventoryClickEvent e){
            Inventory inv = e.getInventory();
            Inventory c = e.getClickedInventory();
            if(!inv.equals(i))return;
            if(c == null) return;
            if(!c.equals(inv)) {
                e.setCancelled(true);
                return;
            }
            if(e.getCurrentItem().getType().equals(Material.AIR)) return;
            getLogger().info(String.valueOf(e.getCurrentItem()));
            e.setCancelled(true);
            //if(c == null) return;
            //if(c.equals(inv)) e.setCancelled(true);
    
        }
        public void registerGlow(){
            try{
                Field f = Enchantment.class.getDeclaredField("acceptingNew");
                f.setAccessible(true);
                f.set(null,true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try{
                NamespacedKey key = new NamespacedKey(this,"70");
                Glow glow = new Glow(key);
                Enchantment.registerEnchantment(glow);
            }catch (IllegalArgumentException e){
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    Items class code:
    Code:
    public class Items {
        private final Main plugin;
        public Items(Main plugin){
            this.plugin = plugin;
        }
        public ItemStack spawnItem(){
            NamespacedKey key = new NamespacedKey(plugin, "71");
            Glow glow = new Glow(key);
            ItemStack item = new ItemStack(Material.GOLD_BLOCK);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(ChatColor.GOLD+""+ChatColor.BOLD+"Меню спавна");
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.BLUE+"Открывает меню спавна");
            lore.add(ChatColor.BLUE+"Также сдесь можно настроить спавн");
            meta.addEnchant(glow,1,true);
            meta.setLore(lore);
            item.setItemMeta(meta);
            //plugin.getLogger().info(String.valueOf(item));
            return item;
        }
    }

    Excluding each line in turn, I realized that the problem is in the Item class: meta.addEnchant (glow, 1, true);
    But in the guides on the forums for creating a custom enchantment, I did not find a solution to my problem and even went to the documentation, but I did not find anything there either, so I ask you to help me.

    I would be happy for every help!

    P.s. I recently started developing plugins, so I can not know some of the points.
     
  2. Offline

    Xp10d3

    ItemMeta#addEnchant doesn't pass "Glow", but rather passes "Enchantment".
     
  3. Offline

    Shqep

    @valera6666
    You're creating entirely fresh and new empty instances of Glow and NamespacedKey. Keep only one instance of the custom enchantment class, and pass with that exact instance. I'm guessing this is the problem.
    Code:Java
    1. public static Glow glowingEnch; // Do your own initialization and put this somewhere to hold this instance.
    2.  
    3. // And when you want to apply, just retrieve that instance:
    4. meta.addEnchant(SomeClass.glowingEnch, 1, true);
     
  4. Offline

    valera6666

    Thanks for the help! I found the error after you advised me your ideas.
    The problem was that my enchantment didn't have a name. I found out when I compared my enchantment and vanilla enchantment. And me just had to change null to "" and everything did work as it should.
    Code for others:
    Code:
        public String getName() { //Glow class
            return null; //before
        }
    Code:
        public String getName() {//Glow class
            return ""; // after
        }
     
    Shqep likes this.
  5. Offline

    Shqep

    @valera6666
    If that's the case then you shouldn't put an empty String at all. Put the name of the Enchantment instead. "Glow" would work fine.
     

Share This Page