Reliable Time/Place To Save Data

So recently an issue came up in my plugin, and that is that if the server crashes, my plugin will not save its in-memory data because it is saved in the server shutdown event. A temporary solution is to save the data on a timer, but that could block the main thread, depending on the amount of data, and if I try to move it to an async thread concurrent modification exceptions will come flying out at me. I know that minecraft catches all uncaught errors, and generates a crash report, so does that mean that shutdown hooks are still called? Is there perhaps a server crash event?

What sort of data is it? How often is it being modified?

If you are getting CME you should be copying the entire data before handing it off to your timed task, also rather then just timing it, if the data is rarely modified, you could have a dirty flag and only start the timer if the data is dirty.

One thing to protect you from potential file corruption as well, is to save it to a new file each time, then delete the old copy, then rename the new copy.

then on plugin startup, you can check to see if both copies exist, if so, saving failed, use the original. if only 1 copy exists, use that.

@ryantheleach
The data is mainly comprised of Votes from players (counts, offline status, etc.), and as such could be modified once a month, or every few seconds, depending on the scale of the server. Therefore it is difficult to determine exactly how much there is, and how often it is being modified. Copying all the data admittedly would lower the amount of impact the save operation had, perhaps I’ll do that. But I still want to know if I can cheat and use shutdown hooks ;).

Thanks for the help.

I’d honestly just use scoreboards :3 , but that’s me for a smaller server.

Why not segregate it into multiple files and save each of those when the data changes?