Update Inventory with StringList in Config

Discussion in 'Spigot Plugin Development' started by nflug, May 26, 2015.

  1. Hey I try to update lores in my Inventory but it dont work here my code:

    Config replacer:


    Code (Text):
    public void variable(List<String> lore, Player p){
             for(int i = 0; i < lore.size(); i++){

               String name = p.getName();
               lore.set(i, ChatColor.translateAlternateColorCodes('&', lore.get(i).replaceAll("%name", name)));
           
             }
    }

    StringList:
    Code (Text):
    public List<String> infolore = getConfig().getStringList("Profile.Inventory.Info.Lore");
    InventoryUpdater:

    Code (Text):
    lores = new BukkitRunnable() {
       
           [USER=32110]@Override[/USER]
           public void run() {

             String invinfodisname = plugin.getConfig().getString("Profile.Inventory.Info.ItemName");
       
             invinfodisname = color(invinfodisname);
             (color(name) is only ChatColor.translate....)
         
             ItemStack info = new
    ItemStack(Material.getMaterial(plugin.getConfig().getString("Profile.Inventory.Info.ItemID")));
             ItemMeta infometa = info.getItemMeta();
             infometa.setDisplayName(invinfodisname);
             variable(plugin.infolore, p);
             infometa.setLore(plugin.infolore);
             info.setItemMeta(infometa);

             inv.clear();
             p.updateInventory();
         
             inv.setItem(11, info);
             p.updateInventory();

           }
         }.runTaskTimer(plugin, 20, 5);

         p.openInventory(inv);
       }
    Lores from the Stringlist on items will work but I cant update the lores...
    Can anyone help me ?

    MfG nflug
     
    #1 nflug, May 26, 2015
    Last edited: May 26, 2015
  2. So there is %name in every item lore? Try "replace()" instead of "replaceAll()":

    Code (Text):
    public void variable(List<String> lore, Player p){
    for(int i = 0; i < lore.size(); i++){

    String name = p.getName();
    lore.set(i, ChatColor.translateAlternateColorCodes('&', lore.get(i).replace("%name", name)));

    }
    }
     

  3. Thank you Friwi, but it dont work :/
     
  4. When you modified your lore containing %name once it contains for example "Friwi" instead. Then you got the problem that it wont update ever again. You should create a copy of plugin.infolore before processing it through variable() and then use the copy as a lore.

    Hope this helps
     
  5. I know what you mean, but I dont know how to create a copy of my StringList :/
     
  6. Code (Text):
    public static List<String> copyList(List<String> list) {
            List<String> clone = new ArrayList<String>(list.size());
            for(String item: list) clone.add(item+"");
            return clone;
        }
     
    • Winner Winner x 1
  7. Code (Text):
             ...
             ItemMeta infometa = info.getItemMeta();
             infometa.setDisplayName(invinfodisname);

             List<String> l = copyList(plugin.infolore);

             variable(l, p);
             infometa.setLore(l);
             info.setItemMeta(infometa);
             ...
     
    • Winner Winner x 1
  8. Thanks you :)! I try it later
     
  9. It's works! Thank you so much <33