So, my first thread here in the resource section The mostly servers have plugins or something else to make a auto save. In the auto save, player and world data is saved to the disk. While the save runs, the server "freezes" so that you can't build or chat. This takes normally just < 1 second so it's not a problem. But now I'm working at a really big server with so many loaded chunks that the save takes sometimes over 5 seconds! One time, my horse died in the save, because of the lag, so that I decided to remove the lag in the save. As solution I made a new thread which calls the save methods. So the main thread wasn't affected. Here is my code: Code:java public class GameSaveThread extends Thread{ public GameSaveThread(){super();} @Overridepublic void run(){try{//Clone the players and save the dataPlayer[] players = Bukkit.getOnlinePlayers().clone();for(Player p: players){p.saveData();} //Get a list of all worldsList<World> worlds = Bukkit.getWorlds();//A loop trough all worldsfor(World w: worlds){//Check wheter we can cast the org.bukkit.World to org.bukkit.craftbukkit.v_16_R3.CraftWorldif(w instanceof CraftWorld){//Cast the world to craftbukkit code. Isn't perfect but I don't know a alternative solution.CraftWorld world = (CraftWorld) w; //Check wheter we could save the world. If not wait and try again.IChunkProvider provider = world.getHandle().chunkProvider;while(!provider.canSave()){Thread.sleep(1);} //Now we can save so we will do it.world.save();}else{//Can't cast the world. Can't make a secure save of the world.}}//Just for fun. It isn't necessary to call the garbage collectorRuntime.getRuntime().gc();}catch(Throwable e){//An error occured while saving the world. Try again...run();}}} This code works for me. Tested it 2.000 times without errors. You can see that I use craftbukkit code, because I want to check wheter I can save. If I just call "save()" the server throws sometimes a exception. On my server it works. You can optimize this code and upload it as a plugin if you want. Of course here is missing a way how to create and start the thread. I used the BukkitScheduler for it (Make a repeating task and this task start the thread) And I don't know wheter this code it is 100% error free. Note: Because of the "canSave()" check sometimes the save takes a few seconds. I would be happy if you help me to improve this code!
This seems really useful, I'm going to integrate it into my private plugin API incase I need to auto-save/save. Thanks.
I made this exactly for such big worlds. I know that CB & NMS is mostly not threadsave and yesterday I saw, that calling Code:java provider.canSave() is useless. I'm actually working on another solution. But, if the save fails because of a ConcurrentModification, I made a loop which try again if it fails.