Bukkit configuration file won't keep properties in an alphabetical order

Discussion in 'Plugin Development' started by shadowmax507, Sep 11, 2011.

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

    shadowmax507

    Well, I've got a problem with the following code:

    Code:
    Vector<String> messageIds = new Vector<String>();
    messageIds.addAll(_defaultMessages.keySet());
    Collections.sort(messageIds);
    
    for (int i = 0; i < messageIds.size(); i++)
    {
         String id = messageIds.get(i);
    
         String messageNode = parentNode + "." + id;
    
         if(_messages.containsKey(id))
         {
                config.setProperty(messageNode, _messages.get(id));
    
                continue;
         }
    
         config.setProperty(messageNode, _defaultMessages.get(id));
    }
    
    (_defaultMessages is a HashMap<String, String>)

    As you can see, I sorted all IDs before adding them to the YAML configuration file. When it gets saved however, all values appear to be no longer in any order at all.
    Can somebody please explain me what I'm doing wrong? Or isn't it possible to save configurations with properties sorted alphabetically?


    EDIT: Fixed wrongly posted code.
     
  2. Offline

    garbagemule

    I forget if it's Configuration or the snakeyaml emitter that discards all ordering of the actual files. The only way you can force it to appear in a specific order is to set a property, then config.save(), then set another property, then config.save(), etc. It's not very pretty, but it does work if you really, really want it in a certain order.

    Edit: Ordering isn't actually "discarded", it's just changed to fit the sorting algorithm of the data structure (some kind of tree). There's a method to the madness, but not a method that would make any sense to normal people :p
     
  3. Offline

    shadowmax507

    Wow, this is indeed a pretty ugly way of doing it. I might consider it though because it's a configuration file in which about 57 messages can be edited. Things get pretty confusing if you have no order and try to find that one message you want to edit.
    Thanks for your reply!
    EDIT: Well, I tried this method and it didn't work quite as expected but nevermind, I found my own solution.

    It certainly seems that is the case. :D
     
Thread Status:
Not open for further replies.

Share This Page