YAMLConfiguration (When to save?)

Discussion in 'Plugin Development' started by Dai_Kunai, Sep 13, 2021 at 4:18 AM.

  1. Offline

    Dai_Kunai

    So I've used YAMLConfigurations a ton as many others have in order for either configuration (config.yml) or other custom .yml's just simply in order to save info when a server stops. That is of course the main reason. That means that throughout the plugin, many times info in the custom YML's are changed due to something changing in the actual game, so I update it in the plugin using .set, also as expected. After these changes are done, I have to save the YAMLConfiguration at some point for the info to actually stay, right? Should I be waiting till onDisable() to do this or should I save after every single change that happens? And should I be pulling info straight out of the YAMLConfiguration file every time, or should I save the info as a variable when the server starts up.

    Lots of weird questions but I'm really not sure what the best way to do this is.

    I was a bit worried about what would happen if the server crashes; onDisable isn't called when the server crashes out of nowhere, meaning that if I only save in onDisable(), the info that changed since the last reset wouldn't save at all onto the file and would disappear or cause issues after.

    Thanks,
    DaiKunai.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Dai_Kunai How often does it gets changed? What kind of info do you save in there?
     
  3. Offline

    Dai_Kunai

    A lot of Strings and integers mainly (UUID's and scores and stuff). It changes a lot, assuming people are playing the game.

    EDIT: "A lot" as in once a minute average I guess, though it's pretty random.
     
  4. Online

    davidclue

    @Dai_Kunai You only need to call a save before you access it again if you changed any information otherwise it will retrieve the old data. The onDisable method is called even on server crashes for me.
     
  5. Offline

    Dai_Kunai

    Okay so in general if I change something using the .set method for a YAMLConfiguration, and I need to reaccess it, should I just have that info saved locally in some variables, or take it out and pull it back out of the actual file (saving and reloading each time.
     
  6. Online

    davidclue

    @Dai_Kunai Actually the best method would be in your onEnable method to read and initialize your variables, then use local variables for everything, and in onDisable just write it to the YAML and save it.
     
  7. Offline

    Tim_M

    In my own plugin I did just that, but also saved it every 10
    minutes or so in case of hard-crashes.
     
  8. Offline

    Dai_Kunai

    I see... I think I'll stick local variables and just pray the server doesn't decide to crash.
    Other than saving at onDisable ofc... If it becomes a problem I'll consider what you said @Tim_M

    I think I'll update every 5 minutes or so if I have constantly updating data that's rather important, like wins in gamemodes and stuff.
     
    Last edited: Sep 17, 2021 at 8:12 PM
    duyphuong.pham2013 likes this.

Share This Page