Supports CommandManager level string combining

Now when someone enters a command surrounded in quotes, it gets combined and passed as an arg with spaces! Woot.
This commit is contained in:
Eric Stokes 2011-06-02 20:58:03 -04:00
parent a5a3618439
commit 80fd19c0f8

View File

@ -16,33 +16,37 @@ import org.bukkit.command.Command;
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;
String input = label + " ";
for (String s : args) {
input += s + " ";
}
BaseCommand match = null;
String[] trimmedArgs = null;
StringBuilder identifier = new StringBuilder();
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()) {
identifier = tmpIdentifier;
match = cmd;
trimmedArgs = tmpArgs;
}
}
if (match != null) {
if (trimmedArgs != null) {
match.execute(sender, trimmedArgs);
@ -55,22 +59,61 @@ public class CommandManager {
}
return true;
}
public void addCommand(BaseCommand command) {
commands.add(command);
}
public void removeCommand(BaseCommand command) {
commands.remove(command);
}
public List<BaseCommand> getCommands() {
return commands;
}
/**
* Combines all quoted strings
* @param args
* @return
*/
private String[] parseAllQuotedStrings(String[] args) {
String[] returnVal = {};
return returnVal;
ArrayList<String> newArgs = new ArrayList<String>();
// Iterate through all command params:
// we could have: "Fish dog" the man bear pig "lives today" and maybe "even tomorrow" or "the" next day
int start = -1;
for (int i = 0; i < args.length; i++) {
// If we aren't looking for an end quote, and the first part of a string is a quote
if (start == -1 && args[i].substring(0, 1).equals("\"")) {
start = i;
}
// Have to keep this seperate for one word quoted strings like: "fish"
if (start != -1 && args[i].substring(args[i].length() - 1, args[i].length()).equals("\"")) {
// Now we've found the second part of a string, let's parse the quoted one out
// Make sure it's i+1, we still want I included
newArgs.add(parseQuotedString(args, start, i + 1));
// Reset the start to look for more!
start = -1;
} else if (start == -1) {
// This is a word that is NOT enclosed in any quotes, so just add it
newArgs.add(args[i]);
}
}
// If the string was ended but had an open quote...
if(start != -1) {
newArgs.add(parseQuotedString(args, start, args.length));
}
String[] results = newArgs.toArray(new String[newArgs.size()]);
String msg = "Here's what I have boss: [";
if (results != null) {
for (String s : results) {
msg += s + ", ";
}
msg += "]";
this.sender.sendMessage(msg);
}
return args;
}
/**