Configuration, Getting the next key after a key.

Discussion in 'Spigot Plugin Development' started by MCDoodler, May 8, 2017.

  1. I'm working on a plugin that functions as the servers only plugin
    and i need help getting a key after a key

    say something like
    Default: 2193
    Group2: 1023

    how would I check what is the next key after Default (it's for a rank up system)
  2. Why not just keep track of the next rank inside of the rank block (e.g. the "Default" block)? If the implementation uses a HashMap and not a LinkedHashMap (or other ordered map) at any point you've effectively ruined your hierarchy.
  3. You can do


    This will return the keys in the section groups.
  4. Does not answer the question.
  5. You're wrong.
    He wants to get the key after "Default", so he just need to get the next element out of the array returned by this.getConfig().getConfigurationSection("Groups").getKeys(false);
  6. Code (Java):
    public static void main(String... args) {
        Map<String, String> test = new HashMap<>(2);
        test.put("one", "one");
        test.put("two", "two");
        test.put("three", "three");
    Code (Text):
    {two=two, one=one}
    {two=two, three=three, one=one}
    What comes after two? Is it one?
  7. Make a list, or map/hashmap of the keys, then do a for loop and check if key equals Default, and if it does, just add 1 and you'll get the next value. That is how i might do it, but i just thought of this on the spot
  8. Why using hashmaps?
    Also you can do a simple for to loop through all your keys
    for (String gr: this.getConfig().getConfigurationSection("Groups").getKeys(false)) {
    .... code
    // gr is each String of the Config section:
    // Default,Group2...

    so just check if the string is Default or whatever you want to something like
    if (gr.equalsIgnoreCase("Default"){

    or you can even just take the numbers

    int id = this.getConfig().getInt("Groups." + gr);
  9. Ask snakeyaml.

    Bukkit uses a LinkedHashMap internally for MemorySection (impl for ConfigurationSection most people work with) but the default Composer it uses from snakeyaml uses standard HashMap. Ordering is not guaranteed for all files (or at least, not the same upon loading. It will not change once loaded unless removed).

    You can use the low-level api for snakeyaml to set your own composer to utilize an ordered map:

    Or just put the rank in a sub block which will point to the appropriate upgrade.

    Code (YAML):
    : Member
    : 2193
    : 1023
    I'd prefer the latter. Not only will it not break from your yaml parser randomly, but it also is a stringent way of defining exactly what is the next group, and there is no confusion.
  10. @MCDoodler what does your config look like? post in Code brackets(YAML obviously)