From 4556b0d4c6ea75a294bf71709b62796caad10056 Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Thu, 2 Jun 2011 21:43:01 -0400 Subject: [PATCH] Teleports work with spaces, still needs refactor before merge --- .../MultiverseCore/command/BaseCommand.java | 16 ++--- .../command/CommandManager.java | 12 +--- .../command/commands/CreateCommand.java | 70 +++++-------------- .../command/commands/TeleportCommand.java | 2 +- 4 files changed, 28 insertions(+), 72 deletions(-) diff --git a/src/com/onarandombox/MultiverseCore/command/BaseCommand.java b/src/com/onarandombox/MultiverseCore/command/BaseCommand.java index f3b084e6..a956e0c9 100644 --- a/src/com/onarandombox/MultiverseCore/command/BaseCommand.java +++ b/src/com/onarandombox/MultiverseCore/command/BaseCommand.java @@ -26,22 +26,20 @@ public abstract class BaseCommand { public abstract void execute(CommandSender sender, String[] args); - public String[] validate(String input, StringBuilder identifier) { - String match = matchIdentifier(input); - + public boolean validate(String name, String[] parsedArgs, StringBuilder identifier) { + String match = matchIdentifier(name); if (match != null) { identifier = identifier.append(match); int i = identifier.length(); - String[] args = input.substring(i).trim().split(" "); - if (args[0].isEmpty()) { - args = new String[0]; + if (parsedArgs == null) { + parsedArgs = new String[0]; } - int l = args.length; + int l = parsedArgs.length; if (l >= minArgs && (maxArgs == -1 ||l <= maxArgs)) { - return args; + return true; } } - return null; + return false; } public String matchIdentifier(String input) { diff --git a/src/com/onarandombox/MultiverseCore/command/CommandManager.java b/src/com/onarandombox/MultiverseCore/command/CommandManager.java index 44848436..ee6c9a3b 100644 --- a/src/com/onarandombox/MultiverseCore/command/CommandManager.java +++ b/src/com/onarandombox/MultiverseCore/command/CommandManager.java @@ -26,10 +26,6 @@ public class CommandManager { public boolean dispatch(CommandSender sender, Command command, String label, String[] args) { this.sender = sender; - String input = label + " "; - for (String s : args) { - input += s + " "; - } BaseCommand match = null; String[] trimmedArgs = null; @@ -37,10 +33,8 @@ public class CommandManager { for (BaseCommand cmd : commands) { StringBuilder tmpIdentifier = new StringBuilder(); - String[] tmpArgs = cmd.validate(input, tmpIdentifier); - // If temp args is not null, then we'll parse the quoted strings out of it - tmpArgs = tmpArgs == null ? null : parseAllQuotedStrings(tmpArgs); - if (tmpIdentifier.length() > identifier.length()) { + String[] tmpArgs = parseAllQuotedStrings(args); + if(cmd.validate(label, tmpArgs, tmpIdentifier) && tmpIdentifier.length() > identifier.length()) { identifier = tmpIdentifier; match = cmd; trimmedArgs = tmpArgs; @@ -113,7 +107,7 @@ public class CommandManager { this.sender.sendMessage(msg); } - return args; + return results; } /** diff --git a/src/com/onarandombox/MultiverseCore/command/commands/CreateCommand.java b/src/com/onarandombox/MultiverseCore/command/commands/CreateCommand.java index ff22b44e..8f1612ac 100644 --- a/src/com/onarandombox/MultiverseCore/command/commands/CreateCommand.java +++ b/src/com/onarandombox/MultiverseCore/command/commands/CreateCommand.java @@ -20,59 +20,26 @@ public class CreateCommand extends BaseCommand { super(plugin); name = "Create World"; description = "Creates a new world of the specified type"; - // Syntax is s:SEED so we can see this variable seperately when spaces are allowed in worldnames - usage = "/mvcreate" + ChatColor.GREEN + " {NAME} {TYPE}" + ChatColor.GOLD + " [s:SEED]"; + usage = "/mvcreate" + ChatColor.GREEN + " {NAME} {TYPE}" + ChatColor.GOLD + " [SEED]"; minArgs = 2; - maxArgs = -1; + maxArgs = 3; identifiers.add("mvcreate"); } @Override public void execute(CommandSender sender, String[] args) { // TODO: Permissions, check - // TODO: Allow spaces in world names, currently this will catch it --FF int numOfParams = args.length; - // We'll search for this in a minute, if it's still -1 after the search, a user did this: - // /mvcreate "My Awesome world NETHER - // ie. they forgot to close the " - int envPosition = -1; - boolean hasSeed = false; - - // Did the user input "s? - if (args[0].substring(0, 1).equals("\"")) { - // Find the magical keyword, - for (int i = 0; i < numOfParams; i++) { - if (args[i].substring(args[i].length() - 1, args[i].length()).equals("\"") && envPosition == -1) { - // We found a word that ended with a quote, the next one MUST be the environment keyword - envPosition = i + 1; - } - } - } else { - // There can only be one word, envPosition must be 1 - envPosition = 1; + + boolean hasSeed = numOfParams == 3; + String worldName = args[0]; + String env = args[1]; + String seed = ""; + if(hasSeed) { + seed = args[2]; } - if (envPosition == -1) { - sender.sendMessage("Why are you such a failure..."); - return; - } - - // If the environment position is not the last in the list, we have a seed after it: - if (envPosition != numOfParams - 1) { - hasSeed = true; - } - String worldName = parseQuotedString(args, 0, envPosition); - String env = args[envPosition]; - // If we have a seed, parse the quotes out of it, if not, it's == "" - String seed = hasSeed ? parseQuotedString(args, envPosition + 1, numOfParams) : ""; - - if (args[numOfParams - 1].substring(0, 2).equalsIgnoreCase("s:")) { - envPosition--; - hasSeed = true; - // Pull out the seed name, drop the "s:" - seed = args[numOfParams - 1].split(":")[1]; - } sender.sendMessage("Stuff I found:"); sender.sendMessage(ChatColor.GREEN + "worldName" + ChatColor.WHITE + worldName); sender.sendMessage(ChatColor.GREEN + "env " + ChatColor.WHITE + env); @@ -94,29 +61,26 @@ public class CreateCommand extends BaseCommand { Environment environment = null; // Don't reference the enum directly as there aren't that many, and we can be more forgiving to users this way - if (env.equalsIgnoreCase("NETHER") || env.equalsIgnoreCase("HELL")) - environment = Environment.NETHER; + if (env.equalsIgnoreCase("HELL")) + env = "NETHER";; - if (env.equalsIgnoreCase("NORMAL")) - environment = Environment.NORMAL; - - if (env.equalsIgnoreCase("SKYLANDS") || env.equalsIgnoreCase("SKYLAND")) - environment = Environment.SKYLANDS; - - if (environment == null) { + if (env.equalsIgnoreCase("SKYLAND") || env.equalsIgnoreCase("STARWARS")) + env = "SKYLAND"; + try { + environment = Environment.valueOf(env); + } catch (IllegalArgumentException e) { sender.sendMessage(ChatColor.RED + "Environment type " + env + " does not exist!"); + // TODO: Show the player the mvenvironments command. return; } if (hasSeed) { - // plugin.getServer().createWorld(worldName, environment, Long.parseLong(seed)); try { plugin.addWorld(worldName, environment, Long.parseLong(seed)); } catch (NumberFormatException e) { plugin.addWorld(worldName, environment, (long) seed.hashCode()); } } else { - // plugin.getServer().createWorld(worldName, environment); plugin.addWorld(worldName, environment); } return; diff --git a/src/com/onarandombox/MultiverseCore/command/commands/TeleportCommand.java b/src/com/onarandombox/MultiverseCore/command/commands/TeleportCommand.java index 720bb23d..a53b96dd 100644 --- a/src/com/onarandombox/MultiverseCore/command/commands/TeleportCommand.java +++ b/src/com/onarandombox/MultiverseCore/command/commands/TeleportCommand.java @@ -38,7 +38,7 @@ public class TeleportCommand extends BaseCommand { Destination d = Destination.parseDestination(args[0]); // TODO: I'd like to find a way to do these next bits inside Destination, so we're always valid --FF // TODO: Support portals, but I didn't see the portals list --FF - if (this.plugin.worlds.containsKey(d.getName().toLowerCase())) { + if (this.plugin.worlds.containsKey(d.getName())) { if (d.getType() == DestinationType.World) { Location l = playerTeleporter.getSafeDestination(plugin.getServer().getWorld(d.getName()).getSpawnLocation()); p.teleport(l);