Update keys in config.

Discussion in 'Plugin Development' started by TheE, Jul 19, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    TheE

    I have a config to store different dates and values per date. You can add more dates with values1 and 2, etc.
    Code:
    foo:
      21-07-2012 20:00:
        value1: abc
        value2: def
      19-07-2012 21:15:
        value1: ghi
        value2: jkl
    From this I create an array list of foo objects which is sorted by date. Now I'm searching for a possibility to write these sorted objects back into the config file.
    getConfig().addDefault(...) does nothing, probably because the values are all present in the config. getConfig().set(...) only overrides the first section and deletes the rest.

    Any help would be appreciated.
     
  2. getConfig().set(...) shouldn't alter other sections than the passed section, can you give the failed code for it/
     
  3. Offline

    TheE

    Sure.
    Code:
    //f is my sorted array list of foo objects.
    for (int i = 0; i < f.size(); i++){
                getConfig().set("foo", f.get(i).getUnformatedDate());
                getConfig().set("foo." + f.get(i).getUnformatedDate() + ".value1", f.get(i).getValue1());
                getConfig().set("foo." + f.get(i).getUnformatedDate() + ".value2", f.get(i).getValue2());
            }
            saveConfig();
    After running this, only the latest date and its values are present in the config:
    Code:
    foo:
      21-07-2012 20:00:
        value1: abc
        value2: def
     
  4. Offline

    pzxc

    Maybe it's having a problem with the space in the date? Just a guess.
     
  5. it is this line that causes the removing
    getConfig().set("foo", f.get(i).getUnformatedDate());
    it chances
    Code:yaml
    1. foo:
    2. 21-07-2012 20:00:
    3. value1: abc
    4. value2: def
    5. 19-07-2012 21:15:
    6. value1: ghi
    7. value2: jkl
    to
    Code:yaml
    1. foo: 21-07-2012 20:00
     
  6. Offline

    pzxc

    Oh yeah, good catch. Just remove that line. When you set value1 and value2 it will create the nodes above it automatically.
     
  7. yes, you can even do set("a.really.long.name.for.an.node.You.know.it","?")
    and it creates them all automaticly
     
  8. Offline

    TheE

    Thanks, that fixed the overwriting issue, but does not do what I want it to. I am searching for a possibility to sort the whole config per date, so that the config exemple given in the first post looks like this afterwards:
    Code:
    foo:
      19-07-2012 21:15:
        value1: ghi
        value2: jkl
      21-07-2012 20:00:
        value1: abc
        value2: def
    getConfig().set(...) doesn't seem do do this, since the the whole path is already present in the config. I thought of cleaning the config before writing, but I have no idea how I could do this...

    Any ideas how to archive such a sorting in the config itself?
     
  9. I think you cant do it, because bukkit use an hashmap to store the key-value bindings, and the order of an hashmap is created by the hashcode, whits you can chance
    you can try putting it in a list if you want to keep the order
     
Thread Status:
Not open for further replies.

Share This Page