Rewritten commandManager

This commit is contained in:
Eric Stokes 2011-07-09 07:52:20 -06:00
parent 8f56347572
commit fc9aa456da
5 changed files with 269 additions and 97 deletions

View File

@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@ -168,21 +169,21 @@ public class MultiverseCore extends JavaPlugin {
*/
private void registerCommands() {
// Page 1
this.commandManager.addCommand(new HelpCommand(this));
this.commandManager.addCommand(new CoordCommand(this));
this.commandManager.addCommand(new TeleportCommand(this));
this.commandManager.addCommand(new ListCommand(this));
this.commandManager.addCommand(new WhoCommand(this));
this.commandManager.addCommand(new SetSpawnCommand(this));
this.commandManager.addCommand(new CreateCommand(this));
this.commandManager.addCommand(new ImportCommand(this));
this.commandManager.addCommand(new SpawnCommand(this));
this.commandManager.addCommand(new RemoveCommand(this));
this.commandManager.addCommand(new DeleteCommand(this));
this.commandManager.addCommand(new UnloadCommand(this));
this.commandManager.addCommand(new ConfirmCommand(this));
this.commandManager.addCommand(new InfoCommand(this));
this.commandManager.addCommand(new ReloadCommand(this));
// this.commandManager.addCommand(new HelpCommand(this));
// this.commandManager.addCommand(new CoordCommand(this));
// this.commandManager.addCommand(new TeleportCommand(this));
// this.commandManager.addCommand(new ListCommand(this));
// this.commandManager.addCommand(new WhoCommand(this));
// this.commandManager.addCommand(new SetSpawnCommand(this));
// this.commandManager.addCommand(new CreateCommand(this));
// this.commandManager.addCommand(new ImportCommand(this));
// this.commandManager.addCommand(new SpawnCommand(this));
// this.commandManager.addCommand(new RemoveCommand(this));
// this.commandManager.addCommand(new DeleteCommand(this));
// this.commandManager.addCommand(new UnloadCommand(this));
// this.commandManager.addCommand(new ConfirmCommand(this));
// this.commandManager.addCommand(new InfoCommand(this));
// this.commandManager.addCommand(new ReloadCommand(this));
this.commandManager.addCommand(new ModifyAddCommand(this));
this.commandManager.addCommand(new ModifySetCommand(this));
@ -462,7 +463,13 @@ public class MultiverseCore extends JavaPlugin {
sender.sendMessage("This plugin is Disabled!");
return true;
}
return this.commandManager.dispatch(sender, command, commandLabel, args);
System.out.print("Command executed!");
System.out.print(command.getName());
System.out.print(Arrays.toString(args));
ArrayList<String> allArgs = new ArrayList<String>(Arrays.asList(args));
allArgs.add(0, command.getName());
return this.commandManager.dispatch(sender, allArgs);
// return this.commandManager.dispatch(sender, command, commandLabel, args);
}
/**

View File

@ -1,6 +1,7 @@
package com.onarandombox.MultiverseCore.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.command.CommandSender;
@ -28,6 +29,7 @@ public abstract class BaseCommand {
public abstract void execute(CommandSender sender, String[] args);
@Deprecated
public boolean validate(String name, String[] parsedArgs, StringBuilder identifier) {
String match = this.matchIdentifier(name, parsedArgs);
if (match != null) {
@ -43,6 +45,15 @@ public abstract class BaseCommand {
return false;
}
public boolean validate(ArrayList<String> args) {
int argsLength = args.size();
if ((argsLength == -1 || argsLength >= this.minArgs) && (this.maxArgs == -1 || argsLength <= this.maxArgs)) {
return true;
}
return false;
}
@Deprecated
public String matchIdentifier(String input, String[] args) {
String argsString = this.getArgsString(args);
@ -63,6 +74,50 @@ public abstract class BaseCommand {
}
}
public String getIdentifier(ArrayList<String> allArgs) {
// Combines our args to a space seperated string
String argsString = this.getArgsString(allArgs);
for (String s : this.identifiers) {
String identifier = s.toLowerCase();
if (argsString.matches(identifier + "(\\s+.*|\\s*)")) {
return identifier;
}
}
return null;
}
public ArrayList<String> removeIdentifierArgs(ArrayList<String> allArgs, String identifier) {
int identifierLength = identifier.split(" ").length;
for (int i = 0; i < identifierLength; i++) {
// Since we're pulling from the front, always remove the first element
allArgs.remove(0);
}
return allArgs;
}
protected String[] removeRedundantArgs(String[] args, String command) {
System.out.print("Attempting to remove redundant args:");
System.out.print(Arrays.toString(args));
System.out.print(command);
String[] cmdSplit = command.split(" ");
// Start at cmdSplit[1], because 0 is the command name
int match = 0;
int i = 0;
while (i + 1 < cmdSplit.length && i < args.length && cmdSplit[i + 1].equalsIgnoreCase(args[i])) {
System.out.print("Found a match!");
match = i + 1;
i++;
}
ArrayList<String> newArgs = new ArrayList<String>();
for (int j = match; j < args.length; j++) {
newArgs.add(args[j]);
}
String[] mynewArr = {};
return newArgs.toArray(mynewArr);
}
@Deprecated
private String getArgsString(String[] args) {
String returnString = "";
for (String s : args) {
@ -71,6 +126,14 @@ public abstract class BaseCommand {
return returnString;
}
private String getArgsString(ArrayList<String> args) {
String returnString = "";
for (String s : args) {
returnString += s + " ";
}
return returnString.substring(0, returnString.length() - 1);
}
public List<String> getIdentifiers() {
return this.identifiers;
}
@ -98,5 +161,4 @@ public abstract class BaseCommand {
public String getPermission() {
return this.permission;
}
}

View File

@ -9,8 +9,11 @@
package com.onarandombox.MultiverseCore.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
@ -31,20 +34,65 @@ public class CommandManager {
this.plugin = plugin;
}
public boolean dispatch(CommandSender sender, ArrayList<String> allArgs) {
ArrayList<String> parsedArgs = parseAllQuotedStrings(allArgs);
String identifier = null;
Iterator<BaseCommand> iterator = this.commands.iterator();
BaseCommand foundCommand = null;
// This loop is pretty neat. It goes until either the iterator has no more
// Or until we find an identifier. When we do, we no longer iterate, and the
// value left in "foundCommand" is the command we found!
while (iterator.hasNext() && identifier == null) {
foundCommand = iterator.next();
identifier = foundCommand.getIdentifier(parsedArgs);
if (identifier != null) {
// This method, removeIdentifierArgs mutates parsedArgs
foundCommand.removeIdentifierArgs(parsedArgs, identifier);
validateAndRunCommand(sender, parsedArgs, foundCommand);
}
}
return true;
}
private void validateAndRunCommand(CommandSender sender, ArrayList<String> parsedArgs, BaseCommand foundCommand) {
if (foundCommand.validate(parsedArgs)) {
if (this.plugin.ph.hasPermission(sender, foundCommand.getPermission(), foundCommand.isOpRequired())) {
foundCommand.execute(sender, parsedArgs.toArray(new String[parsedArgs.size()]));
} else {
sender.sendMessage("You do not have permission to use this command. (" + foundCommand.getPermission() + ")");
}
} else {
sender.sendMessage(ChatColor.AQUA + "Command: " + ChatColor.WHITE + foundCommand.getName());
sender.sendMessage(ChatColor.AQUA + "Description: " + ChatColor.WHITE + foundCommand.getDescription());
sender.sendMessage(ChatColor.AQUA + "Usage: " + ChatColor.WHITE + foundCommand.getUsage());
}
}
@Deprecated
public boolean dispatch(CommandSender sender, Command command, String label, String[] args) {
BaseCommand match = null;
String[] trimmedArgs = null;
StringBuilder identifier = new StringBuilder();
String[] finalArgs = null;
for (BaseCommand cmd : this.commands) {
StringBuilder tmpIdentifier = new StringBuilder();
String[] tmpArgs = parseAllQuotedStrings(args);
System.out.print("Before Args");
System.out.print(Arrays.toString(tmpArgs));
if (match == null) {
match = cmd.matchIdentifier(label, tmpArgs) == null ? null : cmd;
// If we have a valid match, then we want to remove any extraneous words.
// For example: /mvmodiy add is the name of a command, we want the command
// to never see the "add"
if (match != null) {
finalArgs = cmd.removeRedundantArgs(tmpArgs, cmd.matchIdentifier(label, tmpArgs));
}
}
System.out.print("After Args");
System.out.print(Arrays.toString(tmpArgs));
if (match != null && cmd.validate(label, tmpArgs, tmpIdentifier) && tmpIdentifier.length() > identifier.length()) {
identifier = tmpIdentifier;
@ -54,8 +102,8 @@ public class CommandManager {
if (match != null) {
if (this.plugin.ph.hasPermission(sender, match.getPermission(), match.isOpRequired())) {
if (trimmedArgs != null) {
match.execute(sender, trimmedArgs);
if (finalArgs != null) {
match.execute(sender, finalArgs);
} else {
sender.sendMessage(ChatColor.AQUA + "Command: " + ChatColor.WHITE + match.getName());
sender.sendMessage(ChatColor.AQUA + "Description: " + ChatColor.WHITE + match.getDescription());
@ -83,8 +131,8 @@ public class CommandManager {
public List<BaseCommand> getCommands(CommandSender sender) {
ArrayList<BaseCommand> playerCommands = new ArrayList<BaseCommand>();
for(BaseCommand c : this.commands) {
if(this.plugin.ph.hasPermission(sender, c.permission, c.isOpRequired())) {
for (BaseCommand c : this.commands) {
if (this.plugin.ph.hasPermission(sender, c.permission, c.isOpRequired())) {
playerCommands.add(c);
}
}
@ -97,6 +145,7 @@ public class CommandManager {
* @param args
* @return
*/
@Deprecated
private String[] parseAllQuotedStrings(String[] args) {
// TODO: Allow '
ArrayList<String> newArgs = new ArrayList<String>();
@ -138,6 +187,7 @@ public class CommandManager {
* @param stop
* @return
*/
@Deprecated
private String parseQuotedString(String[] args, int start, int stop) {
String returnVal = args[start];
for (int i = start + 1; i < stop; i++) {
@ -146,6 +196,60 @@ public class CommandManager {
return returnVal.replace("\"", "");
}
/**
* Combines all quoted strings
*
* @param args
* @return
*/
private ArrayList<String> parseAllQuotedStrings(ArrayList<String> args) {
// TODO: Allow '
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.size(); i++) {
// If we aren't looking for an end quote, and the first part of a string is a quote
if (start == -1 && args.get(i).substring(0, 1).equals("\"")) {
start = i;
}
// Have to keep this seperate for one word quoted strings like: "fish"
if (start != -1 && args.get(i).substring(args.get(i).length() - 1, args.get(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.get(i));
}
}
// 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.
newArgs.add(parseQuotedString(args, start, args.size()));
}
return newArgs;
}
/**
* 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(ArrayList<String> args, int start, int stop) {
String returnVal = args.get(start);
for (int i = start + 1; i < stop; i++) {
returnVal += " " + args.get(i);
}
return returnVal.replace("\"", "");
}
/**
* Returns the given flag value

View File

@ -8,8 +8,6 @@ import com.onarandombox.MultiverseCore.MVWorld;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.command.BaseCommand;
// This will contain all the properties that support the ADD/REMOVE
// Anything not in here will only support the SET action
@ -20,8 +18,8 @@ public class ModifyAddCommand extends BaseCommand {
this.name = "Modify a World (Add a value)";
this.description = "Modify various aspects of worlds. See the help wiki for how to use this command properly. If you do not include a world, the current world will be used";
this.usage = "/mvmodify " + ChatColor.GREEN + "ADD {VALUE} {PROPERTY}" + ChatColor.GOLD + " [WORLD] ";
this.minArgs = 3;
this.maxArgs = 4;
this.minArgs = 2;
this.maxArgs = 3;
this.identifiers.add("mvmodify add");
this.permission = "multiverse.world.modify";
this.requiresOp = true;
@ -31,12 +29,12 @@ public class ModifyAddCommand extends BaseCommand {
public void execute(CommandSender sender, String[] args) {
// We NEED a world from the command line
Player p = null;
if (!(sender instanceof Player)) {
if (sender instanceof Player) {
p = (Player) sender;
}
if (args.length == 3 && p == null) {
sender.sendMessage("From the command line, WORLD is required.");
if (args.length == 2 && p == null) {
sender.sendMessage(ChatColor.RED + "From the command line, WORLD is required.");
sender.sendMessage(this.description);
sender.sendMessage(this.usage);
sender.sendMessage("Nothing changed.");
@ -44,13 +42,13 @@ public class ModifyAddCommand extends BaseCommand {
}
MVWorld world;
String value = args[1];
String property = args[2];
String value = args[0];
String property = args[1];
if (args.length == 3) {
if (args.length == 2) {
world = this.plugin.getMVWorld(p.getWorld().getName());
} else {
world = this.plugin.getMVWorld(args[3]);
world = this.plugin.getMVWorld(args[2]);
}
if (world == null) {

View File

@ -43,6 +43,7 @@ public class ModifyCommand extends BaseCommand {
// ModifySetCommand
// ModifyClearCommand
}
protected static boolean validateAction(Action action, String property) {
if (action == Action.Set) {
try {