Solved Multiline lore

Discussion in 'Plugin Development' started by Matroxko, Jul 1, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Matroxko

    I have metod createItem() which create item with name, inventory slot and lore. I want to have lore for more than 1 line but idk how to make it
    Code:
    public static void createItem(Material material, Inventory inv, int Slot, String name, String lore) {
            
            ItemStack item  = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            ArrayList<String> Lore = new ArrayList<String>();
            Lore.add(lore);
            meta.setLore(Lore);
            item.setItemMeta(meta);
            inv.setItem(Slot, item);
    
            }
    And here I use the following method:
    Code:
    createItem(Material.DIRT, InvMenu, 9, "Magic Dirt", "Right Click - Heal yourself Left Click - Fill your hunger");
    And I want to make Right Click - heal yourself on the 1st line
    and Left Click - fill your hunger on the 2nd line

    I tried to use \n but it doesn't work
    I tried to use , between 1st and 2nd line but it doesn't work. Can anyone help me with that? Thanks
     
  2. Instead of this:
    Lore.add(lore);

    Use this:
    for(String l : lore)
    Lore.add(l);

    And change your lore parameter from String to String...
     
    Matroxko and InstanceofDeath like this.
  3. Offline

    Matroxko

    @knokko but i get error: Can only iterate over an array or an instance of java.lang.Iterable
    for(String l : lore)
    Lore.add(l);

    And what do you mean change String to String ? :)
     
  4. Offline

    Horsey

    Lore can be of as many lines as you like, just take a String[] as input, and iterate over each string and add it to Lore.
     
    Matroxko likes this.
  5. Offline

    Matroxko

    @Horsey can u explain it to me pls ? :)
     
  6. Offline

    Zombie_Striker

    @Matroxko
    [EDIT] You should only be getting that error if you have not changed the lore variable to a List yet. Change it to a List<String> to fix this problem.
     
    Matroxko likes this.
  7. Offline

    Matroxko

    So it should be like this ?
    Code:
    public static void createItem(Material material, Inventory inv, int Slot, String name, List<String> lore) {
            ItemStack item  = new ItemStack(material);
            ItemMeta meta = item.getItemMeta();
            meta.setDisplayName(name);
            List<String> Lore = new ArrayList<String>();
            for(String l : lore)
                Lore.add(l);
            meta.setLore(Lore);
            item.setItemMeta(meta);
            inv.setItem(Slot, item);
            }
    And how to use it in createItem() ?
    Code:
    createItem(Material.GLASS, inv, 12, "Name", "Lore");
    
    Because this is not working and idk how to make it work :)
     
  8. Offline

    Plugers11

    You put a single string not a list/array. Use Arrays.asList(line1, line2, line3) instead
     
    Matroxko likes this.
  9. Offline

    Matroxko

    So where should i put it in ?
     
  10. Offline

    Plugers11

    In createItem so your method would look like this ; createItem(Mat.GL, inv, 12, "name", Arrays.asList("hey", "that works", "hehe");
     
    Matroxko likes this.
  11. Offline

    Matroxko

    Oh thanks :D
     
    Plugers11 likes this.
Thread Status:
Not open for further replies.

Share This Page