Fix help not showing for invalid commands

This commit is contained in:
Eric Stokes 2011-06-03 15:34:39 -04:00
parent 8cb421b27a
commit 3651972901
4 changed files with 54 additions and 36 deletions

View File

@ -9,6 +9,7 @@ import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.command.Command;
@ -524,4 +525,29 @@ public class MultiverseCore extends JavaPlugin {
public String getTag() {
return tag;
}
/**
* This code should get moved somewhere more appropriate, but for now, it's here.
* @param env
* @return
*/
public Environment getEnvFromString(String env, CommandSender sender) {
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("HELL") || env.equalsIgnoreCase("NETHER"))
env = "NETHER";
if (env.equalsIgnoreCase("SKYLANDS") || env.equalsIgnoreCase("SKYLAND") || env.equalsIgnoreCase("STARWARS"))
env = "SKYLANDS";
if (env.equalsIgnoreCase("NORMAL") || env.equalsIgnoreCase("WORLD"))
env = "NORMAL";
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 environment;
}
}

View File

@ -18,14 +18,12 @@ import org.bukkit.command.CommandSender;
public class CommandManager {
protected List<BaseCommand> commands;
private CommandSender sender;
public CommandManager() {
commands = new ArrayList<BaseCommand>();
}
public boolean dispatch(CommandSender sender, Command command, String label, String[] args) {
this.sender = sender;
BaseCommand match = null;
String[] trimmedArgs = null;
@ -34,9 +32,12 @@ public class CommandManager {
for (BaseCommand cmd : commands) {
StringBuilder tmpIdentifier = new StringBuilder();
String[] tmpArgs = parseAllQuotedStrings(args);
if(cmd.validate(label, tmpArgs, tmpIdentifier) && tmpIdentifier.length() > identifier.length()) {
if (match == null) {
match = cmd.matchIdentifier(label) == null ? null : cmd;
}
if (match != null && cmd.validate(label, tmpArgs, tmpIdentifier) && tmpIdentifier.length() > identifier.length()) {
identifier = tmpIdentifier;
match = cmd;
trimmedArgs = tmpArgs;
}
}
@ -65,8 +66,10 @@ public class CommandManager {
public List<BaseCommand> getCommands() {
return commands;
}
/**
* Combines all quoted strings
*
* @param args
* @return
*/
@ -94,8 +97,8 @@ public class CommandManager {
}
}
// If the string was ended but had an open quote...
if(start != -1) {
//... then we want to close that quote and make that one arg.
if (start != -1) {
// ... then we want to close that quote and make that one arg.
newArgs.add(parseQuotedString(args, start, args.length));
}

View File

@ -40,19 +40,6 @@ public class CreateCommand extends BaseCommand {
seed = args[2];
}
sender.sendMessage("Stuff I found:");
sender.sendMessage(ChatColor.GREEN + "worldName" + ChatColor.WHITE + worldName);
sender.sendMessage(ChatColor.GREEN + "env " + ChatColor.WHITE + env);
sender.sendMessage(ChatColor.GREEN + "seed " + ChatColor.WHITE + seed);
// This is unreachable code, will remove next commit
// if (args.length < 2) {
// sender.sendMessage("Not enough parameters to create a new world");
// sender.sendMessage(ChatColor.RED + "/mvcreate {WORLDNAME} {ENVIRONMENT} - Create a new World.");
// sender.sendMessage(ChatColor.RED + "Example - /mvcreate world NORMAL");
// sender.sendMessage(ChatColor.RED + "Example - /mvcreate airworld SKYLANDS");
// sender.sendMessage(ChatColor.RED + "Example - /mvcreate hellworld NETHER");
// return;
// }
if (new File(worldName).exists() || this.plugin.worlds.containsKey(worldName)) {
sender.sendMessage(ChatColor.RED + "A Folder/World already exists with this name!");
sender.sendMessage(ChatColor.RED + "If you are confident it is a world you can import with /mvimport");
@ -85,19 +72,5 @@ public class CreateCommand extends BaseCommand {
}
return;
}
/**
* Takes a string array and returns a combined string, excluding the stop position, including the start
*
* @param args
* @param start
* @param stop
* @return
*/
private String parseQuotedString(String[] args, int start, int stop) {
String returnVal = args[start];
for (int i = start + 1; i < stop; i++) {
returnVal += " " + args[i];
}
return returnVal.replace("\"", "");
}
}

View File

@ -1,9 +1,11 @@
package com.onarandombox.MultiverseCore.command.commands;
import java.io.File;
import org.bukkit.ChatColor;
import org.bukkit.World.Environment;
import org.bukkit.command.CommandSender;
import com.onarandombox.MultiverseCore.MVCommandHandler;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.command.BaseCommand;
@ -21,8 +23,22 @@ public class ImportCommand extends BaseCommand {
@Override
public void execute(CommandSender sender, String[] args) {
// TODO Auto-generated method stub
String worldName = args[0];
if (this.plugin.worlds.containsKey(worldName) && new File(worldName).exists()) {
sender.sendMessage(ChatColor.RED + "Multiverse already knows about this world!");
return;
}
Environment env = plugin.getEnvFromString(args[1], sender);
// TODO: What if the folder does not exist but MV knows about the world?
if (new File(worldName).exists() && env != null) {
sender.sendMessage(ChatColor.AQUA + "Starting world import...");
plugin.addWorld(worldName, env);
sender.sendMessage(ChatColor.GREEN + "Complete!");
return;
}
}
}