From f8fbd295eece0114136e7d16a26ef27601de5bc9 Mon Sep 17 00:00:00 2001 From: TraceL Date: Mon, 15 Aug 2022 01:36:20 +0700 Subject: [PATCH 1/5] Use CBWorld's spawn when regenerating world with a new seed --- .../com/onarandombox/MultiverseCore/utils/WorldManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java index 0b2ab0701..019a9de75 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -971,6 +971,11 @@ public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, S } } + // If using a new seed, you will most definitely want the same spawn the world uses. + if (useNewSeed && !world.getSpawnLocation().equals(world.getCBWorld().getSpawnLocation())) { + world.setSpawnLocation(world.getCBWorld().getSpawnLocation()); + } + // Send all players that were in the old world, BACK to it! SafeTTeleporter teleporter = this.plugin.getSafeTTeleporter(); Location newSpawn = world.getSpawnLocation(); From 4d61d2dc50e640881b4de464495c6b3f024e2904 Mon Sep 17 00:00:00 2001 From: TraceL Date: Mon, 15 Aug 2022 02:15:25 +0700 Subject: [PATCH 2/5] Add an argument option to MVWorldManager#deleteWorld that chooses what to keep when deleting a world. --- .../MultiverseCore/api/MVWorldManager.java | 6 +++--- .../MultiverseCore/enums/KeepWorld.java | 19 +++++++++++++++++++ .../MultiverseCore/utils/FileUtils.java | 13 +++++++++++++ .../MultiverseCore/utils/WorldManager.java | 19 +++++++++++++++---- 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index 599ce7cb9..b2bd40558 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -7,6 +7,7 @@ package com.onarandombox.MultiverseCore.api; +import com.onarandombox.MultiverseCore.enums.KeepWorld; import com.onarandombox.MultiverseCore.utils.PurgeWorlds; import com.onarandombox.MultiverseCore.utils.SimpleWorldPurger; import org.bukkit.World; @@ -108,11 +109,10 @@ boolean addWorld(String name, Environment env, String seedString, WorldType type * @param name The name of the world to remove * @param removeFromConfig If true(default), we'll remove the entries from the * config. If false, they'll stay and the world may come back. - * @param deleteWorldFolder If true the world folder will be completely deleted. If false - * only the contents of the world folder will be deleted + * @param keepContents The files you want to keep. (Paper configuration, World directory, or nothing) * @return True if success, false if failure. */ - boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder); + boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents); /** * Unload a world from Multiverse. diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java b/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java new file mode 100644 index 000000000..d060d0a49 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java @@ -0,0 +1,19 @@ +package com.onarandombox.MultiverseCore.enums; + +/** + * Custom enum for method argument in deciding what to keep when deleting world files. + */ +public enum KeepWorld { + /** + * Keep paper's config file if it exists. + */ + CONFIG, + /** + * Keeps the directory of the world folder, delete all the contents. + */ + FOLDER, + /** + * Delete everything, including the directory. + */ + NONE +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index 571143bb8..a13a610eb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -68,6 +68,19 @@ public static boolean deleteFolderContents(File file) { } } + public static boolean deleteWorldContents(File file) { + try (Stream files = Files.walk(file.toPath())){ + files.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .filter(f -> !f.equals(file) && !f.getName().equals("paper-world.yml")) + .forEach(File::delete); + return true; + } catch (IOException e) { + Logging.warning(e.getMessage()); + return false; + } + } + /** * Helper method to copy the world-folder. * @param source Source-File diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java index 019a9de75..56502f5af 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -16,6 +16,7 @@ import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; +import com.onarandombox.MultiverseCore.enums.KeepWorld; import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent; import org.bukkit.Bukkit; import org.bukkit.GameRule; @@ -505,7 +506,7 @@ private boolean doLoad(WorldCreator creator, boolean ignoreExists) { * {@inheritDoc} */ @Override - public boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder) { + public boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents) { if (this.hasUnloadedWorld(name, false)) { // Attempt to load if unloaded so we can actually delete the world if (!this.doLoad(name)) { @@ -540,7 +541,17 @@ public boolean deleteWorld(String name, boolean removeFromConfig, boolean delete try { File worldFile = world.getWorldFolder(); Logging.finer("deleteWorld(): worldFile: " + worldFile.getAbsolutePath()); - if (deleteWorldFolder ? FileUtils.deleteFolder(worldFile) : FileUtils.deleteFolderContents(worldFile)) { + boolean success; + + if (keepContents.equals(KeepWorld.FOLDER)) { + success = FileUtils.deleteFolderContents(worldFile); + } else if (keepContents.equals(KeepWorld.CONFIG)) { + success = FileUtils.deleteWorldContents(worldFile); + } else { + success = FileUtils.deleteFolder(worldFile); + } + + if (success) { Logging.info("World '%s' was DELETED.", name); return true; } else { @@ -564,7 +575,7 @@ public boolean deleteWorld(String name, boolean removeFromConfig, boolean delete */ @Override public boolean deleteWorld(String name, boolean removeFromConfig) { - return this.deleteWorld(name, removeFromConfig, true); + return this.deleteWorld(name, removeFromConfig, KeepWorld.NONE); } /** @@ -947,7 +958,7 @@ public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, S } // Do the regen. - if (!this.deleteWorld(name, false, false)) { + if (!this.deleteWorld(name, false, KeepWorld.CONFIG)) { Logging.severe("Unable to regen world as world cannot be deleted."); return false; } From 4c52bcbddbc118f9ef5bde6563f4038640bf100e Mon Sep 17 00:00:00 2001 From: TraceL Date: Mon, 15 Aug 2022 12:30:20 +0700 Subject: [PATCH 3/5] Revert changes to API - Add an overload method for choosing what to delete in MVWorldManager#deleteWorld - Deprecated deleteWorld method - Renamed KeepWorld to WorldDeleteMode --- .../MultiverseCore/api/MVWorldManager.java | 19 ++++++++++++++--- .../MultiverseCore/enums/KeepWorld.java | 19 ----------------- .../MultiverseCore/enums/WorldDeleteMode.java | 19 +++++++++++++++++ .../MultiverseCore/utils/WorldManager.java | 21 ++++++++++++------- 4 files changed, 49 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/enums/WorldDeleteMode.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java index b2bd40558..ee2c149f0 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/MVWorldManager.java @@ -7,7 +7,7 @@ package com.onarandombox.MultiverseCore.api; -import com.onarandombox.MultiverseCore.enums.KeepWorld; +import com.onarandombox.MultiverseCore.enums.WorldDeleteMode; import com.onarandombox.MultiverseCore.utils.PurgeWorlds; import com.onarandombox.MultiverseCore.utils.SimpleWorldPurger; import org.bukkit.World; @@ -109,10 +109,23 @@ boolean addWorld(String name, Environment env, String seedString, WorldType type * @param name The name of the world to remove * @param removeFromConfig If true(default), we'll remove the entries from the * config. If false, they'll stay and the world may come back. - * @param keepContents The files you want to keep. (Paper configuration, World directory, or nothing) + * @param deleteWorldFolder If true, the world folder will be completely deleted. If false, + * only the contents of the world folder will be deleted * @return True if success, false if failure. + * @deprecated Use {@link MVWorldManager#deleteWorld(String, boolean, WorldDeleteMode)} */ - boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents); + @Deprecated + boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder); + + /** + * + * @param name The world name + * @param removeFromConfig If true(default), we'll remove the entires from the + * config. If false, they'll stay and the world may come back. + * @param whatToDelete What files or folders should be deleted. + * @return True if success, false if failure. + */ + boolean deleteWorld(String name, boolean removeFromConfig, WorldDeleteMode whatToDelete); /** * Unload a world from Multiverse. diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java b/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java deleted file mode 100644 index d060d0a49..000000000 --- a/src/main/java/com/onarandombox/MultiverseCore/enums/KeepWorld.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.onarandombox.MultiverseCore.enums; - -/** - * Custom enum for method argument in deciding what to keep when deleting world files. - */ -public enum KeepWorld { - /** - * Keep paper's config file if it exists. - */ - CONFIG, - /** - * Keeps the directory of the world folder, delete all the contents. - */ - FOLDER, - /** - * Delete everything, including the directory. - */ - NONE -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/WorldDeleteMode.java b/src/main/java/com/onarandombox/MultiverseCore/enums/WorldDeleteMode.java new file mode 100644 index 000000000..7f1f5ce97 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/enums/WorldDeleteMode.java @@ -0,0 +1,19 @@ +package com.onarandombox.MultiverseCore.enums; + +/** + * Custom enum for method argument in deciding what to delete when deleting a world + */ +public enum WorldDeleteMode { + /** + * Delete all contents in the directory except for Paper's configuration file. + */ + WORLD, + /** + * Delete all contents in the directory. + */ + CONFIG_AND_WORLD, + /** + * Delete everything, including the directory. + */ + ALL +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java index 56502f5af..0560aeb2b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -16,7 +16,7 @@ import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.api.SafeTTeleporter; import com.onarandombox.MultiverseCore.api.WorldPurger; -import com.onarandombox.MultiverseCore.enums.KeepWorld; +import com.onarandombox.MultiverseCore.enums.WorldDeleteMode; import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent; import org.bukkit.Bukkit; import org.bukkit.GameRule; @@ -49,7 +49,6 @@ import java.util.Stack; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; -import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -506,7 +505,7 @@ private boolean doLoad(WorldCreator creator, boolean ignoreExists) { * {@inheritDoc} */ @Override - public boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keepContents) { + public boolean deleteWorld(String name, boolean removeFromConfig, WorldDeleteMode whatToDelete) { if (this.hasUnloadedWorld(name, false)) { // Attempt to load if unloaded so we can actually delete the world if (!this.doLoad(name)) { @@ -543,9 +542,9 @@ public boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keep Logging.finer("deleteWorld(): worldFile: " + worldFile.getAbsolutePath()); boolean success; - if (keepContents.equals(KeepWorld.FOLDER)) { + if (whatToDelete.equals(WorldDeleteMode.CONFIG_AND_WORLD)) { success = FileUtils.deleteFolderContents(worldFile); - } else if (keepContents.equals(KeepWorld.CONFIG)) { + } else if (whatToDelete.equals(WorldDeleteMode.WORLD)) { success = FileUtils.deleteWorldContents(worldFile); } else { success = FileUtils.deleteFolder(worldFile); @@ -570,12 +569,20 @@ public boolean deleteWorld(String name, boolean removeFromConfig, KeepWorld keep } } + /** + * {@inheritDoc} + */ + @Override + public boolean deleteWorld(String name, boolean removeFromConfig, boolean deleteWorldFolder) { + return this.deleteWorld(name, removeFromConfig, deleteWorldFolder ? WorldDeleteMode.ALL : WorldDeleteMode.CONFIG_AND_WORLD); + } + /** * {@inheritDoc} */ @Override public boolean deleteWorld(String name, boolean removeFromConfig) { - return this.deleteWorld(name, removeFromConfig, KeepWorld.NONE); + return this.deleteWorld(name, removeFromConfig, true); } /** @@ -958,7 +965,7 @@ public boolean regenWorld(String name, boolean useNewSeed, boolean randomSeed, S } // Do the regen. - if (!this.deleteWorld(name, false, KeepWorld.CONFIG)) { + if (!this.deleteWorld(name, false, WorldDeleteMode.WORLD)) { Logging.severe("Unable to regen world as world cannot be deleted."); return false; } From 1946763a2961b87c606698b8daeaf4a96a79ca7c Mon Sep 17 00:00:00 2001 From: TraceL Date: Wed, 17 Aug 2022 01:24:44 +0700 Subject: [PATCH 4/5] Fix a bug with arg handling in RegenCommand If you do `/mv regen worldName -s --keep-gamerules`, the plugin will think that "--keep-gamerules" is a seed we're supplying. That shouldn't happen. --- .../com/onarandombox/MultiverseCore/commands/RegenCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java index 4efe7ad51..f21f56dbf 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java @@ -41,7 +41,8 @@ public RegenCommand(MultiverseCore plugin) { public void runCommand(CommandSender sender, List args) { String worldName = args.get(0); boolean useseed = (!(args.size() == 1)); - boolean randomseed = (args.size() == 2 && args.get(1).equalsIgnoreCase("-s")); + boolean randomseed = (args.size() == 2 && args.get(1).equalsIgnoreCase("-s")) || + (args.size() == 3 && args.get(2).equals("--keep-gamerules")); String seed = (args.size() == 3) ? args.get(2) : ""; boolean keepGamerules = CommandHandler.hasFlag("--keep-gamerules", args); this.plugin.getCommandQueueManager().addToQueue(new QueuedCommand( From 874ca9d4beafedf0b40758c9ec03eab6bc930da8 Mon Sep 17 00:00:00 2001 From: TraceL Date: Wed, 17 Aug 2022 16:55:11 +0700 Subject: [PATCH 5/5] use equalsIgnoreCase instead to match with CommandHandler#hasFlag --- .../com/onarandombox/MultiverseCore/commands/RegenCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java index f21f56dbf..8361ed517 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/RegenCommand.java @@ -42,7 +42,7 @@ public void runCommand(CommandSender sender, List args) { String worldName = args.get(0); boolean useseed = (!(args.size() == 1)); boolean randomseed = (args.size() == 2 && args.get(1).equalsIgnoreCase("-s")) || - (args.size() == 3 && args.get(2).equals("--keep-gamerules")); + (args.size() == 3 && args.get(2).equalsIgnoreCase("--keep-gamerules")); String seed = (args.size() == 3) ? args.get(2) : ""; boolean keepGamerules = CommandHandler.hasFlag("--keep-gamerules", args); this.plugin.getCommandQueueManager().addToQueue(new QueuedCommand(