Solved Adding +1 level to enchantment

Discussion in 'Spigot Plugin Development' started by Joshuak52, Mar 4, 2020.

Thread Status:
Not open for further replies.
  1. Hey so I am trying to make a system where a player can add a level to their enchantment.
    The code I have now sends a message with their current level correctly but it's not adding the level like its supposed to

    Code (Text):
    ItemStack item = player.getItemInHand();
                    ItemMeta itemmeta = item.getItemMeta();
                    int level = item.getEnchantments().get(Enchantment.DIG_SPEED);
                    item.addUnsafeEnchantment(Enchantment.DIG_SPEED, level + Integer.valueOf(1));
                    item.setItemMeta(itemmeta);
                    player.sendMessage("" + level);
     
  2. Hi, sadly this was my original code but this also did not work
     
  3. Don't set item meta.
     
  4. Try to replace the item in hand with the item where you set the itemmeta
     
  5. Hi, thanks for the help @imDaniX that fixed it.

    Also, now I am having this issue. I want to remove a lore on picks but make sure to keep the other lores

    Code (Text):
    ItemStack item = player.getItemInHand();
                ItemMeta itemMeta = item.getItemMeta();
                itemMeta.getLore().remove(ChatColor.GOLD + "Mine Destroyer");
                itemMeta.getLore().remove(ChatColor.GOLD + "" + ChatColor.ITALIC + "Mine Destroyer");
                item.setItemMeta(itemMeta);
                Bukkit.getServer().getPlayer("Instigated").sendMessage(itemMeta + "");
    this does not work
     
  6. Get lore and store it as List<String>, change(remove your stuff), and use itemMeta.setLore(newLore).
    Also don't use player.getItemInHand() cuz it's deprecated - player.getInventory().getItemInMainHand() instead.
     
  7. Perfect! Thank you buddy
     
Thread Status:
Not open for further replies.