Serialisation for saving to config file

Discussion in 'Spigot Plugin Development' started by spinnekoppie, May 12, 2016.

  1. Hi folks
    I'm not sure whether I'm doing something wrong or whether I misunderstand how the serialisation works.

    I have a Hashtable that I want to save to a file. It all works well except for the way the key is saved. For the key I want to use the location of a block, i.e. the x, y and z coordinates. I tried a few things but all resulted in the same thing which is as below:
    Code (Text):

    sectionname:
      '8':
        '0_62':
          0_-47:
            '0':
              ==: SerializableClass
              location: 8.0_62.0_-47.0
              material: COAL_BLOCK
              player: playername
     
    I am using the location as a key but formatted as a string, eg. "8,62,-47" and I also tried "8_62_-47". However, it is always saved as above and thus, when I try to read the configuration it just used the 8 as the key and I end up with a nullpointer exception.

    The code for saving:
    Code (Text):

        public static void save() {
            try {
                // Create a new YAML configuration
                YamlConfiguration config = new YamlConfiguration();

                // Add the hashmap to the config by creating a section
                config.createSection("sectionname", hashtable_stuff);
                Set<String> keys = hashtable_stuff.keySet();
                for (String key: keys) { // I did this to see if the key is actually what I want it to be and it is.
                    System.out.println(key);
                }

                // Write the configuration to our save file
                config.save(new File(plugin.getDataFolder(), "myconfig.yml"));
            } catch (Exception saveFailed) {
                plugin.getLogger().severe("Save Failed!" + saveFailed);
            }
        }
     
    As I said. Everything works perfectly except for the way the key is saved. I edited the config file by hand to be as below and then everything works as expected:
    Code (Text):

    sectionname:
      '8,62,-47':
              ==: SerializableClass
              location: 8.0_62.0_-47.0
              material: COAL_BLOCK
              player: playername
     
    What does work is converting the location to a string in the format "############", where four #'s represent the three digit location co-ordinate with a sign. I then just use substring to get four characters at a time and convert that to a double.

    The question though remains. So why is the key saved as it is when there are '_' or ',' in it? What do I need to do to change it?
     
  2. MiniDigger

    Supporter

    the config is only made for serialisation of a map<string,object>, its not made to for using a location as a key.
    Maybe you should reiterate on your config design here.
    why do you need to have the location as a key?
     
  3. I'm not sure what you mean by your first sentence. A hashtable implements the Map<K,V> interface and a map is for mapping a key to an object. So it should be fine. The class that I use for objects implements ConfigurationSerializable. All this works fine and is not really my question.

    What I want to know is why the config.save() method, if the string I use for the key is something like "a,b,c" or "a_b_c", parses and stores the key hierarchical ? If I use ' a b c' it works fine.


    The rest is not of consequence right now.

    BTW MiniDigger, how do you manage to ALWAYS be on-line? Do you ever sleep or do you Spigot even when you are asleep??? :)
     
  4. MiniDigger

    Supporter

    its 16:00 here ^^
    I get bored at work and hop on the forum sometimes and I got some rss feeds setup ;)
    For you question, idk who bukkit handles path names. I know that it splits on '.' If have no idea if other characters then a-zA-Z0-9 are allowed.
    What I found about maps is this: http://wiki.bukkit.org/Configuration_API_Reference#HashMaps maybe it helps you with your problem.