From 27c9de47768f5077db696e3e7306b332fa931567 Mon Sep 17 00:00:00 2001 From: LabodiDavid Date: Thu, 12 Dec 2024 22:28:56 +0100 Subject: [PATCH] Minor optimizations in save command --- .../hu/ditservices/commands/SaveCommand.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/hu/ditservices/commands/SaveCommand.java b/src/main/java/hu/ditservices/commands/SaveCommand.java index e028d71..7177496 100644 --- a/src/main/java/hu/ditservices/commands/SaveCommand.java +++ b/src/main/java/hu/ditservices/commands/SaveCommand.java @@ -6,20 +6,33 @@ import org.bukkit.World; public class SaveCommand { /** - * Saves all worlds and players data. + * Saves all worlds and players data on the main thread. * * @return boolean */ - public static boolean Run(){ + public static boolean Run() { STPlugin plugin = STPlugin.getInstance(); - String p = plugin.getConfig().getString("Saving.broadcastMsgProgress").replace("{PREFIX}",plugin.getPrefix()); - String d = plugin.getConfig().getString("Saving.broadcastMsgDone").replace("{PREFIX}",plugin.getPrefix()); - Bukkit.broadcast(p,"st.st"); - for(World w : Bukkit.getServer().getWorlds()){ - w.save(); - } - Bukkit.savePlayers(); - Bukkit.broadcast(d,"st.st"); + String prefix = plugin.getPrefix(); + String progressMsg = plugin.getConfig().getString("Saving.broadcastMsgProgress").replace("{PREFIX}", prefix); + String doneMsg = plugin.getConfig().getString("Saving.broadcastMsgDone").replace("{PREFIX}", prefix); + + // Notify players of save progress + Bukkit.broadcast(progressMsg, "st.st"); + + // Schedule task to run on the main thread + Bukkit.getScheduler().runTask(plugin, () -> { + // Save all worlds + for (World world : Bukkit.getServer().getWorlds()) { + world.save(); // Runs on main thread + } + + // Save player data + Bukkit.savePlayers(); // Also runs on main thread + + // Notify players of save completion + Bukkit.broadcast(doneMsg, "st.st"); + }); + return true; } }