1.15.2 Cannot add enchantment to itemstack

Discussion in 'Spigot Plugin Development' started by xUltraGaming, Feb 17, 2020.

  1. I'm trying to add a custom enchantment to an itemstack on an InventoryClickEvent. Check the spoiler for the inventoryclickevent code

    Code (Java):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getWhoClicked() instanceof Player) {
            Player p = (Player) e.getWhoClicked();
           
            if ((e.getCursor() != null) && (e.getCurrentItem() != null)) {
            ItemStack item = e.getCursor();
         
            if ((item.getType() == Material.ENCHANTED_BOOK) && (item.getItemMeta().hasDisplayName()) && (item.getItemMeta().getEnchants() != null)
            && (item.getItemMeta().getEnchants().size() == 1) && (e.getCurrentItem() != null) && (e.getCurrentItem().getType() != Material.AIR)) {
                Map<Enchantment, Integer> ench = new HashMap<Enchantment, Integer>();
                ench.putAll(item.getItemMeta().getEnchants());
               
                for (Map.Entry<Enchantment,Integer> entry : ench.entrySet()) {
                    p.sendMessage(ChatColor.GRAY + "Enchant Name: " + ChatColor.WHITE + entry.getKey().getName());
                    p.sendMessage(ChatColor.GRAY + "Enchant Level: " + ChatColor.WHITE + entry.getValue());
                    p.sendMessage(ChatColor.GRAY + "Cursor Item: " + ChatColor.WHITE + item.getType().toString());
                    p.sendMessage(ChatColor.GRAY + "Current Item: " + ChatColor.WHITE + e.getCurrentItem().getType().toString());
                    vCustomEnchants.addEnchantToItem(p, e.getCurrentItem(), entry.getKey(), entry.getValue());
                }
                }
                }
             
            }
        }

    The actual add method is called from another class. View the next spoiler to see the code.

    Code (Java):
    public static void addEnchantToItem(Player p, ItemStack i, Enchantment e, int level) {
            if (e.canEnchantItem(i)) {
                if (!i.getEnchantments().toString().contains(e.getName())) {
                    if (level <= e.getMaxLevel()  && level >= e.getStartLevel()) {
                ItemMeta im = i.getItemMeta();
                im.addEnchant(e, level, false);
                if (im.hasLore()) {
                    List<String> lore = im.getLore();
                    lore.add(ChatColor.translateAlternateColorCodes('&', "&7" + e.getName()
                    + " " + vCustomEnchants.convertIntToRomanNumerals(level).toString()));
                    im.setLore(lore);
                    i.setItemMeta(im);
                    p.sendMessage(ChatColor.GREEN + "Item successfully enchanted!");
               
            } else if (!(im.hasLore())) {
                List<String> lore = new ArrayList<>();
                lore.add(ChatColor.translateAlternateColorCodes('&', "&7" + e.getName()
                + " " + vCustomEnchants.convertIntToRomanNumerals(level).toString()));
                im.setLore(lore);
                i.setItemMeta(im);
                p.sendMessage(ChatColor.GREEN + "Item successfully enchanted!");
            } else {
        }
        } else {
            p.sendMessage(ChatColor.DARK_GRAY + "The enchant level is too high!"
            + "\nMin Level: " + ChatColor.DARK_RED + e.getStartLevel() + ChatColor.RED
            + "\nMax Level: " + ChatColor.DARK_RED + e.getMaxLevel());
        }
        } else {
            p.sendMessage(ChatColor.RED + "This enchant has already been applied to this item!");
        }
        } else {
            p.sendMessage(ChatColor.RED + "This enchant is not compatible with this item!");
        }
    }

    So, whenever I try to place an enchanted book on an item, the message "Item successfully enchanted" appears. However, the enchantment AND the lore do not. The addEnchantToItem() method works fine because I made a command to manually add enchants, which leads me to believe that the problem is located in the first method. If anyone could help me out, that'd be great. Thanks!
     
  2. make new method:
    public static ItemStack addEnchantToItem(ItemStack item, ItemStack enchantedBook){}
    it will return you the item with all the book's enchantments.

    and just put the new item you will get on the clicked slot,
    meaning replacing the old itemstack with the new itemstack the method gave you.
     
  3. @PvPNiK Here is the updated method.

    Code (Java):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (e.getWhoClicked() instanceof Player) {
            Player p = (Player) e.getWhoClicked();
         
            if ((e.getCursor() != null) && (e.getCurrentItem() != null)) {
            ItemStack book = e.getCursor();
            ItemStack item = e.getCurrentItem();
         
            if ((book.getType() == Material.ENCHANTED_BOOK) && (book.getItemMeta().hasDisplayName()) && (book.getItemMeta().getEnchants() != null)
            && (book.getItemMeta().getEnchants().size() == 1) && (e.getCurrentItem() != null) && (e.getCurrentItem().getType() != Material.AIR)) {
                ItemStack i = vCustomEnchants.addEnchantToItem(book, item);
             
                p.getInventory().remove(e.getCurrentItem());
                p.getInventory().remove(e.getCursor());
                p.getInventory().addItem(i);
             
                p.sendMessage(ChatColor.GREEN + "Item successfully enchanted!");
            }
            }
            }
        }

    The enchant transferring works but now I can't remove the old book and item even though I used the lines:

    Code (Java):

    p.getInventory().remove(e.getCurrentItem());
                p.getInventory().remove(e.getCursor());
     
    Do you have any idea why this is?
     

  4. if you are using
    You could always try removing the book from the players inventory in this method.
    change:
    Code (Text):
    public static ItemStack addEnchantToItem(ItemStack item, ItemStack enchantedBook){

    }
    to:

    Code (Java):
    public static ItemStack addEnchantToItem(ItemStack item, ItemStack enchantedBook, Player player){

    // use your code from the addEnchantToItem() method, but also add in player.getInventory().remove(enchantedBook);

    //and player.getInventory().remove(item) if you are creating a new ItemStack and giving the player that.


    }
    and see if that does it.

    you might also try replacing

    Code (Java):
    p.getInventory().remove(e.getCursor());
    with

    Code (Java):
    p.getInventory().removeItem(e.getCursor());
     
  5. I've tried both of those methods, but they don't work. Also, I've tried cancelling the event and adding the items from there but its still the same result.