[Solved] Saving config WITHOUT deleting comments

Discussion in 'Spigot Plugin Development' started by NonameSL, Jun 20, 2015.

  1. I have a default config in the jar and I'm using saveDefaultConfig(); to load it with the comments, and whenver I'm trying to save the config all the comments are deleted. How can I fix that?
    Thanks in advance!

    Before you comment, a few notes:
    1. I can't use getConfig().options().copyDefaults(true) and saveConfig() in onEnable because that wont save all the comments which is what I am going for.
    2. I can't use saveConfig() in onDisable because that won't save all the comments.
    3. I cant use saveDefaultConfig() in onDisable because that won't save at all.

    I'm only writing this because all the comments were these solutions. I'm trying to both save the config and not remove the comments!

    Edit: Found a solution.
     
    #1 NonameSL, Jun 20, 2015
    Last edited: Jun 21, 2015
  2. don,t use getConfig().options().copyDefaults(true);
    saveConfig();

    use
    saveDefaultConfig(); at main class instead
     
  3. I'm sorry, I didn't understand what you said. Can you please explain yourself?
    I only have saveDefaultConfig() in onEnable() and saveConfig() in onDisable() (which deletes the comments)
     
  4. oh you didn't notice then delete the saveConfig(); at onDisable
     
  5. But if I delete the saveConfig() at onDisable, then it won't save the config (I'm setting stuff in the config in onDisable). Any better idea?
     
  6. lol it will save :p
    you need to learn java won't you?
     
    • Optimistic Optimistic x 2
  7. What is your current code and do you have any errors?
     
  8. Create a config.yml and write your stuffs there.
    And add this to your onEnable():
    getConfig().options().copyDefaults(true);
    saveConfig();
     
  9. he wants to save comments at config

    like
    #hello
    boolean: true

    and he still using saveConfig(); at onDisable
     
  10. I dont have any errors. I'll put the code in the original post
    I know java just fine - I just didn't understand what you were saying since you were unclear. Also, it doesn't save, because if it did I wouldn't be having this problem. When I'm using saveConfig() it saves what I'm setting in onDisable() but deletes all the comments besides the header.
     
  11. saveDefaultConfig(); must be good
     
  12. That won't save the comments.
     
  13. Well, I use this too, and it works fine.
     
  14. I don't mean just a header, I mean like:
    Code (Text):
    a-line-in-the-confg:
    # This is a comment here.
    another-line: true
    # Look, here's another comment!
    one-more-line: false
     
    Using copyDefaults(true); won't save these ^^^ comments
     
  15. I use

    saveDefaultConfig();
    reloadConfig();

    in my onEnables, it saves comments and everything :)
     
  16. Let's start with this,
    could you show the code where you load, edit and save the config?
     
  17. Please read the original post. I want to save the config at a certain point in onDisable() but it deletes commands...
     
  18. It's a simple getConfig().set(path, string) and saveConfig() in onDisable, and a saveDefaultConfig() in onEnable. Thats it
     
  19. if you dont show the code we cant help you then
     
    • Optimistic Optimistic x 1
  20. ...
    Code (Text):
    public void onEnable(){
        saveDefaultConfig();
        ///... Unrelated code
    }
    //...
    public void onDisable(){
        getConfig().set("apath", "astring");
        saveConfig();//Without this, apath won't be saved.
        //... Unrelated code
    }