Add comments to config, with the default bukkit config API?

Discussion in 'Plugin Development' started by MrMag518, Feb 8, 2012.

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

    MrMag518

    I was wondering (as the title says) if there's a possibility to add comments to the config using the default config API(bukkit)?
    Example:
    Code:
    # This thing makes things to smile.
    Enable-thing-smile: true
    
    If it's not possible, how do I do it?
    I've tested some other methods but they don't seem compitable with the default config API..
     
  2. no, i don't think this is possible with the default config api, only to set the header....
    To still make it possible you can make a already finished standard config file with comments as a resource in your jar and later just read it and write it into a new file.
     
  3. Offline

    MrMag518

    It's wierd how they only allow headers, and not comments :/
    I hope they'll implement it..
    Thanks for the help, btw!

    kumpelblase2
    Sorry for asking, but I'm not so sure on how to listen to the config.yml resource .. Do you know how?

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

    Don Redhorse

    the wiki has some documentation about how to access the config..

    Take a look at Sync from @Adamkills or for my HelperClasses when they are finished, I still need some time though... probably 7 working days...
     
    MrMag518 likes this.
  5. Offline

    MrMag518

    I am already using Sync ^^
    It's awesome, but I am getting irritated that other servers have to have it installed to :/
    But I just figured out how to get the resource, thanks for replying :)
     
  6. Offline

    Don Redhorse

    Yeah... it needs some more plugins requiring it... perhaps even a good plugin requiring it... or better it should probably be IN bukkit..

    If I finish my project... atm it should be probably when not if... you can just dump the classes into your project without changing a lot (mainly package name etc).. the rest is almost fire and forget... but not for really sophisticated stuff... more for commented configs, easy permission and command handling, plus an advanced logger.

    Unfortunately a lot of good ideas are buried in plugins but nobody really did come forward with making it as easy to use as Sync for example... some plugins have cool command handling, some have cool permission handling but nothing is documented or just directly useable... sk89q wrote a nice blog about stuff he does... but the code was never released and wouldn't work for other plugins...
     
    MrMag518 likes this.
  7. Offline

    thehutch

    You can add comments to the config.yml basically you need to write it yourself instead od adding defaults then use saveDefaultConfig();
     
  8. Offline

    MrMag518

    Ye, I've been thinking of that, thanks :)
     
  9. Offline

    coldandtired

    If the config is simple enough just write it line by line with a FileWriter
    Code:Java
    1.  
    2. FileWriter fw = new FileWriter(f);
    3. fw.write("# example line here\n");
    4. fw.write("default_key: default_value\n");
    5. //etc.
     
  10. Offline

    MrMag518

    Ye, but its returning a NPE, because the bukkit config API uses FileConfiguration, and the FileWritter needs File, it's simply un-compitable with FileConfiguration in my point.
     
  11. Offline

    coldandtired

  12. Offline

    MrMag518

  13. Offline

    thehutch

    coldandtired Why would you do that it would just be much simpler to actually write the file and also how do you retrieve strings/ints etc from that you would have to write your own config API
     
  14. Offline

    Don Redhorse

    only problem with reading the config.yml from inside the plugin and storing it that on first try the comments are there... afterwards they are gone..

    you need to have a custom config API to save comments again and again... either by using something like sync, my helperclasses (in the future) or by writing your own.
     
  15. Offline

    coldandtired

    The FileConfiguration class doesn't work so well when you don't know what values are going to be put there.

    I have a config where I make the outline and the admin fills in any blanks he wants to use. In code I can check for a null value to a key, but the config class won't let me set an empty key, or an empty section.

    You don't need to write your own config API at all, just just MemorySections and MapLists.
     
  16. Offline

    thehutch

    You should really use YamlConfiguration not FileConfiguration and if you don't know what you're going to put in the config use the saveDefaultConfig() and you just write it all out, also your FileWrite does exactly what addDefault() does :D
     
  17. MrMag518:
    why do you need to use the default config api? Sure, it's easier to use, but if something isn't working why don't use alternatives then?
    Since this is only for the DEFAULT part, meaning that if no config exists (I'm assuming no one will delete a path in the config), it'll create one. For this situation you can simply use getResourceFileAsStream() to read from the resource and write it to a file. then just reloading the file that you've written and you can still use the config api.

    You can put into the resource what every you like. So you can set comments wherever you like and so on. Since you know where your file from the config will be, you can check if it exists, and if not create it, and then just write into it like I said.

    Code:java
    1.  
    2. FileOutputStream writer = new FileOutputStream(new File(getDataFolder() + File.seperator + "config.yml"));
    3. InputStream out = <yourMainClass>.class.getResourceAsStream("path_to_your/file");
    4. byte[] linebuffer = new byte[4096];
    5. int lineLength = 0;
    6. while((lineLength = out.read(linbuffer)) > 0)
    7. {
    8. writer.write(line buffer, 0, lineLength);
    9. }
    10. fw.close();
    11.  

    and all your comments will be written.
    You just need to make sure the resource that you're trying to read is inside the jar. E.g. if you have a folder besides your src folder called "resources" the path would be "/resources/file.txt" or whatever your filename is.
    In that file.txt you can write all you default config with comments in there.
     
    SchulziHD and MrMag518 like this.
  18. Offline

    coldandtired

    Not quite. I can't output this
    Code:
    blaze:
      general:
      auto_spawn:
      spawn_rules:
      death_rules:
    with a config class so I had to write it line by line.
     
  19. Offline

    MrMag518

    Thanks, I'ma test it.
     
Thread Status:
Not open for further replies.

Share This Page