Resource Dumptruckman's CommentedConfiguration for 1.12(+) [NOW ALSO WITH MAVEN]

Discussion in 'Spigot Plugin Development' started by Articdive, Jul 10, 2018.

  1. So a while back in 2011 @dumptruckman made this:

    Towny, which is one of the oldest plugins to date, (afaik almost as old as Worldguard), never stopped using the CommentedConfiguration and @LlmDl always made sure it was updated and working. Recently I joined the Towny project and took care of many different things, at some point we also tweaked the CommentedConfiguration to hold strings longer than 80 chars in 1 line and a ton of other stuff.
    I with permission from both dumptruckman and LLmdl have decided to post about it.

    What even is a CommentedConfiguration?
    With this configuration system you can programatically (so in your IDE) setup your config and can read different values very easily , it also generates your configuration automatically and handles simple updates.

    - Very Easy To Use once Implemented
    - Not Tedious
    - Requires no more config.yml to be included in your JAR (in your maven resources)
    - Nor do you need to write the config.yml in yaml and learn YML formatting.
    - Pretty much updates itself whenever something is added.
    - Handles comments and default values without problems.

    - Shouldn't be used for auto-generating configs (it's possible but it defeats the purpose of using this)
    - Skips a line which looks a little stupid (searching for a fix):
    Temporary Fix: Adding a "\n" to the end of a line will result it them following each other directly, found by @Twan040
    Permanent Fix: has been implemented, if you are using the old version, copy FileMgmt again!
    - 1 line is skipped at the start of the config.
    Permanent Fix: has been implemented, if you are using the old version, copy CommentedConfiguraton again.
    - Makes the startup a little longer (not really noticeable)
    - Doesn't have real-time updates (?)

    HOW DOES IT WORK (roughly):
    It reads the file into JAVA (if it doesn't exist it just reads an empty file).
    It checks for every single node in the Enum you specified for that file, if that enum exists it does nothing and just copies it into a new config-object which is kept in memory.
    It that enum doesn't exist (at this point they are config sections) it creates it and also reads it into memory. So pretty much your entire config is stored in memory and can then be read.

    In the past the CommentedConfiguration used to be very very similar to the YAMLConfiguration in Bukkit, I changed that a while ago so we could have lines longer than 80 chars and not have them wrap.

    So pretty much we will need 4 class files:
    - ConfigEnum (Contains all the options, their default value, yaml path (section) and comments)
    - CommentedConfiguration (Is the object this can just be copied over or you can use it from someone else's maven dependency)
    - ConfigurationHandler (Handles config loading and saving, auto-generation and contains the methods to get options)
    - FileMgmt (Contains basic file managing methods, you can also use it from someone else's maven dependency)
    Once you have all of that copied and there are no missing classes, you just need to add the onEnable from to your Main Class.

    Thanks to some help from @creatorfromhell I have managed to create a maven-dependency for CommentedConfigurations which will allow the creation of an entirely custom CommentedConfiguration by pretty much 1 line of code and 1 Enum. Extensively if you want to have self-generating configs so data storage in configs (usually the case) then you can add an extra class.

    Code (Text):



    The Gist contains examples for your Nodes file and what you need to type in your on Enable:
    Maven Gist:

    Here is the Main Gist:

    Please note in the GISTs I kept some values from my projects (in the ConfigNodes e.t.c)
    it's all just an example!

    I personally like to make ConfigurationHandler a static class which means you initialise it at the start THIS IS NOT NECESSARY! Towny instantiates it aswell!
    Please remember to initialise FileMgmt in your onEnable though, you could instantiate it but that is down to you.

    This is my first ever post on the Spigot Forums, I hope you guys enjoyed, if you have any suggestions, fixes or just want to leave a Thank You, Feel Free!
    #1 Articdive, Jul 10, 2018
    Last edited: Sep 23, 2018
    • Like Like x 2
    • Winner Winner x 2
  2. Definitely gonna have to play with this. I love wizardy ^_^
  3. Hmm looks interesting, gonna play around with it :)
  4. Let me know if you guys discover anything or want to know anything about it :)
  5. a really good tutorial and example, i waiting already for the fix for skipping thath lines

    "- Skips a line which looks a little stupid (searching for a fix):"
  6. After doing some research, it seems that the skipping of said lines is Operating System dependant, I will continue trying to fix it but that is something that helps me.
  7. TEMPORARY FIX (maybe you find a better fix for this)

    okey i got a fix maybe not the best fix but it works


    if you can see the difference between 1 and 2

    2. is from your example

    1. is what i tested it out and it work just fine, what i just did is removed the "," and added "/n" at the end of the line and added "+" between the strings/text


    so you see here it won't skip the lines anymore (1.)

    Attached Files:

    • Useful Useful x 2
    • Creative Creative x 1
  8. and is there a way to set boolean and int/double in the ConfigNodes class?
  9. That brings me to a very nice idea on how to fix it, seems like it doesn't like adding the newline on some OS
    EDIT: Fixed it :) look at post.
    If you want to set anything you will have to save the config again. I don't have any setBoolean or setInt methods but they can be made quite simply.
    that would be done by doing something similar to:

    public static void setLastRunVersion(String currentVersion) {
    setProperty(ConfigNodes.LAST_RUN_VERSION.getRoot(), currentVersion);;

    So the same way you need a get method for every node you will need a set method for every node.
    #9 Articdive, Jul 12, 2018
    Last edited: Jul 12, 2018
    • Like Like x 1
  10. Another bug has been squashed regarding the CommentedConfiguration. There may soon be a maven dependency that will allow you to make CommentedConfigurations extremely easily :)
    • Like Like x 2
  11. i cannot wait for that
  12. Maven Integration is now out. Hope you guys enjoy
    • Like Like x 1