diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java index b16bf239..e2a6b581 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ImportCommand.java @@ -10,6 +10,7 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.utils.WorldNameChecker; import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; import org.bukkit.World.Environment; @@ -45,28 +46,6 @@ public class ImportCommand extends MultiverseCommand { this.worldManager = this.plugin.getMVWorldManager(); } - /** - * A very basic check to see if a folder has a level.dat file. - * If it does, we can safely assume it's a world folder. - * - * @param worldFolder The File that may be a world. - * @return True if it looks like a world, false if not. - */ - private static boolean checkIfIsWorld(File worldFolder) { - if (worldFolder.isDirectory()) { - File[] files = worldFolder.listFiles(new FilenameFilter() { - @Override - public boolean accept(File file, String name) { - return name.toLowerCase().endsWith(".dat"); - } - }); - if (files != null && files.length > 0) { - return true; - } - } - return false; - } - private String getPotentialWorlds() { File worldFolder = this.plugin.getServer().getWorldContainer(); if (worldFolder == null) { @@ -84,7 +63,7 @@ public class ImportCommand extends MultiverseCommand { } ChatColor currColor = ChatColor.WHITE; for (File file : files) { - if (file.isDirectory() && checkIfIsWorld(file) && !worldStrings.contains(file.getName())) { + if (file.isDirectory() && WorldNameChecker.isValidWorldFolder(file) && !worldStrings.contains(file.getName())) { worldList += currColor + file.getName() + " "; if (currColor == ChatColor.WHITE) { currColor = ChatColor.YELLOW; @@ -152,7 +131,7 @@ public class ImportCommand extends MultiverseCommand { String worldList = this.getPotentialWorlds(); sender.sendMessage("That world folder does not exist. These look like worlds to me:"); sender.sendMessage(worldList); - } else if (!checkIfIsWorld(worldFile)) { + } else if (!WorldNameChecker.isValidWorldFolder(worldFile)) { sender.sendMessage(ChatColor.RED + "FAILED."); sender.sendMessage(String.format("'%s' does not appear to be a world. It is lacking a .dat file.", worldName)); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java index f951fe98..3979b7cb 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldManager.java @@ -130,7 +130,7 @@ public class WorldManager implements MVWorldManager { } // Check for valid world name - if (!(isValidWorldName(oldName) && isValidWorldName(newName))) { + if (!(WorldNameChecker.isValidWorldName(oldName) && WorldNameChecker.isValidWorldName(oldName))) { return false; } @@ -238,7 +238,9 @@ public class WorldManager implements MVWorldManager { return false; } - if (!isValidWorldName(name)) { + if (!WorldNameChecker.isValidWorldName(name)) { + Logging.warning("Invalid world name '" + name + "'"); + Logging.warning("World name should not contain spaces or special characters!"); return false; } @@ -424,21 +426,6 @@ public class WorldManager implements MVWorldManager { } } - /** - * Check if the world name is allowed - * - * @param name Name of the world - * @return True if the world world name is valid based on regex - */ - private boolean isValidWorldName(String name) { - if (!worldNamePattern.matcher(name).matches()) { - Logging.warning("Invalid world name '" + name + "'"); - Logging.warning("World name should not contain spaces or special characters!"); - return false; - } - return true; - } - private void brokenWorld(String name) { Logging.severe("The world '" + name + "' could NOT be loaded because it contains errors and is probably corrupt!"); Logging.severe("Try using Minecraft Region Fixer to repair your world! '" + name + "'"); diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java new file mode 100644 index 00000000..7e484167 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/WorldNameChecker.java @@ -0,0 +1,159 @@ +package com.onarandombox.MultiverseCore.utils; + +import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; + +/** + *
Utility class in helping to check the status of a world name and it's associated world folder.
+ * + *Note this is for preliminary checks and better command output. A valid result will suggest but not + * 100% determine that a world name can be created, loaded or imported.
+ */ +public class WorldNameChecker { + + private static final Pattern WORLD_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9/._-]+"); + private static final Set