[SOLVED] Append existing config?

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

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

    THEK

    I'm using the Bukkit config api to make config files for my plugins. However, every time I bring out an update that changes the config I have to ask the users to recreate their config file. I'm sure this is getting pretty annoying for them.

    What is an efficient way of simply adding the changes to an existing config file? Do I have to load the entire config and save it with the appended lines?
     
  2. Offline

    Chekkaa

  3. Offline

    THEK

    But the only time I use setProperty is if the config file doesn't exist. Apart from that all I do is load it.
     
  4. Offline

    Chekkaa

    I just checked. You can easily add to an existing config file by adding a new property with setProperty.
     
  5. Offline

    THEK

    Thank you. How would I check if it doesn't exist? At the moment if I load it even with a default value it throws a nullPointerException.
     
  6. Just check if the existing config file is "outdated" by your definition.
    Your new version apparently has changes in the config, so check if for example new properties don't exist yet, and then set them with setProperty.
    Note that that will mess up the order and comments of your config.
     
  7. Offline

    THEK

    I've been using getBoolean for a certain node that shouldn't exist and I have to give it a default value. This means that it'll load no matter what. How do I check if it doesn't exist at all?
     
  8. Code:
    config.getProperty("whatever") == null
     
  9. Offline

    DrAgonmoray

    Here's what I do to auto-update my configs:
    PHP:
        public String readString(String nodeString def) {
            
    Object v config.getProperty(node);
            if (
    != null) {
                return 
    config.getString(node);
            } else {
                
    config.setProperty(nodedef);
                
    config.save();
                return 
    def;
            }
        }
    And then later on, I do something like..
    PHP:
    config.load();
            
    plugin.worlds readString("settings.worlds""").replace(" """).split(",");
    This way, it will load it, or if it doesn't exist, it will add it in with a default value.
     
  10. Offline

    THEK

    Thank you very much :) It worked perfectly.
     
Thread Status:
Not open for further replies.

Share This Page