Implemented server autosave config (#1296)

This commit is contained in:
Anuken
2020-07-14 11:11:49 -04:00
parent b48e6d0e2f
commit fbd55a341e
3 changed files with 48 additions and 4 deletions

View File

@ -39,7 +39,7 @@ public class ServerControl implements ApplicationListener{
private static final int maxLogLength = 1024 * 512;
protected static String[] tags = {"&lc&fb[D]", "&lg&fb[I]", "&ly&fb[W]", "&lr&fb[E]", ""};
protected static DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("MM-dd-yyyy | HH:mm:ss");
protected static DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("MM-dd-yyyy HH:mm:ss");
private final CommandHandler handler = new CommandHandler("");
private final Fi logFolder = Core.settings.getDataDirectory().child("logs/");
@ -49,6 +49,7 @@ public class ServerControl implements ApplicationListener{
private Task lastTask;
private Gamemode lastMode = Gamemode.survival;
private @Nullable Map nextMapOverride;
private Interval autosaveCount = new Interval();
private Thread socketThread;
private ServerSocket serverSocket;
@ -153,12 +154,52 @@ public class ServerControl implements ApplicationListener{
}
});
//reset autosave on world load
Events.on(WorldLoadEvent.class, e -> {
autosaveCount.reset(0, Config.autosaveSpacing.num() * 60);
});
//autosave periodically
Events.on(Trigger.update, () -> {
if(state.isPlaying() && Config.autosave.bool()){
if(autosaveCount.get(Config.autosaveSpacing.num() * 60)){
int max = Config.autosaveAmount.num();
//use map file name to make sure it can be saved
String mapName = (state.map.file == null ? "unknown" : state.map.file.nameWithoutExtension()).replace(" ", "_");
String date = dateTime.format(LocalDateTime.now()).replace(" ", "_");
Seq<Fi> autosaves = saveDirectory.findAll(f -> f.name().startsWith("auto_"));
autosaves.sort(f -> -f.lastModified());
//delete older saves
if(autosaves.size >= max){
for(int i = max - 1; i < autosaves.size; i++){
autosaves.get(i).delete();
}
}
String fileName = "auto_" + mapName + "_" + date + "." + saveExtension;
Fi file = saveDirectory.child(fileName);
info("&lbAutosaving...");
try{
SaveIO.save(file);
info("&lbAutosave completed.");
}catch(Throwable e){
err("Autosave failed.", e);
}
}
}
});
Events.on(Trigger.socketConfigChanged, () -> {
toggleSocket(false);
toggleSocket(Config.socketInput.bool());
});
Events.on(PlayEvent.class, e -> {
try{
JsonValue value = JsonIO.json().fromJson(null, Core.settings.getString("globalrules"));
JsonIO.json().readFields(state.rules, value);