Commit 6c2c12c6 by Jonathan Thomas

Adding auto-saving to the mod, every 15 minutes

parent cfc59775
...@@ -21,6 +21,8 @@ import net.minecraft.text.Text; ...@@ -21,6 +21,8 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.owlmaddie.chat.ChatDataSaverScheduler;
import java.util.concurrent.TimeUnit;
import java.util.Arrays; import java.util.Arrays;
import java.util.Locale; import java.util.Locale;
...@@ -34,6 +36,7 @@ import java.util.UUID; ...@@ -34,6 +36,7 @@ import java.util.UUID;
public class ModInit implements ModInitializer { public class ModInit implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt"); public static final Logger LOGGER = LoggerFactory.getLogger("mobgpt");
public static MinecraftServer serverInstance; public static MinecraftServer serverInstance;
private static ChatDataSaverScheduler scheduler = null;
public static final Identifier PACKET_C2S_GREETING = new Identifier("mobgpt", "packet_c2s_greeting"); public static final Identifier PACKET_C2S_GREETING = new Identifier("mobgpt", "packet_c2s_greeting");
public static final Identifier PACKET_C2S_READ_NEXT = new Identifier("mobgpt", "packet_c2s_read_next"); public static final Identifier PACKET_C2S_READ_NEXT = new Identifier("mobgpt", "packet_c2s_read_next");
public static final Identifier PACKET_C2S_START_CHAT = new Identifier("mobgpt", "packet_c2s_start_chat"); public static final Identifier PACKET_C2S_START_CHAT = new Identifier("mobgpt", "packet_c2s_start_chat");
...@@ -180,9 +183,13 @@ public class ModInit implements ModInitializer { ...@@ -180,9 +183,13 @@ public class ModInit implements ModInitializer {
ServerWorldEvents.LOAD.register((server, world) -> { ServerWorldEvents.LOAD.register((server, world) -> {
String world_name = world.getRegistryKey().getValue().getPath(); String world_name = world.getRegistryKey().getValue().getPath();
if (world_name == "overworld") { if (world_name.equals("overworld")) {
serverInstance = server; serverInstance = server;
ChatDataManager.getServerInstance().loadChatData(server); ChatDataManager.getServerInstance().loadChatData(server);
// Start the auto-save task to save every X minutes
scheduler = new ChatDataSaverScheduler();
scheduler.startAutoSaveTask(server, 15, TimeUnit.MINUTES);
} }
}); });
ServerWorldEvents.UNLOAD.register((server, world) -> { ServerWorldEvents.UNLOAD.register((server, world) -> {
...@@ -190,6 +197,9 @@ public class ModInit implements ModInitializer { ...@@ -190,6 +197,9 @@ public class ModInit implements ModInitializer {
if (world_name == "overworld") { if (world_name == "overworld") {
ChatDataManager.getServerInstance().saveChatData(server); ChatDataManager.getServerInstance().saveChatData(server);
serverInstance = null; serverInstance = null;
// Shutdown auto scheduler
scheduler.stopAutoSaveTask();
} }
}); });
......
package com.owlmaddie.chat;
import net.minecraft.server.MinecraftServer;
/**
* The {@code ChatDataAutoSaver} class is a Runnable task, which autosaves the server chat data to JSON.
* It can be scheduled with the {@code ChatDataSaverScheduler} class.
*/
public class ChatDataAutoSaver implements Runnable {
private final MinecraftServer server;
public ChatDataAutoSaver(MinecraftServer server) {
this.server = server;
}
@Override
public void run() {
// Your method to save chat data
ChatDataManager.getServerInstance().saveChatData(server);
}
}
package com.owlmaddie.chat;
import net.minecraft.server.MinecraftServer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* The {@code ChatDataSaverScheduler} class is used to start the auto save Runnable task and schedule it.
*/
public class ChatDataSaverScheduler {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void startAutoSaveTask(MinecraftServer server, long interval, TimeUnit timeUnit) {
ChatDataAutoSaver saverTask = new ChatDataAutoSaver(server);
scheduler.scheduleAtFixedRate(saverTask, interval, interval, timeUnit);
}
public void stopAutoSaveTask() {
scheduler.shutdown();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment