A new way to do config files

Discussion in 'Resources' started by Rayzr522, Aug 20, 2016.

?

Would you use this?

  1. Yes

    7 vote(s)
    77.8%
  2. No

    2 vote(s)
    22.2%
Thread Status:
Not open for further replies.
  1. Offline

    Rayzr522

    UPDATE: This feature has now been added to my WIP library, BitzAPI. The usage is exactly like I listed here, except you need an instance of the ConfigManager class to use it. This isn't much of a problem though because every BitzPlugin instance has an instance of ConfigManager already.

    So recently I was working on my library, BitzAPI (Go check it out, it's somewhat kinda cool :p). I was trying to figure out what I wanted to do next with it, as I was pretty much at a stand-still. Then it hit me: I HATE CONFIG FILES.

    Why? For 2 simple reasons:
    1. You always end up having to write a nice little class to save and load your files but then that just takes extra work and it should really just be much simpler.
    2. To load ANY data from a config file you have to do 3000 checks to make sure that Bob McNooby didn't screw up his config files by changing the value of "player-level" from 52 to "taco". It happens.
    So I thought to myself, wouldn't it be nice if you could just make a data class and save/load it to config files, no hassle? Guess what? You can.

    Now this is just in the proof-of-concept stage, but here's an example of an inflexible but still fully functional implementation of my idea:
    The idea here is that all you would have to do to store data in config files is make a nice little data class like this:

    Code:
    public class MyDataClass implements Serializable {
    
        @Serialized
        public int playerLevel = 0;
    
        @Serialized
        public String playerNickName;
    
        @Serialized
        public double bankAccountMoney = 10.0;
    
    }
    And then save it using something like one of the following methods:
    Code:
    MyDataClass data = new MyDataClass();
    data.playerLevel = 42;
    data.playerNickName = "Joey";
    // Leave the bank account value at the default
    
    
    // Possible save method #1
    ConfigManager.save(data, "data/playerData.yml");
    
    // Possible save method #2
    // configFile is an assumed value gotten somewhere else
    ConfigManager.save(data, configFile);
    
    // Possible save method #3
    // Again, configFile is an assumed value gotten somewhere else
    ConfigurationSection section = configFile.createConfigurationSection("data." + data.playerNickName);
    ConfigManager.save(data, section);
    For loading it would be nearly the same, except you would also have to provide a Class variable to specify what kind of data it's loading. So for example it would be:
    Code:
    // Empty to start with
    MyDataClass data;
    
    // Possible load method #1
    data = ConfigManager.load(MyDataClass.class, "data/playerData.yml");
    
    // Possible load method #2
    // configFile is an assumed value gotten somewhere else
    ConfigManager.load(MyDataClass.class, configFile);
    
    // Possible load method #3
    // Again, configFile is an assumed value gotten somewhere else
    ConfigurationSection section = configFile.getConfigurationSection("data." + data.playerNickName);
    ConfigManager.load(MyDataClass.class, section);
    When you have pre-existing classes (like in the Bukkit API, you obviously can't change those classes) that you want to use as data types, you can create serializers for them (example). This would then allow you to use Vector data types in your data class, like
    Code:
    public class MyDataClass implements Serializable {
    
        @Serialized
        private Vector pos; // This would now be able to be automatically handled
    
    }
    To register a serialization handler, do this:
    Code:
    ConfigManager.registerSerializationHandler(Vector.class, new VectorSerializer());
    
    Obviously replacing Vector.class with the class you want and VectorSerializer with an instance of your serializer.

    I just wanted to post this to get everyone's feedback on this idea. Would this be something you would use? Do you have any suggestions for things to make/change to make this even easier?

    I do have some plans for other utilities like making a config file that saves every XYZ ticks (good for data that REALLY needs to be safe so that it won't be lost when a server crashes), and saving files asynchronously (good for really big files).

    Thank you all for your time, and for being patient with me and reading all of that big wall o' text! I really appreciate it. I know I have a really bad habit of posting/replying with huge walls of text. I think it's a medical condition or something.

    ––––––––––––––––––––––––––– EDIT –––––––––––––––––––––––––––
    Wow. I just realized I forgot to mention a few rather large chunks of this system.

    1. There will be more annotations. Currently all I can think of right now is @Default(value), but there will be more utility annotations for the serialized values.
    2. There will be a system where you can create serializers for pre-existing classes, allowing you to have those data types automatically handled. For an example, you could create a serializer for Vector, meaning if you had a vector type in your data class it would then know how to treat it.
    3. There will be all sorts of checking done for you so you don't have to do those 3000 checks by hand that I was talking about. That IS the main reason for this utility after all :p
    Again, thank you for you patience! Hopefully I'm not forgetting anything else... knowing myself, I probably am. gg
     
    Last edited: Aug 29, 2016
  2. Offline

    Rayzr522

    Thanks for the positive votes! Does anyone have any questions or suggestions?
     
  3. Offline

    Rayzr522

    Bump :3
     
  4. Hey @Rayzr522 ! Nice library :p saves a LOT of hassle when making plugins.
     
  5. Offline

    Rayzr522

    @Mindlessmink
    Thanks! Also the data method that I talk about in this post has now been implemented into BitzAPI. You just implement Serializable for your data class and then add @Serialized to all the fields you want serialized.

    To save/load there is already a ConfigManager instance provided for each BitzPlugin. I hope you enjoy!
     
  6. Offline

    Rayzr522

    Bump ^_^
     
  7. Offline

    Rayzr522

Thread Status:
Not open for further replies.

Share This Page