Comments in config.yml Getting Deleted?

Discussion in 'Plugin Development' started by KeybordPiano459, Jul 6, 2012.

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

    KeybordPiano459

    When I created my plugin (and every time I export it) here's the config:
    Code:
    # Default Configuration File
    # Newspaper Developed By:
    # KeybordPiano459
    # benben500
    #
    # News is what will be shown on /news
    news: This plugin was developed by KeybordPiano459 and benben500
     
    # NOTD is what will be shown on /notd
    NOTD: Welcome! Remember to read the /news!
     
    # Cost is what /news will cost
    cost: 1.00
    When I test out my plugin with a newly generated config, it looks like this:
    Code:
    # Default Configuration File
    # Newspaper Developed By:
    # KeybordPiano459
    # benben500
    news: This plugin was developed by KeybordPiano459 and benben500
    NOTD: Welcome! Remember to read the /news!
    cost: 1.0
    Why does it do this? I need those comments to stay.
     
  2. Offline

    Jnorr44

    You need a 3rd person lib. Just copy someone else's with the correct licensing. Or you can write one on your own, but it's a very long class.
     
  3. Offline

    KeybordPiano459

    Do you know any plugins that are open-sourced that I could copy from?
     
  4. Offline

    Jnorr44

    No, sorry, you'll have to ask someone else.
     
  5. Offline

    KeybordPiano459

    Does anyone else know one? I really need this class.

    Essentials' config seems to work fine, I bet they have one. I was looking around their source code and I couldn't find a class like this. Was anyone else able to?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. Offline

    EnvisionRed

    You can probably write a loop of sorts that manually writes out each line to a config file you create.
     
  7. Offline

    KeybordPiano459

    The problem is I have absolutely no idea how I would do that. :(
     
  8. The reason it works for essentials is because essentials doesn't save the config. It only loads from it. For some reason all comments get overwritten when you save the config.
     
  9. Take a look at my plugin, in the package: tk.blackwolf12333.utils.config, you could create a class like mine in which you create your own config handler to load and save the config, this one was really simple, but it works:)

    greetz blackwolf12333
     
  10. blackwolf12333
    That's not simple, it's pointlessly complicated... using saveResource() however is simple... or better yet, don't use anything, the getConfig() automatically saves the config.yml file from the jar to the corect folder, getConfig() loads defaults from resource config which also removes the comments, the saveDefaultConfig() method saves the config.yml from the jar file.

    For custom configs you can just use saveResource() to save them from the jar to a folder.
    Also, your class doesn't preserve comments when saving file with changes, it only saves default which like I said, getConfig() already does.
     
  11. Offline

    KeybordPiano459

    All I have in my onEnable now is new GLConfigHandler(this); Do I need anything else? My config files don't generate anymore.

    Would this work?
    Code:
    saveResource(String /config.yml, boolean replace);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  12. Code:
    saveResource("config.yml", false);
    EDIT: actually, you can just use saveDefaultConfig() because it also checks if the file exists which is better because saveResource() prints a message if the file exists.
    Which can be confusing for admins.

    For custom configs, just add this and change the method name and file name:
    Code:
        public void saveDefaultConfig() {
            if (!new File(getDataFolder(), "config.yml").exists()) {
                saveResource("config.yml", false);
            }
        }
     
  13. That's weird, because it works quite well for my plugin...
     
  14. I didn't say it doesn't work :) I said it's pointless when such methods are already in Bukkit.
     
  15. hmm yeah, whatever, i am still learning:p
     
Thread Status:
Not open for further replies.

Share This Page