diff --git a/src/main/java/de/fanta/challenges/Timer.java b/src/main/java/de/fanta/challenges/Timer.java index 9dedbd4..7ec507d 100644 --- a/src/main/java/de/fanta/challenges/Timer.java +++ b/src/main/java/de/fanta/challenges/Timer.java @@ -4,6 +4,7 @@ import de.fanta.challenges.schedular.CancellableTask; import de.fanta.challenges.utils.ChatUtil; import de.fanta.challenges.utils.Config; +import de.fanta.challenges.utils.SaveWorldUtils; import de.iani.cubesideutils.StringUtil; import net.md_5.bungee.api.ChatColor; import org.bukkit.Bukkit; @@ -98,7 +99,7 @@ idleActionBarTaskId = plugin.getScheduler().runGlobalAtFixedRate(() -> { for (Player p : Bukkit.getOnlinePlayers()) { if (!plugin.getVanish().isVanish(p)) { - p.sendActionBar(ChatUtil.RED + "" + ChatColor.BOLD + "Der Timer ist pausiert."); + sendFormatedTimerActionBarToPlayer(p, ChatUtil.RED + "" + ChatColor.BOLD + "Der Timer ist pausiert."); if (p.getGameMode() != GameMode.SPECTATOR) { p.getWorld().playEffect(p.getLocation(), Effect.ENDER_SIGNAL, 2); } @@ -112,13 +113,17 @@ if (!plugin.getVanish().isVanish(p)) { if (Config.getBoolean("showtimer")) { //p.sendActionBar(formatTime(color)); - p.sendActionBar(formateRainbowTime(!Config.getBoolean("editsettings"))); + sendFormatedTimerActionBarToPlayer(p, formateRainbowTime(!Config.getBoolean("editsettings"))); } } } } + private void sendFormatedTimerActionBarToPlayer(Player player, String text) { + player.sendActionBar(text + (SaveWorldUtils.isCopyWorld ? ChatUtil.GREEN + " Save: " + SaveWorldUtils.getProgressBar(SaveWorldUtils.progress) : "")); + } + public String formatTime(ChatColor color) { return color + formatTime(); } diff --git a/src/main/java/de/fanta/challenges/commands/hp/HPAddCommand.java b/src/main/java/de/fanta/challenges/commands/hp/HPAddCommand.java index e49c1a1..209e679 100644 --- a/src/main/java/de/fanta/challenges/commands/hp/HPAddCommand.java +++ b/src/main/java/de/fanta/challenges/commands/hp/HPAddCommand.java @@ -2,6 +2,7 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.Config; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; import org.bukkit.Bukkit; @@ -38,6 +39,14 @@ player.setHealth(finalAddHP); ChatUtil.sendNormalMessage(player, "Du wurdest von " + sender.getName() + " um " + input + "HP geheilt!"); ChatUtil.sendNormalMessage(sender, "Du hast " + player.getName() + " " + input + "HP hinzugefügt!"); + + if (!Config.getBoolean("editsettings")) { + Config.setValue("editsettings", true, false); + plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da die HP von " + player.getName() + " auf " + input + " geändert wurden."); + if (plugin.getCurrentEditor() != null) { + ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da die HP von einem Spieler bearbeitet wurden."); + } + } } else { ChatUtil.sendErrorMessage(sender, "Operation nicht möglich!"); } diff --git a/src/main/java/de/fanta/challenges/commands/hp/HPMaxCommand.java b/src/main/java/de/fanta/challenges/commands/hp/HPMaxCommand.java index 9c3345a..9594f09 100644 --- a/src/main/java/de/fanta/challenges/commands/hp/HPMaxCommand.java +++ b/src/main/java/de/fanta/challenges/commands/hp/HPMaxCommand.java @@ -2,6 +2,7 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.Config; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; import org.bukkit.Bukkit; @@ -29,6 +30,14 @@ pp.setHealth(pp.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); } ChatUtil.sendNormalMessage(sender, "Du hast die maximalen HP auf " + next + " gesetzt!"); + + if (!Config.getBoolean("editsettings")) { + Config.setValue("editsettings", true, false); + plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da die Max HP von auf " + next + " geändert wurden."); + if (plugin.getCurrentEditor() != null) { + ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da die Max HP geändert wurden."); + } + } return true; } } diff --git a/src/main/java/de/fanta/challenges/commands/hp/HPRemoveCommand.java b/src/main/java/de/fanta/challenges/commands/hp/HPRemoveCommand.java index e86b78a..c17a92b 100644 --- a/src/main/java/de/fanta/challenges/commands/hp/HPRemoveCommand.java +++ b/src/main/java/de/fanta/challenges/commands/hp/HPRemoveCommand.java @@ -2,6 +2,7 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.Config; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; import org.bukkit.Bukkit; @@ -37,6 +38,14 @@ player.setHealth(finalRemoveHP); ChatUtil.sendNormalMessage(player, sender.getName() + " hat dir " + input + " HP entfernt!"); ChatUtil.sendNormalMessage(sender, "Du hast " + player.getName() + " " + input + " HP entfernt!"); + + if (!Config.getBoolean("editsettings")) { + Config.setValue("editsettings", true, false); + plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da " + input + " HP von " + player.getName() + " abgezogen wurden"); + if (plugin.getCurrentEditor() != null) { + ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da die HP von einem Spieler bearbeitet wurden."); + } + } } else { ChatUtil.sendErrorMessage(sender, "Operation nicht möglich!"); } diff --git a/src/main/java/de/fanta/challenges/commands/hp/HPSetCommand.java b/src/main/java/de/fanta/challenges/commands/hp/HPSetCommand.java index 0bc071b..40506d1 100644 --- a/src/main/java/de/fanta/challenges/commands/hp/HPSetCommand.java +++ b/src/main/java/de/fanta/challenges/commands/hp/HPSetCommand.java @@ -2,6 +2,7 @@ import de.fanta.challenges.Challenges; import de.fanta.challenges.utils.ChatUtil; +import de.fanta.challenges.utils.Config; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; import org.bukkit.Bukkit; @@ -36,6 +37,14 @@ player.setHealth(input); ChatUtil.sendNormalMessage(player, sender.getName() + " hat deine HP auf " + input + " gesetzt!"); ChatUtil.sendNormalMessage(sender, "Du hast die HP von " + player.getName() + " auf " + input + " gesetzt!"); + + if (!Config.getBoolean("editsettings")) { + Config.setValue("editsettings", true, false); + plugin.getLogger().info("Diese Challenge ist kein SpeedRun mehr da die HP von " + player.getName() + " auf " + input + " geändert wurden."); + if (plugin.getCurrentEditor() != null) { + ChatUtil.sendWarningMessage(plugin.getCurrentEditor(), "Diese Challenge wird nicht mehr als Speed Run gezählt, da die HP von einem Spieler bearbeitet wurden."); + } + } } else { ChatUtil.sendErrorMessage(sender, "Operation nicht möglich!"); } diff --git a/src/main/java/de/fanta/challenges/guis/savegui/ChallengeSaveGui.java b/src/main/java/de/fanta/challenges/guis/savegui/ChallengeSaveGui.java index 9837144..7d75bfa 100644 --- a/src/main/java/de/fanta/challenges/guis/savegui/ChallengeSaveGui.java +++ b/src/main/java/de/fanta/challenges/guis/savegui/ChallengeSaveGui.java @@ -26,7 +26,6 @@ import java.util.HashMap; import java.util.List; import java.util.UUID; -import java.util.concurrent.ExecutionException; import java.util.logging.Level; public class ChallengeSaveGui extends AbstractWindow { @@ -51,37 +50,25 @@ File save2; File save3; File saveAuto; - File saveZip1; - File saveZip2; - File saveZip3; - File saveZipAuto; if (plugin.getServerType() != ServerType.ADVENTURE) { save1 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/1"); save2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2"); save3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3"); saveAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave"); - saveZip1 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/1.zip"); - saveZip2 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/2.zip"); - saveZip3 = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/3.zip"); - saveZipAuto = new File(plugin.getChallengeSavePath().toFile(), savePlayerID + "/autosave.zip"); } else { save1 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/1"); save2 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/2"); save3 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/3"); saveAuto = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/autosave"); - saveZip1 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/1.zip"); - saveZip2 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/2.zip"); - saveZip3 = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/3.zip"); - saveZipAuto = new File(plugin.getAdventureSavePath().toFile(), savePlayerID + "/autosave.zip"); } for (int i = 0; i < WINDOW_SIZE; i++) { ItemStack item; switch (i) { - case SAVE_1_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_1, save1, saveZip1); - case SAVE_2_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_2, save2, saveZip2); - case SAVE_3_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_3, save3, saveZip3); - case SAVE_AUTO_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_AUTO, saveAuto, saveZipAuto); + case SAVE_1_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_1, save1); + case SAVE_2_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_2, save2); + case SAVE_3_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_3, save3); + case SAVE_AUTO_INDEX -> item = getDisplayItem(player, SaveSlot.SLOT_AUTO, saveAuto); default -> item = GUIUtils.EMPTY_ICON; } this.getInventory().setItem(i, item); @@ -169,51 +156,29 @@ return StringUtil.formatDate(fileTime.toMillis()); } - private static ItemStack getDisplayItem(Player player, SaveSlot saveSlot, File save, File saveZIP) { + private static ItemStack getDisplayItem(Player player, SaveSlot saveSlot, File save) { ItemStack item; if (player.hasPermission(saveSlot.getPermission())) { - File saveFile = null; if (save.isDirectory()) { - saveFile = save; - } else if (saveZIP.isFile()) { - saveFile = saveZIP; - } - - - if (saveFile != null) { - File mapConfig = null; - if (save.isDirectory()) { - mapConfig = new File(saveFile, "/Challenges/serverconfig.yml"); - } else if (saveZIP.isFile()) { - try { - mapConfig = SaveWorldUtils.getFileFromZip(saveZIP, "/Challenges/serverconfig.yml"); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - - + File mapConfig = new File(save, "/Challenges/serverconfig.yml"); Material displayItem = null; String displayName = null; - if (mapConfig != null) { - try { - YamlConfiguration serverConfig = new YamlConfiguration(); - serverConfig.load(mapConfig); - String itemType = serverConfig.getString("displayItem.item"); - String itemName = serverConfig.getString("displayItem.name"); - if (itemType != null) { - try { - displayItem = Material.valueOf(itemType); - } catch (IllegalArgumentException ignore) { - } + try { + YamlConfiguration serverConfig = new YamlConfiguration(); + serverConfig.load(mapConfig); + String itemType = serverConfig.getString("displayItem.item"); + String itemName = serverConfig.getString("displayItem.name"); + if (itemType != null) { + try { + displayItem = Material.valueOf(itemType); + } catch (IllegalArgumentException ignore) { } - if (itemName != null) { - displayName = itemName; - } - } catch (IOException | InvalidConfigurationException ex) { - plugin.getLogger().log(Level.SEVERE, "Fehler beim laden der config", ex); } + if (itemName != null) { + displayName = itemName; + } + } catch (IOException | InvalidConfigurationException ex) { + plugin.getLogger().log(Level.SEVERE, "Fehler beim laden der config", ex); } ItemStack stack = new ItemStack(displayItem != null ? displayItem : Material.MAP); @@ -221,9 +186,9 @@ meta.setDisplayName(displayName != null ? displayName : ChatUtil.GREEN + "Save " + saveSlot.getSlot()); if (saveSlot == SaveSlot.SLOT_AUTO) { - meta.setLore(List.of(ChatUtil.RED + "AutoSave kann nicht überschrieben werden!", ChatUtil.GREEN + "Gespeichert am " + getFileDate(saveFile))); + meta.setLore(List.of(ChatUtil.RED + "AutoSave kann nicht überschrieben werden!", ChatUtil.GREEN + "Gespeichert am " + getFileDate(save))); } else { - meta.setLore(List.of(ChatUtil.GREEN + "Gespeichert am " + getFileDate(saveFile))); + meta.setLore(List.of(ChatUtil.GREEN + "Gespeichert am " + getFileDate(save))); } stack.setItemMeta(meta); diff --git a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java index 1a14e93..06b4e1e 100644 --- a/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java +++ b/src/main/java/de/fanta/challenges/utils/SaveWorldUtils.java @@ -16,30 +16,30 @@ import javax.annotation.Nullable; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; import java.util.Properties; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; -import java.util.zip.ZipOutputStream; public class SaveWorldUtils { private static final Challenges plugin = Challenges.getPlugin(); public static boolean isSavingWorld = false; + public static boolean isCopyWorld = false; private static boolean isServerRestartRequested = false; private static boolean restart = false; private static String restartSeed = null; + public static double progress = 0.0; public static boolean saveWorld(String saveID, SaveSlot saveSlot) { return saveWorld(null, saveID, saveSlot); @@ -113,7 +113,6 @@ } private static boolean copyWorldAsync(Player player, String saveID, SaveSlot saveSlot) { - ArrayList fileList = new ArrayList<>(); File dir = new File(saveID); File configs = new File("plugins/Challenges"); File end = new File("world_the_end"); @@ -137,36 +136,24 @@ FileUtils.copyDirectory(nether, savenether); FileUtils.copyDirectory(world, saveworld); - fileList.add(saveconfigs); - fileList.add(saveend); - fileList.add(savenether); - fileList.add(saveworld); - if (player != null) { ChatUtil.sendNormalMessage(player, "Welt wurde erfolgreich kopiert und wird nun gespeichert. Dies kann einige Minuten dauern. Du kannst aber ganz normal weiter spielen."); } CompletableFuture copyFuture = CompletableFuture.supplyAsync(() -> { try { - createZipFile(saveSlot.getSlot() + ".zip", fileList); - - if (dir.isDirectory()) { - FileUtils.deleteDirectory(dir); - } - - if (saveFolder.isDirectory()) { + if (!saveFolder.isDirectory()) { saveFolder.mkdirs(); } - File saveFile = new File(saveFolder, saveSlot.getSlot() + "/"); - if (saveFile.isDirectory()) { - FileUtils.deleteDirectory(saveFile); + + File saveSlotFolder = new File(saveFolder, saveSlot.getSlot()); + + if (saveSlotFolder.isDirectory()) { + FileUtils.deleteDirectory(saveSlotFolder); } - saveFile = new File(saveFolder, saveSlot.getSlot() + ".zip"); - System.out.println(saveFile.toPath()); - if (saveFile.exists()) { - FileUtils.delete(saveFile); - } - FileUtils.moveFile(new File(saveSlot.getSlot() + ".zip"), new File(saveFolder, saveSlot.getSlot() + ".zip"), StandardCopyOption.REPLACE_EXISTING); + HashMap copyMap = new HashMap<>(); + copyMap.put(dir, saveSlotFolder); + moveFolderWithProgress(copyMap); return true; } catch (IOException ex) { Bukkit.getLogger().log(Level.SEVERE, "Could not save world ", ex); @@ -201,41 +188,6 @@ return done.get(); } - public static void createZipFile(String zipFilePath, List folders) throws IOException { - byte[] buffer = new byte[1024]; - - try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath))) { - for (File folder : folders) { - if (folder.isDirectory()) { - addFolderToZip(folder, folder.getName(), zipOutputStream, buffer); - } - } - } - } - - private static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zipOutputStream, byte[] buffer) throws IOException { - File[] files = folder.listFiles(); - - if (files != null) { - for (File file : files) { - if (file.isDirectory()) { - addFolderToZip(file, parentFolder + "/" + file.getName(), zipOutputStream, buffer); - } else { - try (FileInputStream fileInputStream = new FileInputStream(file)) { - zipOutputStream.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName())); - - int length; - while ((length = fileInputStream.read(buffer)) > 0) { - zipOutputStream.write(buffer, 0, length); - } - - zipOutputStream.closeEntry(); - } - } - } - } - } - public static void restartServer(@Nullable String seed) { if (isSavingWorld) { restartSeed = seed; @@ -269,7 +221,7 @@ plugin.saveConfig(); plugin.getBackpack().saveInventoryToConfig(); - if (Config.getBoolean("firsttimerstart") && plugin.getFirstEditor() != null) { + if (Config.getBoolean("firsttimerstart") && plugin.getFirstEditor() != null && plugin.getServerType() != ServerType.CHALLENGE_EVENT) { restart = true; SaveWorldUtils.saveWorld(plugin.getFirstEditor().getUniqueId().toString(), SaveSlot.SLOT_AUTO); } else { @@ -300,16 +252,52 @@ } } - public static File getFileFromZip(File zipFilePath, String fileName) throws IOException { - try (ZipFile zipFile = new ZipFile(zipFilePath)) { - ZipEntry entry = zipFile.getEntry(fileName); + public static void moveFolderWithProgress(HashMap copyMap) throws IOException { + isCopyWorld = true; + final long[] totalBytes = {0}; + final long[] copiedBytes = {0}; - if (entry != null) { - File tempFile = File.createTempFile("temp", null); - tempFile.deleteOnExit(); - return tempFile; - } - return null; + for (File sourceFolder : copyMap.keySet()) { + Files.walk(sourceFolder.toPath()).forEach(source -> { + if (!Files.isDirectory(source)) { + totalBytes[0] += source.toFile().length(); + } + }); } + + for (File sourceFolder : copyMap.keySet()) { + Files.walk(sourceFolder.toPath()).forEach(source -> { + try { + File target = new File(copyMap.get(sourceFolder).toPath().resolve(sourceFolder.toPath().relativize(source)).toString()); + if (Files.isDirectory(source)) { + target.mkdir(); + } else { + Files.copy(source, target.toPath(), StandardCopyOption.REPLACE_EXISTING); + copiedBytes[0] += source.toFile().length(); + progress = (double) copiedBytes[0] / totalBytes[0] * 100; + } + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + isCopyWorld = false; + } + + public static String getProgressBar(double value) { + int progress = (int) (value / 100 * 40); + + StringBuilder progressBar = new StringBuilder(); + progressBar.append(ChatUtil.RED + "["); + for (int i = 0; i < 40; i++) { + if (i < progress) { + progressBar.append(ChatUtil.PINK + "|"); + } else { + progressBar.append(ChatUtil.GREEN + "|"); + } + } + progressBar.append(ChatUtil.RED + "] " + ChatUtil.PINK + String.format("%.2f", value) + "%"); + + return progressBar.toString(); } }