How to access an integer list in a config.yml for a GUI

Discussion in 'Spigot Plugin Development' started by PCPSells, Jun 16, 2018.

  1. My other issue was resolved, but this is my current dilemma:
    I need to access this new list of integers I've created for my reports.yml.
    Code -
    Code (Java):
    Player p = (Player) sender;
                Inventory inv = Bukkit.createInventory(null, 54, ChatColor.translateAlternateColorCodes('&', getConfig().getString("GUI.Name")));
                ConfigurationSection c = yamlFile.getConfigurationSection("reports");
                int nextKey = (c == null)?0:c.getKeys(false).size();
                @SuppressWarnings("unused") /// this is here as I know I have to use this list in some way.. but how? ;-;
                List<?> reports =  yamlFile.getList("reports." + nextKey);
                ItemStack itemStack = new ItemStack(Material.valueOf(getConfig().getString("GUI.Material")));
                ItemMeta meta = itemStack.getItemMeta();
                meta.setDisplayName("");
                itemStack.setItemMeta(meta);
                inv.addItem(itemStack.clone());
                p.openInventory(inv);
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.Note")).replace("%prefix%", ChatColor.translateAlternateColorCodes('&', getConfig().getString("messages.Prefix"))));
                return true;
    Code I'm using to make the data:
    Code (Java):
    ConfigurationSection c = yamlFile.getConfigurationSection("reports");
              int nextKey = (c == null)?0:c.getKeys(false).size();
              yamlFile.set("reports." + nextKey + ".Reporter", sender.getName());
              yamlFile.set("reports." + nextKey + ".Reported", args[0]);
              yamlFile.set("reports." + nextKey + ".Reason", str.toString());
                try {
                    yamlFile.save(file);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
    What I'm trying to do is be able to set the display name to a line in each of the items.
    Current the GUI opens just fine, but only with one paper no matter the amount of reports (as it isn't hook correctly, obviously) and I'm trying to get the lines out of the arrays.

    Format:
    Code (YAML):
    reports:
        '0'
    :
            Reporter
    : name
            Reported
    : othername
            Reason
    : 'reason provided '
     
    #1 PCPSells, Jun 16, 2018
    Last edited: Jun 16, 2018
  2. Create an internal counter for reports and on report increase that counter and save the data to the counter's value. After a restart of the plugin you get the counter's value by counting the entries in the config.

    Code (Java):
    int counter = getConfig().getConfigurationSection("reports").getKeys(false).size()

    getConfig().set("reports." + counter, yourData);
     
  3. Strahan

    Benefactor

    Well.. you could get the configuration section reports and inc one to the getkeys size and use that as the next number to place.

    EDIT: ninja'd, lol. Though what's the meaning of setting a counter entry in the config when you can just go by the key size as you did in the first line? Seems pointless. Also you aren't protecting against null in that example. I'd do:

    Code (Text):
    ConfigurationSection c = getConfig().getConfigurationSection("reports");
    int nextKey = (c == null)?0:c.getKeys(false).size();
    getConfig().set("reports." + nextKey + ".report", "xx");
    saveConfig();
     
    • Winner Winner x 1
  4. Depending on how you have your reports setup, you might have entries that aren't actually reports. If you don't want to have to work around this, you could just do a while loop starting from 0 until there aren't any more entries. Alternatively you could have something like "CurrentReport" as an entry and use that, and all you'd have to do is increment it once you make a new report.
     
  5. Works perfectly, thank you! Another question though, how would I format this layout to where I could use the list of integers to make items in a GUI?

    Current code:
    Code (Java):
    Player p = (Player) sender;
                Inventory inv = Bukkit.createInventory(null, 54, ChatColor.translateAlternateColorCodes('&', getConfig().getString("GUI.Name")));
                List<String> reports = (List<String>) yamlFile.getList("reports");
                ItemStack itemStack = new ItemStack(Material.valueOf(getConfig().getString("GUI.Material")));
                ItemMeta meta = itemStack.getItemMeta();
                for (String string : reports) {
                    meta.setDisplayName(string);
                    itemStack.setItemMeta(meta);
                    inv.addItem(itemStack.clone());
                } p.openInventory(inv);
                 return true;
    I know that it obviously has something to do with "List<String> reports = (List<String>) yamlFile.getList("reports");" but how I would I make that work with each new list properly?? <3
     
    #5 PCPSells, Jun 16, 2018
    Last edited: Jun 16, 2018
  6. Well what @Strahan said works fine. Just need to know how to get the information into the previous method I stated above.
     
  7. Bump, main thread message changed.
     
  8. You'll want to use YamlConfiguration#getKeys(boolean deep); to get the keys.

    the deep generally is false, basically if deep is true then it iterates through all the keys, Eg:

    Code (Text):

    Reports:
      '1':
        Reporter: 'MSWS'
        Reason: 'TEST'
      '2':
        Reporter: Notch
        Reason: 'ABC'
     
    if you do getConfigurationSection("Reports").getKeys(false);, then it'd result in 1 and 2. However if deep was true, then it'd result in
    1, 1.Reporter, 1.Reason, 2, 2.Reporter, and 2.Reason