Need help saving config with comments

Discussion in 'Plugin Development' started by aman207, Jul 16, 2013.

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

    aman207

    So first I will just say this isn't a problem with copying in onEnable. I got that figured out.

    What I am trying to do, is I am trying to create a section in the config.yml but keep my comments that are already there. Right now, I am using plugin.saveConfig(); but that overwrites my comments (except for the header comments).

    Do I have to get the file via InputSteam, write to it and then put it back or whatever using OutputSteam?

    Thanks.
     
  2. Offline

    CubieX

    If using the bukkit config API, it's not possible to retain the comments between nodes on saving the file.
    If you modify the file via an OutputStream, you can control it yourself, I guess.
    But it's not as easy to use as the Bukkit config API.

    What I usually do in this case is to place all comments first in the file and then do the config part below it.
    Because comments before the first config node should be kept. Even when saving the file.
     
  3. Offline

    tombath10

    Heres how you add a comment into the top of the file.

    Code:java
    1. @Override
    2. public void onEnable() {
    3.  
    4. FileConfiguration config = getConfig();
    5.  
    6. config.options()
    7. .header("#====== DO NOT EDIT THIS FILE ======\r\n======= THIS FILE STORES USER DATA ========");
    8. config.options().copyHeader(true);
    9.  
    10. config.options().copyDefaults(true);
    11. saveConfig();
    12. }
     
  4. Offline

    aman207

    Sorry I should have been more clear. I am trying to retain comments with a custom yml file. Not with the config.yml
     
  5. Offline

    Wingzzz

    aman207
    Code:YAML
    1. # retainable comment

    Ensure there is at least one space between the '#' key and your text/characters that follow.
     
  6. Offline

    aman207

  7. Offline

    Wingzzz

    Well I'm not sure then. I use it and have no problems as well as others have had success when they've used that way of comments. :(
     
  8. Offline

    aman207

    I figured out another way around.

    Thanks anyways
     
    Wingzzz likes this.
  9. Offline

    SnipsRevival

    Please post your solution.
     
  10. Offline

    aman207

    Well instead of trying add a section to my .yml with the comments, I just decided to create a new file, and put the new sections in there. Which actually helps with organization as well.
     
Thread Status:
Not open for further replies.

Share This Page