diff --git a/src/main/java/de/fanta/challengesjoinentities/Server.java b/src/main/java/de/fanta/challengesjoinentities/Server.java index 5b13d8d..0760909 100644 --- a/src/main/java/de/fanta/challengesjoinentities/Server.java +++ b/src/main/java/de/fanta/challengesjoinentities/Server.java @@ -11,13 +11,21 @@ import org.jetbrains.annotations.Nullable; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; public class Server { public static HashMap serverPlayers = new HashMap<>(); @@ -82,24 +90,32 @@ public void loadSaved(Player p, UUID playerID, String slot) { this.online = true; serverPlayers.put(this, p.getUniqueId()); - File dir; + File saveFIle; //dir = new File("/home/minecraft/" + this.serverType.getDir() + "/" + playerID + "/" + slot); if (serverType == ServerType.CHALLENGES) { - dir = new File(plugin.getChallengeSavePath().toFile(), "/" + playerID + "/" + slot); + saveFIle = new File(plugin.getChallengeSavePath().toFile(), "/" + playerID + "/" + slot); } else { - dir = new File(plugin.getAdventureSavePath().toFile(), "/" + playerID + "/" + slot); + saveFIle = new File(plugin.getAdventureSavePath().toFile(), "/" + playerID + "/" + slot); + } + + if (!saveFIle.isDirectory()) { + if (serverType == ServerType.CHALLENGES) { + saveFIle = new File(plugin.getChallengeSavePath().toFile(), "/" + playerID + "/" + slot + ".zip"); + } else { + saveFIle = new File(plugin.getAdventureSavePath().toFile(), "/" + playerID + "/" + slot + ".zip"); + } } String path = playerID + "/" + slot; - if (!dir.isDirectory()) { + if (!saveFIle.isDirectory() && !saveFIle.isFile()) { ChatUtil.sendErrorMessage(p, "Map " + path + " nicht gefunden!"); this.online = false; serverPlayers.remove(this); return; } - load(p, this.serverType.getDir(), path, getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); + load(p, this.serverType.getDir(), path); } public void loadNewAdventure(Player player, String category, String map, String mapVersion) { @@ -117,15 +133,14 @@ return; } - load(player, "Adventure-Maps", category + "/" + map, getMapVersion(new File(dir + "/Challenges/serverconfig.yml"))); + load(player, "Adventure-Maps", category + "/" + map); } else { ChatUtil.sendErrorMessage(player, "Du brauchst mindestens die Version " + ChatUtil.BLUE + mapVersion + ChatUtil.RED + " um die Map zu starten!"); } } - public void load(Player p, String serverTypeDir, String targetDir, String ServerVersion) { + public void load(Player p, String serverTypeDir, String targetDir) { plugin.getScheduler().runAsync(() -> { - //File serverFolder = new File("/home/minecraft/Server/" + this.dir); File serverFolder = new File(plugin.getServerFolderPath().toFile(), this.dir); File configs = new File(serverFolder, "/plugins/Challenges"); File end = new File(serverFolder, "/world_the_end"); @@ -133,8 +148,6 @@ File world = new File(serverFolder, "/world"); File mlgworld = new File(serverFolder, "/mlg_challenge"); File serverJar = new File(serverFolder, "/paper.jar"); - //File saveServerJar = new File("/home/minecraft/ServerJars/" + ServerVersion + "/paper.jar"); - File saveServerJar = new File(plugin.getServerJarsPath().toFile(), ServerVersion + "/paper.jar"); File saveend; File savenether; File saveworld; @@ -142,21 +155,11 @@ if (!serverTypeDir.equals("Adventure-Maps")) { - /*saveend = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world_the_end"); - savenether = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world_nether"); - saveworld = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/world"); - saveconfigs = new File("/home/minecraft/" + serverTypeDir + "/" + targetDir + "/Challenges");*/ - saveend = new File(plugin.getChallengeSavePath().toFile(), targetDir + "/world_the_end"); savenether = new File(plugin.getChallengeSavePath().toFile(), targetDir + "/world_nether"); saveworld = new File(plugin.getChallengeSavePath().toFile(), targetDir + "/world"); saveconfigs = new File(plugin.getChallengeSavePath().toFile(), targetDir + "/Challenges"); } else { - /*saveend = new File("/home/storagebox/" + serverTypeDir + "/" + targetDir + "/world_the_end"); - savenether = new File("/home/storagebox/" + serverTypeDir + "/" + targetDir + "/world_nether"); - saveworld = new File("/home/storagebox/" + serverTypeDir + "/" + targetDir + "/world"); - saveconfigs = new File("/home/storagebox/" + serverTypeDir + "/" + targetDir + "/Challenges");*/ - saveend = new File(plugin.getAdventureSavePath().toFile(), targetDir + "/world_the_end"); savenether = new File(plugin.getAdventureSavePath().toFile(), targetDir + "/world_nether"); saveworld = new File(plugin.getAdventureSavePath().toFile(), targetDir + "/world"); @@ -181,20 +184,39 @@ } HashMap copyMap = new HashMap<>(); - copyMap.put(saveServerJar, serverJar); - copyMap.put(saveconfigs, configs); - copyMap.put(saveworld, world); - copyMap.put(savenether, nether); - copyMap.put(saveend, end); - moveFolderWithProgress(copyMap, p); - //FileUtils.copyFile(saveServerJar, serverJar); - //FileUtils.copyDirectory(saveconfigs, configs); - //FileUtils.copyDirectory(saveworld, world); - //FileUtils.copyDirectory(savenether, nether); - //FileUtils.copyDirectory(saveend, end); - //File serverConfigFile = new File("/home/minecraft/Server/" + this.dir + "/plugins/Challenges/serverconfig.yml"); - File serverConfigFile = new File(plugin.getServerFolderPath().toFile(), this.dir + "/plugins/Challenges/serverconfig.yml"); + if (saveworld.isDirectory()) { + copyMap.put(saveconfigs, configs); + copyMap.put(saveworld, world); + copyMap.put(savenether, nether); + copyMap.put(saveend, end); + } else { + if (!serverTypeDir.equals("Adventure-Maps")) { + copyMap.put(new File(plugin.getChallengeSavePath().toFile(), targetDir + ".zip"), new File(serverFolder, "save.zip")); + } else { + copyMap.put(new File(plugin.getAdventureSavePath().toFile(), targetDir + ".zip"), new File(serverFolder, "save.zip")); + } + } + + for (File file : copyMap.keySet()) { + System.out.println(file.toPath() + " " + copyMap.get(file).toPath()); + } + + moveFolderWithProgress(copyMap, "Welt", p); + + if (new File(serverFolder, "save.zip").isFile()) { + extractZipWithProgress(new File(serverFolder, "save.zip"), serverFolder, p); + copyMap.clear(); + copyMap.put(new File(serverFolder, "/Challenges/"), new File(serverFolder, "/plugins/Challenges/")); + moveFolderWithProgress(copyMap, "Config", p); + } + + File saveServerJar = new File(plugin.getServerJarsPath().toFile(), getMapVersion(new File(serverFolder, "/plugins/Challenges/serverconfig.yml")) + "/paper.jar"); + copyMap.clear(); + copyMap.put(saveServerJar, serverJar); + moveFolderWithProgress(copyMap, "ServerJar", p); + + File serverConfigFile = new File(serverFolder, "/plugins/Challenges/serverconfig.yml"); YamlConfiguration serverConfig = new YamlConfiguration(); serverConfig.load(serverConfigFile); @@ -221,7 +243,7 @@ }); } - public static void moveFolderWithProgress(HashMap copyMap, Player player) throws IOException { + public static void moveFolderWithProgress(HashMap copyMap, String description, Player player) throws IOException { final long[] totalBytes = {0}; final long[] copiedBytes = {0}; AtomicReference progress = new AtomicReference<>(0.0); @@ -244,7 +266,7 @@ Files.copy(source, target.toPath(), StandardCopyOption.REPLACE_EXISTING); copiedBytes[0] += source.toFile().length(); progress.set((double) copiedBytes[0] / totalBytes[0] * 100); - player.sendActionBar(ChatUtil.GREEN + "Kopiere Welt: " + getProgressBar(progress.get())); + player.sendActionBar(ChatUtil.GREEN + "Kopiere " + description + ": " + getProgressBar(progress.get())); } } catch (IOException e) { e.printStackTrace(); @@ -333,4 +355,41 @@ } return serverVersion; } + + public static void extractZipWithProgress(File zipFile, File destination, Player player) throws IOException { + try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile))) { + ZipEntry entry; + byte[] buffer = new byte[1024]; + long totalBytes = 0; + long extractedBytes = 0; + + while ((entry = zipInputStream.getNextEntry()) != null) { + if (!entry.isDirectory()) { + File file = new File(destination, entry.getName()); + File parentDir = file.getParentFile(); + + if (!parentDir.exists()) { + parentDir.mkdirs(); + } + + try (OutputStream outputStream = new FileOutputStream(file)) { + int bytesRead; + + while ((bytesRead = zipInputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + extractedBytes += bytesRead; + int progress = (int) ((extractedBytes * 100) / entry.getSize()); + player.sendActionBar(ChatUtil.GREEN + "Entpacke Welt: " + getProgressBar(progress)); + } + } + } + + zipInputStream.closeEntry(); + totalBytes += entry.getCompressedSize(); + } + + System.out.println("Entpackung abgeschlossen!"); + System.out.println("Gesamtfortschritt: " + ((extractedBytes * 100) / totalBytes) + "%"); + } + } } \ No newline at end of file diff --git a/src/main/java/de/fanta/challengesjoinentities/loadgui/ChallengeLoadGUI.java b/src/main/java/de/fanta/challengesjoinentities/loadgui/ChallengeLoadGUI.java index a18ad20..f1eb62a 100644 --- a/src/main/java/de/fanta/challengesjoinentities/loadgui/ChallengeLoadGUI.java +++ b/src/main/java/de/fanta/challengesjoinentities/loadgui/ChallengeLoadGUI.java @@ -51,14 +51,18 @@ File challengeSave2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2"); File challengeSave3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3"); File challengeSaveAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave"); + File challengeSaveZip1 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/1.zip"); + File challengeSaveZip2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2.zip"); + File challengeSaveZip3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3.zip"); + File challengeSaveZipAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave.zip"); for (int i = 0; i < WINDOW_SIZE; i++) { ItemStack item; switch (i) { case SAVE_1_INDEX -> { if (p.hasPermission("challenge.save.slot1")) { - if (challengeSave1.isDirectory()) { - item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 1", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave1)); + if (challengeSave1.isDirectory() || challengeSaveZip1.isFile()) { + item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 1", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave1.isDirectory() ? challengeSave1 : challengeSaveZip1)); } else { item = GUIUtils.createGuiItem(Material.LIME_STAINED_GLASS_PANE, ChatUtil.GREEN + "Frei"); } @@ -69,8 +73,8 @@ case SAVE_2_INDEX -> { if (p.hasPermission("challenge.save.slot2")) { - if (challengeSave2.isDirectory()) { - item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 2", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave2)); + if (challengeSave2.isDirectory() || challengeSaveZip2.isFile()) { + item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 2", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave2.isDirectory() ? challengeSave2 : challengeSaveZip2)); } else { item = GUIUtils.createGuiItem(Material.LIME_STAINED_GLASS_PANE, ChatUtil.GREEN + "Frei"); } @@ -81,8 +85,8 @@ case SAVE_3_INDEX -> { if (p.hasPermission("challenge.save.slot3")) { - if (challengeSave3.isDirectory()) { - item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 3", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave3)); + if (challengeSave3.isDirectory() || challengeSaveZip3.isFile()) { + item = GUIUtils.createGuiItem(Material.MAP, ChatUtil.GREEN + "Save 3", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSave3.isDirectory() ? challengeSave3 : challengeSaveZip3)); } else { item = GUIUtils.createGuiItem(Material.LIME_STAINED_GLASS_PANE, ChatUtil.GREEN + "Frei"); } @@ -93,8 +97,8 @@ case SAVE_AUTO_INDEX -> { if (p.hasPermission("challenge.save.slotauto")) { - if (challengeSaveAuto.isDirectory()) { - item = GUIUtils.createGuiItem(Material.CLOCK, ChatUtil.GREEN + "AutoSave", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSaveAuto)); + if (challengeSaveAuto.isDirectory() || challengeSaveZipAuto.isFile()) { + item = GUIUtils.createGuiItem(Material.CLOCK, ChatUtil.GREEN + "AutoSave", ChatUtil.GREEN + "Klicken zum laden.", ChatUtil.GREEN + "Gespeichert am " + getFileDate(challengeSaveAuto.isDirectory() ? challengeSaveAuto : challengeSaveZipAuto)); } else { item = GUIUtils.createGuiItem(Material.LIME_STAINED_GLASS_PANE, ChatUtil.GREEN + "AutoSave"); } @@ -131,6 +135,10 @@ File challengeSave2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2"); File challengeSave3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3"); File challengeSaveAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave"); + File challengeSaveZip1 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/1.zip"); + File challengeSaveZip2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2.zip"); + File challengeSaveZip3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3.zip"); + File challengeSaveZipAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave.zip"); int slot = event.getSlot(); @@ -140,11 +148,16 @@ ChatUtil.sendErrorMessage(p, "Für diesen Slot hast du keine Rechte!"); return; } - if (!challengeSave1.isDirectory()) { + ChatUtil.sendDebugMessage(p, "Klick"); + ChatUtil.sendDebugMessage(p, challengeSave1.toPath()); + ChatUtil.sendDebugMessage(p, challengeSaveZip1.toPath()); + if (!challengeSave1.isDirectory() && !challengeSaveZip1.isFile()) { return; } + ChatUtil.sendDebugMessage(p, "Test1"); if (!Server.serverPlayers.containsValue(p.getUniqueId())) { p.closeInventory(); + ChatUtil.sendDebugMessage(p, "Test2"); plugin.getPluginConfig().getOnlineChanllengeServer().ifPresentOrElse(server -> server.loadSaved(p, savePlayerID, "1"), () -> ChatUtil.sendErrorMessage(p, "Aktuell ist kein Server Frei!")); } else { p.closeInventory(); @@ -156,7 +169,7 @@ ChatUtil.sendErrorMessage(p, "Für diesen Slot brauchst du VIP!"); return; } - if (!challengeSave2.isDirectory()) { + if (!challengeSave2.isDirectory() && !challengeSaveZip2.isFile()) { return; } if (!Server.serverPlayers.containsValue(p.getUniqueId())) { @@ -172,7 +185,7 @@ ChatUtil.sendErrorMessage(p, "Für diesen Slot brauchst du VIP!"); return; } - if (!challengeSave3.isDirectory()) { + if (!challengeSave3.isDirectory() && !challengeSaveZip3.isFile()) { return; } if (!Server.serverPlayers.containsValue(p.getUniqueId())) { @@ -188,7 +201,7 @@ ChatUtil.sendErrorMessage(p, "Für diesen Slot hast du keine Rechte!"); return; } - if (!challengeSaveAuto.isDirectory()) { + if (!challengeSaveAuto.isDirectory() && !challengeSaveZipAuto.isFile()) { return; } if (!Server.serverPlayers.containsValue(p.getUniqueId())) {