Solved Adding Lore Line to an Existing Item

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 24, 2016.

  1. I'm trying to add a new lore line to an existing item in a player's inventory. This is my code so far, but gives me errors. I can't think of a way to get List<String> to somehow work in setLore().
    Code (Text):
           
            List<String> loreCurrent = event.getCurrentItem().getItemMeta().getLore();
            String[] loreCurrent1 = loreCurrent.toArray(new String[0]);
            String enchantName = event.getCursor().getItemMeta().getDisplayName();
        event.getCurrentItem().getItemMeta().setLore(loreCurrent1, enchantName);
     
    Thanks, Max.
     
  2. MiniDigger

    Supporter

    get the itemmeta and save it.
    get the lore and save it
    change the lore
    set the lore for the meta
    set the meta for the item
     
    • Like Like x 1
  3. I've got this so far, but I'm not sure how I would go about adding a String to a List<String>.
    Code (Text):
            ItemStack currentItem = event.getCurrentItem();
            ItemMeta currentItemItemMeta = currentItem.getItemMeta();
            String name = currentItemItemMeta.getDisplayName();
            List<String> currentItemLore = currentItemItemMeta.getLore();
            List<String> currentItemLore1 = currentItemLore, name;
            currentItemItemMeta.setLore(currentItemLore1);
            currentItem.setItemMeta(currentItemItemMeta);
     
  4. MiniDigger

    Supporter

    read about java collections
     
  5. First of all check if it really has Lore. I will give you an example how to add a line to lore.
    Code (Java):
    ItemStack stack = ..... // Your itemstack
    ItemMeta meta = stack.getItemMeta(); // ItemMeta of the itemstack.
    if (meta.hasLore()) { // Checking if it already has lore.
    List<String> lore = meta.getLore();
    lore.add("This is a line"); // Adding a line.
    meta.setLore(lore); // Setting lore.
    stack.setItemMeta(meta); // Setting ItemMeta.
    }
     
    • Like Like x 1
  6. I've tried this out, but it doesn't give it a lore, it just creates a new empty line in the lore. Here is my code:
    Code (Text):
            ItemStack currentItem = event.getCurrentItem();
            ItemMeta currentItemItemMeta = currentItem.getItemMeta();
            String name = currentItemItemMeta.getDisplayName();
            String name1 = ChatColor.stripColor(name);
         
            if (currentItemItemMeta.hasLore()) {
                List<String> lore11 = currentItemItemMeta.getLore();
                lore11.add(name1);
                currentItemItemMeta.setLore(lore11);
                currentItem.setItemMeta(currentItemItemMeta);
            } else {
                currentItemItemMeta.setLore(Arrays.asList(name1));
                currentItem.setItemMeta(currentItemItemMeta);
            }
     
  7. The display name is probably empty, only renamed items have a display name
     
    • Like Like x 1
  8. Another dumb mistake I've made xD. I was checking for the wrong displayer name. Its all working now, thanks for the help!