Solved Custom Enchantment Books

Discussion in 'Spigot Plugin Development' started by Dusk_2_Dawn, Jan 18, 2020.

  1. So I am creating a plugin where I am creating Custom Enchantment books and I need to have it apply the enchantment to the item and remove the book. What I have currently works perfectly except for the fact that it doesn't apply the enchantment to the item. Like it removes the book from the cursor, but it doesn't add the lore to the item.

    Here is the code I am using:
    Code (Java):
    @EventHandler
        public void invenClick(InventoryClickEvent e) {
            ItemStack book = e.getCursor();
            ItemStack weapon = e.getCurrentItem();
            String[] bookName = ChatColor.stripColor(book.getItemMeta().getDisplayName()).split(" ");

            ArrayList<Material> weapons = new ArrayList<>();
            weapons.add(Material.WOOD_SWORD);
            weapons.add(Material.WOOD_AXE);
            weapons.add(Material.STONE_SWORD);
            weapons.add(Material.STONE_AXE);
            weapons.add(Material.IRON_SWORD);
            weapons.add(Material.IRON_AXE);
            weapons.add(Material.DIAMOND_SWORD);
            weapons.add(Material.DIAMOND_AXE);
            weapons.add(Material.BOW);

            ArrayList<String> enchants = new ArrayList<>();
            enchants.add("Launch");
            enchants.add("Zeus");
            enchants.add("Rabbit");
            enchants.add("Swift");

            if (!(e.getInventory() instanceof PlayerInventory)) return;
            if(book.getType() != Material.ENCHANTED_BOOK) return;
            if(!weapons.contains(weapon.getType())) return;
            if(!enchants.contains(bookName[0])) return;

            if(weapon.hasItemMeta() && weapon.getItemMeta().hasLore()) {
                Integer enchantments = 0;
                for(int i = 0; i < weapon.getItemMeta().getLore().size(); i++) {
                    String[] lore = ChatColor.stripColor(weapon.getItemMeta().getLore().get(i)).split(" ");

                    if(enchants.contains(lore[0])) enchantments += 1;
                    else continue;
                }

                if(enchantments >= 3) {
                    e.getWhoClicked().sendMessage(main.prefix + ChatColor.RED + "You cannot have more than 3 custom enchantments!");
                    return;
                }
               
                e.getCursor().setType(Material.AIR);
                ItemMeta meta = addEnchantment(weapon, bookName[0], romanToNumber(bookName[1]));
                weapon.setItemMeta(meta);
            } else {
                e.getCursor().setType(Material.AIR);
                ItemMeta meta = addEnchantment(weapon, bookName[0], romanToNumber(bookName[1]));
                weapon.setItemMeta(meta);
            }
        }

        public ItemMeta addEnchantment(ItemStack item, String enchantment, Integer level) {
            if(item == null || enchantment == null) return null;

            ArrayList<String> newLore = new ArrayList<>();
            if(item.hasItemMeta() && item.getItemMeta().hasLore()) {
                newLore.addAll(item.getItemMeta().getLore());
                String romanLevel = numberToRoman(level);

                if(newLore.contains(ChatColor.AQUA + enchantment + " " + romanLevel)) return null;

                newLore.add(main.format(ChatColor.AQUA + enchantment) + " " + romanLevel);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setLore(newLore);
                return itemMeta;
            } else {
                String romanLevel = numberToRoman(level);
                newLore.add(main.format(ChatColor.AQUA + enchantment) + " " + romanLevel);
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.setLore(newLore);
                return itemMeta;
            }
        }
    Any idea why this isn't working?
     
  2. Not sure what this could be, but you should try debugging by using System.out.println() to show you if the code runs into the if statements.

    You could try using InventoryClickEvent}setCurrentItem(ItemStack) and Player#updateInventory() if you find that the if statements are running but the items aren’t updating.
     
  3. Sorry for the late response.

    I have already tried doing some debugging and it got all the way down to adding the enchantment to the item and said that it was adding it but it never actually added it. I can try what you sent me and I will let you know if it works.
     
  4. Enchantment lore is handled client side, you have to manually add the lore yourself. There is no way to get Mc to do it.
     
  5. I am adding the lore manually. I have an addEnchantment() method where I give it the item, enchantment, and level, and it returns the ItemMeta which I apply to the item.
     
  6. Okay, I figured it out. This is what I have to set the enchantment now:

    Code (Java):
                e.getWhoClicked().setItemOnCursor(null);
                ItemMeta meta = addEnchantment(weapon, bookName[0], romanToNumber(bookName[1]));
                newItem.setItemMeta(meta);
                e.setCurrentItem(newItem);
                p.updateInventory();
    Thanks for the help!
     
  7. Im wondering, how is it going on the plugin? I was searching for a plugin like this for a long time.