Solved Setting item lore from string list that supports color codes

Discussion in 'Spigot Plugin Development' started by DevPazzo, Feb 15, 2020.

  1. i use lambda
    #1 DevPazzo, Feb 15, 2020
    Last edited: Mar 13, 2021
  2. have you already solved your problem in moments from posting it or is the solved tag a mistake? If not it should just be getConfig().getStringList("Example.Apple.Lore") to retrieve it and then setting the lore would be the same as creating one basically.
    • Useful Useful x 1
  3. Where's your example of it not working, I can't just give it to you.
  4. Have you even looked at your code? You're making a new ArrayList then iterating over it. There's nothing in a new list.

    Anyway, you'd have to use getConfig() and get the string array. Then set the lore to that.
    • Friendly Friendly x 1
  5. I'm not gonna spoonfeed you code. Read the Javadocs then find the method you should use to get a string list, and just ItemMeta#setLore to that.

    ChatColor.translateAlternateColorCodes can also translate e.g. &a to the light green color code, so use that to allow color.
  6. ????? Your code makes no sense. You make a brand new list with nothing in it, then you iterate over it, adding what's in it (nothing) to the list again????

    The next line, meta.setLore, should be fine though. Does that work?
  7. Lines 25-28 do nothing, is what I'm saying. You make a new list, but you do nothing with it. That code is probably left over from another attempt at coding that.

    But does the item you get actually have any lore? Because it should do, because of line 29.
  8. This is the basic theory of how to set a lore with colors using java 8+.
    Code (Java):
    List<String> lore= configLore; // get string list from config
    lore = -> ChatColor.blablabla('&', line)).collect(Collectors.toList()); // map all elements in the list to a element with translated color codes, using the already mentioned ChatColor class
    itemMeta.setLore(lore); // set the lore in the item meta
  9. i use lambda you?
    #9 DevPazzo, Feb 15, 2020
    Last edited: Mar 13, 2021
  10. You haven't actually included the error, but from what I can guess something is null. Try this:
    Code (Java):
    and check what line has the error now.

    Beelzebu posted his solution before my post, but it works. I would say that it's kind of unnecessarily complex though - why not just iterate over the list, instead of stream and map?
  11. "Not work"? Is it the same error again? Is it a different error? Is the line number different?
  12. I personally find it easier to use stream and map, but as you said it can be done just iterating over the elements in the list like this
    Code (Java):
    List<String> lore = configLore;
    for (int i = 0; i < lore.size(); i++) {
        lore.set(i, ChatColor.blablabla('&', lore.get(i)));
    You can also use raw arrays
    Code (Java):
    String[] lore = configLore.toArray(new String[0]);
    for (int i = 0; i < lore.length; i++) {
        lore[i] = ChatColor.blablabla('&', lore[i]);
    In the end the result is the same.
  13. If I'm not wrong, wouldn't .steam() and .map() be slower? I love the syntax and single line of your solution but it might not be as efficient.
  14. May be, but you're not editing the lore of an item every millisecond, so in this case speed should not matter.