diff --git a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java index 20d2c93..56d4959 100644 --- a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java +++ b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.logging.Level; public class SaveWorldUtils { @@ -26,14 +27,6 @@ } public static void saveWorld(@Nullable Player player, String saveID, SaveSlot saveSlot) { - /*if (plugin.isFolia()) { - if (player != null) { - ChatUtil.sendErrorMessage(player, "Auf diesem Server können aktuell keine Welten gespeichert werden!"); - } - return; - }*/ - - try { if (plugin.getServerType() == ServerType.CHALLENGE_EVENT) { if (player != null) { @@ -65,6 +58,7 @@ nms.getWorldUtils().saveWorldNow(world_world); nms.getWorldUtils().saveWorldNow(world_end); nms.getWorldUtils().saveWorldNow(world_nether); + copyWorld(player, saveID, saveSlot); } else { ArrayList> allTasks = new ArrayList<>(); for (World world : Bukkit.getWorlds()) { @@ -80,45 +74,18 @@ }); } } - CompletableFuture waitForAll = CompletableFuture.allOf(allTasks.toArray(new CompletableFuture[allTasks.size()])); - waitForAll.get(); + CompletableFuture waitForAll = CompletableFuture.allOf(allTasks.toArray(new CompletableFuture[0])); + plugin.getScheduler().runAsync(() -> { + try { + waitForAll.get(); + nms.getWorldUtils().flushChunkSaves(); + copyWorld(player, saveID, saveSlot); + } catch (InterruptedException | ExecutionException e) { + throw new RuntimeException(e); + } + }); } } - File dir = new File(saveID); - File configs = new File("plugins/Challenges"); - File end = new File("world_the_end"); - File nether = new File("world_nether"); - File world = new File("world"); - File saveend = new File(saveID + "/world_the_end"); - File savenether = new File(saveID + "/world_nether"); - File saveworld = new File(saveID + "/world"); - File saveconfigs = new File(saveID + "/Challenges"); - File saveFolder; - if (plugin.getServerType() != ServerType.ADVENTURE) { - saveFolder = new File("/home/minecraft/saves/" + saveID + "/" + saveSlot.getSlot() + "/"); - } else { - saveFolder = new File("/home/minecraft/Adventure-saves/" + saveID + "/" + saveSlot.getSlot() + "/"); - } - try { - FileUtils.forceMkdir(dir); - FileUtils.copyDirectory(configs, saveconfigs); - FileUtils.copyDirectory(end, saveend); - FileUtils.copyDirectory(nether, savenether); - FileUtils.copyDirectory(world, saveworld); - if (saveFolder.isDirectory()) { - FileUtils.deleteDirectory(saveFolder); - } - FileUtils.moveDirectory(dir, saveFolder); - } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Could not save world ", ex); - if (player != null) { - ChatUtil.sendErrorMessage(Bukkit.getPlayer(saveID), "Map konnte nicht richtig gespeichert werden, bitte versuche es noch einmal"); - } - return; - } - if (player != null) { - ChatUtil.sendNormalMessage(player, "Welt wurde erfolgreich auf Slot " + saveSlot.getSlot() + " gespeichert!"); - } } catch (Exception ex) { plugin.getLogger().log(Level.SEVERE, "Welt konnte nicht gespeichert werden", ex); if (player != null) { @@ -126,4 +93,42 @@ } } } + + private static void copyWorld(Player player, String saveID, SaveSlot saveSlot) { + File dir = new File(saveID); + File configs = new File("plugins/Challenges"); + File end = new File("world_the_end"); + File nether = new File("world_nether"); + File world = new File("world"); + File saveend = new File(saveID + "/world_the_end"); + File savenether = new File(saveID + "/world_nether"); + File saveworld = new File(saveID + "/world"); + File saveconfigs = new File(saveID + "/Challenges"); + File saveFolder; + if (plugin.getServerType() != ServerType.ADVENTURE) { + saveFolder = new File("/home/minecraft/saves/" + saveID + "/" + saveSlot.getSlot() + "/"); + } else { + saveFolder = new File("/home/minecraft/Adventure-saves/" + saveID + "/" + saveSlot.getSlot() + "/"); + } + try { + FileUtils.forceMkdir(dir); + FileUtils.copyDirectory(configs, saveconfigs); + FileUtils.copyDirectory(end, saveend); + FileUtils.copyDirectory(nether, savenether); + FileUtils.copyDirectory(world, saveworld); + if (saveFolder.isDirectory()) { + FileUtils.deleteDirectory(saveFolder); + } + FileUtils.moveDirectory(dir, saveFolder); + } catch (IOException ex) { + Bukkit.getLogger().log(Level.SEVERE, "Could not save world ", ex); + if (player != null) { + ChatUtil.sendErrorMessage(Bukkit.getPlayer(saveID), "Map konnte nicht richtig gespeichert werden, bitte versuche es noch einmal"); + } + return; + } + if (player != null) { + ChatUtil.sendNormalMessage(player, "Welt wurde erfolgreich auf Slot " + saveSlot.getSlot() + " gespeichert!"); + } + } }