Solved Grabbing Item from Configuration

Discussion in 'Spigot Plugin Development' started by Willim, Feb 9, 2018.

  1. I grab the item from the configuration and that works perfectly fine but when it comes to adding the lores to the item it looks like the following:

    Code (Text):

    [Lore, lore1, even more lores]
     

    How can I make it so it looks like this:
    Code (Text):

    Lore
    Lore1
    Even more lores
     

    My code for grabbing the helmet:
    Code (Text):

    String helmetType = config.getString("Kits." + name + ".Armor.helmet.type");
    String helmetName = config.getString("Kits." + name + ".Armor.helmet.name");

    List<String> helmetLore = config.getStringList("Kits." + name + ".Armor.helmet.lore");
    List<String> helmetEnchants = config.getStringList("Kits." + name + ".Armor.helmet.enchants");

    ItemStack helmet = new ItemStack(Material.matchMaterial(helmetType));
    ItemMeta helmetMeta = helmet.getItemMeta();

    if (helmetName != null) {
        helmetMeta.setDisplayName(color(helmetName));
    }

    if (!helmetLore.isEmpty()) {
        helmetMeta.setLore(Arrays.asList(color(helmetLore.toString())));
    }

    if (!helmetEnchants.isEmpty()) {
        for (String s : helmetEnchants) {
            String[] idAndEnchants = s.split(":");
            helmetMeta.addEnchant(Enchantment.getByName(idAndEnchants[0].toUpperCase()), Integer.parseInt(idAndEnchants[1]), true);
        }

    }

    helmet.setItemMeta(helmetMeta);
    player.getInventory().setHelmet(helmet);
     
     
  2. This line explains why. You simply need to pass in a List<String> to the setLore method, but you get a String representation of the List which is a single string looking like "[item, item, item, item]". You then do a lot of other weird things like casting it back to an array as a list or something? What are you trying to achieve, why doesn't "helmetMeta.setLore(helmetLore);" not work for you?
     
  3. I would have used "helmetMeta.setLore(helmetLore);" but I need to translateAlternateColorCodes to allow colour codes but that requires a string.
     
  4. Just do it simple.
    Configuration file:
    Code (Text):
    lore:
    - 'I am the first line!'
    - 'I am the second line!'
    And for the ItemMeta.
    Code (Text):
    meta#setLore(config.getStringList("lore"));
     
  5. I know that, that's not my issue currently. My issue is that if I want to allow for colour codes, then ChatColor.translateAlternateColorCodes(); needs to have a string inputted not a StringList
     
  6. Simply loop over them. I'll give you a snippet of my code how I handle that.
    Code (Text):
    public static List<String> getStringList(String path) {
    List<String> stringlist = new ArrayList<String>();
    for (String list : Maincore.getInstance().getConfig().getStringList(path)) {
    stringlist.add(ChatColor.translateAlternateColorCodes('&', list));
    }
    return stringlist;
    }
     
    • Like Like x 1
    • Agree Agree x 1
    • Winner Winner x 1
  7. This code helped me out of a problem, thank you very much.
     

Share This Page