saveConfig but keep comments?

Discussion in 'Plugin Development' started by WolfMage1, Dec 28, 2016.

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

    WolfMage1

    We all know that Bukkits FileConfiguration#saveConfig method doesn't save comments, only the header which is kind of useless imo if you want to explain certain parts of a config without people having to scroll all the way to the top to remember what it does again.

    Is there any way to use FileConfiguration#saveConfig and keep the comments? Only thing I've found so far is just how to use configs, nothing about comments not saving.
     
  2. Offline

    MrGeneralQ

    There is a simple method

    Create the configfile in your plugin directory. Then instead of using. ".saveConfig() " use saveDefaultConfig"

    Now one more important thing, you should use the config as a read only document! Don't make the plugin add something in the config file, as soon as you add something in the config or update something the comments will dissapear.

    So only use it to read stuff, not write.

    That's how you do it :)
     
  3. Offline

    WolfMage1

    I was using saveConfig to add values that weren't there, I guess I can just re write it from the one in the jar if they're not found....
     
  4. Offline

    MrGeneralQ

    If you want to store data use a second config file. Make sure the regular config is read only. (Not an actual file setting)
     
  5. Offline

    WolfMage1

    Code:
    config.put("regeneration_interval", 120);
    
            config.put("ignore_creative_players", false);
    
            config.put("remove_placed", true);
            config.put("restore_broken", true);
            config.put("restore_exploded", true);
    
            config.put("worldguard_support", false);
            config.put("factions_support", false);
    
            config.put("protected_worlds", Arrays.asList("world", "world_nether", "world_the_end"));
            config.put("protected_regions", new ArrayList<>());
            config.put("ignored_materials", new ArrayList<>());
            config.put("ignored_users", new ArrayList<>());
    Then I was iterating over it and seeing if the config contains those items, if it did ignore it, otherwise set it.
     
  6. Offline

    dart2112

    what I do is I have a setting at the top called config version, I check that it is the same as the one that is packaged with the jar, if it's not then I save the config.yml as config_old.yml and run saveDefaultConfig() again. that way the user can still see what their old setting were and have all the new setting and comments
     
  7. Offline

    MrGeneralQ

    As I said I would keep this config read only.

    I wouldn't use config.put(..)

    Just create a new config.yml file in Your project folder and write the custom config.

    Way better
     
  8. Offline

    WolfMage1

    config is a hashmap, its not the actual config.
     
  9. Offline

    MrGeneralQ

    That is indeed a very proper way of doing it! But is Your problem solved or stille some thing?

    I check tomorrow again it's 2:33am here , i'm tired asf.

    Until tomorrow!
     
Thread Status:
Not open for further replies.

Share This Page