GUI Item For Loop

Discussion in 'Spigot Plugin Development' started by JC_Plays_MC, May 21, 2015.

  1. A quick explanation of what I'm trying to do is have the code grab each "item" and its other values from the config file. I have it setup to work with one item and obviously do not want to have the code repeated 54 times (Equivalent of a double chest). I have tried the following however my loop does not work.

    Code in plugin:
    Code (Text):
        if(cmd.getName().equalsIgnoreCase("warp")) {
                if(sender instanceof Player){
                Player p = (Player) sender;
                String chestName = getConfig().getString("ChestName");
                List<String> itemList = getConfig().getStringList("GUI");
             
                Inventory inv = Bukkit.createInventory(null, getConfig().getInt("ChestSize"), (ChatColor.translateAlternateColorCodes('&' ,chestName)));
             
                for (String listString : itemList){
             
                    String itemPermission = getConfig().getString("GUI"+listString+"Permission", "");
                    String permissionItem = getConfig().getString("GUI"+listString+"PermissionItem", "");
                    String permissionName = getConfig().getString("GUI"+listString+"PermissionName", "");
                    List<String> permissionLore = getConfig().getStringList("GUI"+listString+"PermissionLore");
                    String permissionEnchant = getConfig().getString("GUI"+listString+"PermissionEnchant", "");
                    int permissionEnchantLevel = getConfig().getInt("GUI"+listString+"PermissionEnchantLevel");
                 
                    int itemSlot = getConfig().getInt("GUI"+listString+"ItemSlot");
             
                if(p.hasPermission(itemPermission)){
                    ItemStack itemP = new ItemStack(Material.getMaterial(permissionItem));
                    ItemMeta itemPMeta = itemP.getItemMeta();
                    itemPMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&' , permissionName));
                 
                    ArrayList<String> itemPLore = new ArrayList<String>();
                    if(permissionLore.isEmpty()){
                        itemPMeta.getLore();
                    }
                    else {
                        for (String string : permissionLore) {
                            itemPLore.add(ChatColor.translateAlternateColorCodes('&', string));
                        }
                    }
                 
                    itemPMeta.setLore(itemPLore);
                    itemP.setItemMeta(itemPMeta);
                    if(permissionEnchant.isEmpty()){
                        itemPMeta.getEnchants();
                    }
                    else {
                        itemP.addUnsafeEnchantment(Enchantment.getByName(permissionEnchant), permissionEnchantLevel);
                    }
                    inv.setItem(itemSlot, itemP);
    Config.yml (for values):
    Code (Text):
    ChestSize: 9   ##Item slots in GUI can be either 9,18,27,36,47,56
    ChestName: WarpGUI   ##The name of the GUI displayed when you enter /warp

    GUI:
      0:
        Permission: example.permission
        PermissionItem: DIAMOND
        PermissionName: '&b&lDiamond'
        PermissionLore:
        - '&aFirst line of lore'
        - '&aSecond line of lore'
        PermissionEnchant: DURABILITY
        PermissionEnchantLevel: 1
        PermissionCommands:
        DefaultItem: COAL
        DefaultName: '&8&lCoal'
        DefaultLore:
        - '&aFirst line of lore'
        - '&aSecond line of lore'
        DefaultEnchant: DURABILITY
        DefaultEnchantLevel: 1
        DefaultCommand:
        ItemSlot: 1
    So the '0' is the part that I am looking to access from the config in a loop so multiple values ex. '0', '1', '2' would all be added to the GUI. Any help would be much appreciated :)
     
    #1 JC_Plays_MC, May 21, 2015
    Last edited: May 21, 2015
  2. Hm, not sure if this helps but you have
    Code (Text):
     String itemPermission = getConfig().getString("GUI"+listString+"Permission", "");
    Notice you're lacking the .'s so this won't return anything useful, you need
    Code (Text):
     String itemPermission = getConfig().getString("GUI."+listString+".Permission", "");
     
    • Agree Agree x 1
  3. You don't even need the +'s , it can be in one string
     
  4. I see what you're saying, I'll try that and see if it works.
     
  5. So after doing a bit more testing it appears that it has something to do with how I am accessing the the string list data from the code:

    Code (Text):
    List<String> itemList = getConfig().getStringList("GUI");
    As I tried changing that to a separate list with each of the items like so and it worked fine, however I would prefer if I wouldn't have to register each item in a separate list.

    Code (Text):
    List<String> itemList = getConfig().getStringList("Test");
    Code (Text):
    Test:
    - 0
    - 1
     
    Any thoughts?
     
  6. You're not attempting to access a list.
    You want to access the 0, 1, 2 etc, which contains more data inside
    Example of a list:
    Code (Text):
    yml-list:
    - hello
    - there
    To get the data inside "GUI" you need to use getConfigurationSection(String) and getKeys(boolean)
     
  7. @HeyImErik

    I've yet to work with this before so I apologize for the questions but would it look something like this?

    Code (Text):
                List<String> itemList = getConfig().getConfigurationSection("GUI").getKeys(true);
             
                Inventory inv = Bukkit.createInventory(null, getConfig().getInt("ChestSize"), (ChatColor.translateAlternateColorCodes('&' ,chestName)));
             
                for (String listString : itemList){
     
  8. @JC_Plays_MC No,

    Code (Text):
    Set<String> itemList = getConfig().getConfigurationSection("GUI").getKeys(false);
     
    • Winner Winner x 1
  9. Thank you so much :)