List / for loop

Discussion in 'Spigot Plugin Development' started by WailTosti, Apr 14, 2017.

  1. I've created a for loop for List lores.
    That people can create new lores in the config.
    That works if you don't add color support.
    And i need color support, anyone do understand this and can help me?

    Code (Text):
        public void setupItem3(){
            item3 = new ItemStack(Material.valueOf(fm.getConfig("Inventories.yml").get("Inventories.Test.Items.3.Material")));
            ItemMeta meta = item3.getItemMeta();
            meta.setDisplayName(ChatUtil.format(fm.getConfig("Inventories.yml").get("Inventories.Test.Items.3.Name")));
            int lines = fm.getConfig("Inventories.yml").getInt("Inventories.Test.Items.3.Lore.Lines-Total");
            for (int i = 0; i < lines; i++)
            {
            List<String> lore = new ArrayList<>();
            lore.add(ChatUtil.format(fm.getConfig("Inventories.yml").get("Inventories.Test.Items.3.Lore.lore-" + (i + 1)).replace("%arrow%", "»").replace("&", "§")));
            meta.setLore(lore);
            item3.setItemMeta(meta);
            }
        }
     
  2. You can add color support by using
    ChatColor.translateAlternateColorCodes('&',string);
    Replace the string with the input from the yml and this will make a string with all color codes.
     
  3. Get the string list, use
    Code (Text):
     .stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
     
  4. What do you mean? Where must i put that?
     
  5. Java 7 has been dead for two years, time to update :p
     
    • Agree Agree x 2
  6. I use Java 8? Anyone that can help me for that version
     
  7. Put that method after your string list, this method returns a string list.

    For example
    Code (Text):

    List<String> memes = new ArrayList<>();
    memes.add("&6DOGE");
    memes = memes.stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
    Bukkit.broadcastMessage(memes.get(0));
     
    This returns DOGE
     
  8. It says 'cannot infer type arguments'
     
  9. Send the code you tried.
     
  10. Code (Text):
        public void setupItem3(){
            item3 = new ItemStack(Material.valueOf(fm.getConfig("Test.yml").get("Inventories.Compass.Items.3.Material")));
            ItemMeta meta = item3.getItemMeta();
            meta.setDisplayName(ChatUtil.format(fm.getConfig("
        public void setupItem3(){
            item3 = new ItemStack(Material.valueOf(fm.getConfig("Inventories.yml").get("Inventories.Compass.Items.3.Material")));
            ItemMeta meta = item3.getItemMeta();
            meta.setDisplayName(ChatUtil.format(fm.getConfig("Inventories.yml").get("Inventories.Compass.Items.3.Name")));
           int lines = fm.getConfig("Inventories.yml").getInt("Inventories.Compass.Items.3.Lore.Lines-Total");
           for (int i = 0; i < lines; i++)
           {
           List<String> lore = new ArrayList<>();
           lore.add(ChatUtil.format(fm.getConfig("Inventories.yml").get("Inventories.Compass.Items.3.Lore.lore-" + (i + 1)).replace("%arrow%", "»").replace("&", "§")));
           lore.add("&6DOGE");
           lore = lore.stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
           Bukkit.broadcastMessage(lore.get(0));
           meta.setLore(lore);
           item3.setItemMeta(meta);
           }
        }
    .yml").get("Inventories.Compass.Items.3.Name")));
            int lines = fm.getConfig("Inventories.yml").getInt("Inventories.Compass.Items.3.Lore.Lines-Total");
            for (int i = 0; i < lines; i++)
            {
            List<String> lore = new ArrayList<>();
            lore.add(ChatUtil.format(fm.getConfig("Inventories.yml").get("Inventories.Compass.Items.3.Lore.lore-" + (i + 1)).replace("%arrow%", "»").replace("&", "§")));
            lore.add("&6DOGE");
            lore = lore.stream().map(s -> ChatColor.translateAlternateColorCodes('&', s)).collect(Collectors.toList());
            Bukkit.broadcastMessage(lore.get(0));
            meta.setLore(lore);
            item3.setItemMeta(meta);
            }
        }
     
  11. Does Collectors.toList() need the list type? Like Collectors.<String>toList()? My guess would be it doesn't, but I admit I have no idea.