Best method of storing and loading config

Discussion in 'Plugin Development' started by MrGeneralQ, Feb 20, 2017.

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

    MrGeneralQ

    Okay so I'm currently creating a plugin (big plugin) with a friend. Now my question:

    What would be the best method to store and retrieve data?

    This is what I currently have:

    I'm currently storing everything in another class which initializes from my Main class. There I got some arrays and hashMaps to store the data. I'm using methods and retrieve the data from those Arrays and HashMaps.
    The same goes for storing, I store everything in the map.

    Now in order to save it to the config I was think to do that just on the onDisable(). However, then I changed my mind , if anything would go wrong all data would be lost and there is no way to get it back. So then I was thinking on storing the data and at the same time save it in the config. And the get methods only get data from the arrays , not the config. So loading data would only happen if the plugin enables.

    Would that be a good way to do it? I'm also thinking the more stuff I store in the local memory, the higher the ram usage goes. Any help from someone experienced would be much appreciated!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MrGeneralQ Config gets fully loaded into memory any ways I believe. So don't worry about that.
    Only save when a save is needed.
    How often will your data change?
     
  3. Offline

    MrGeneralQ

    It depends from the data actually. Sometimes it does, sometimes it doesn't. So is it a good thing to save the data to the config after I saved it in the memory(class)?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @MrGeneralQ I would say if it hasn't been touched for ... time.
     
  5. Offline

    MrGeneralQ

    Now I lost you. What do you mean?
     
  6. But you're trying to save config or data? If the answer is data then I recommended gson, works pretty well because you don't need a parser, it does all the saving and loading by its own.
    Code:
    String save = new Gson().toJson(YourObject);
    
    YourObject = newGson().fromJson(savedString, YourObject.class);
    Note: If you are using List or Map you will need to use a TypeToken but it's just a simple edit.
    Code:
    YourList = new Gson().fromJson(savedString, new TypeToken<List<T>>(){}.getType());
    
    YourMap = new Gson().fromJson(savedString, new TypeToken<Map<T, T>>(){}.getType());
     
    Last edited: Feb 22, 2017
  7. Offline

    Drkmaster83

    @timtower probably means create a backup period. You can keep track of when you last saved/modified the data from the maps/arrays to config, and if the time passed has surpassed that threshold, you can just save all the data in the arrays/maps to the file. Essentially a backup period at "...", or X amount of time.
     
Thread Status:
Not open for further replies.

Share This Page