Plex-FAWE/src/com/sk89q/minecraft/util/commands/CommandsManager.java

385 lines
13 KiB
Java
Raw Normal View History

2011-01-29 10:05:22 +00:00
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.minecraft.util.commands;
2011-01-29 10:05:22 +00:00
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
2011-01-29 10:05:22 +00:00
import java.util.Map;
import java.util.Set;
2011-02-01 10:03:18 +00:00
import com.sk89q.util.StringUtil;
2011-01-29 10:05:22 +00:00
/**
* Manager for handling commands. This allows you to easily process commands,
* including nested commands, by correctly annotating methods of a class.
* The commands are thus declaratively defined, and it's easy to spot
* how permissions and commands work out, and it decreases the opportunity
* for errors because the consistency would cause any odd balls to show.
* The manager also handles some boilerplate code such as number of arguments
* checking and printing usage.
*
* <p>To use this, it is merely a matter of registering classes containing
* the commands (as methods with the proper annotations) with the
* manager. When you want to process a command, use one of the
* <code>execute</code> methods. If something is wrong, such as incorrect
* usage, insufficient permissions, or a missing command altogether, an
* exception will be raised for upstream handling.
*
* <p>To mark a method as a command, use {@link Command}. For nested commands,
* see {@link NestedCommand}. To handle permissions, use
* {@link CommandPermissions}.
*
* <p>This uses Java reflection extensively, but to reduce the overhead of
* reflection, command lookups are completely cached on registration. This
* allows for fast command handling. Method invocation still has to be done
* with reflection, but this is quite fast in that of itself.
2011-01-29 10:05:22 +00:00
*
* @author sk89q
* @param <T> command sender class
2011-01-29 10:05:22 +00:00
*/
public abstract class CommandsManager<T> {
2011-01-29 10:05:22 +00:00
/**
* Mapping of commands (including aliases) with a description. Root
* commands are stored under a key of null, whereas child commands are
* cached under their respective {@link Method}.
2011-01-29 10:05:22 +00:00
*/
protected Map<Method, Map<String, Method>> commands
2011-02-01 10:03:18 +00:00
= new HashMap<Method, Map<String, Method>>();
2011-01-29 10:05:22 +00:00
/**
* Mapping of commands (not including aliases) with a description.
*/
protected Map<String, String> descs = new HashMap<String, String>();
2011-01-29 10:05:22 +00:00
/**
* Register an object that contains commands (denoted by
* {@link Command}. The methods are
2011-01-29 10:05:22 +00:00
* cached into a map for later usage and it reduces the overhead of
* reflection (method lookup via reflection is relatively slow).
*
* @param cls
*/
public void register(Class<?> cls) {
2011-02-01 10:03:18 +00:00
registerMethods(cls, null);
}
/**
* Register the methods of a class.
*
* @param cls
* @param parent
*/
private void registerMethods(Class<?> cls, Method parent) {
Map<String, Method> map;
// Make a new hash map to cache the commands for this class
// as looking up methods via reflection is fairly slow
2011-02-01 10:03:18 +00:00
if (commands.containsKey(parent)) {
map = commands.get(parent);
} else {
map = new HashMap<String, Method>();
commands.put(parent, map);
}
2011-01-29 10:05:22 +00:00
for (Method method : cls.getMethods()) {
if (!method.isAnnotationPresent(Command.class)) {
continue;
}
Command cmd = method.getAnnotation(Command.class);
// Cache the aliases too
2011-01-29 10:05:22 +00:00
for (String alias : cmd.aliases()) {
2011-02-01 10:03:18 +00:00
map.put(alias, method);
2011-01-29 10:05:22 +00:00
}
// Build a list of commands and their usage details, at least for
// root level commands
if (parent == null) {
if (cmd.usage().length() == 0) {
descs.put(cmd.aliases()[0], cmd.desc());
} else {
descs.put(cmd.aliases()[0], cmd.usage() + " - " + cmd.desc());
}
2011-01-29 10:05:22 +00:00
}
2011-02-01 10:03:18 +00:00
// Look for nested commands -- if there are any, those have
// to be cached too so that they can be quickly looked
// up when processing commands
2011-02-01 10:03:18 +00:00
if (method.isAnnotationPresent(NestedCommand.class)) {
NestedCommand nestedCmd = method.getAnnotation(NestedCommand.class);
for (Class<?> nestedCls : nestedCmd.value()) {
registerMethods(nestedCls, method);
}
}
2011-01-29 10:05:22 +00:00
}
}
/**
* Checks to see whether there is a command named such at the root level.
* This will check aliases as well.
2011-01-29 10:05:22 +00:00
*
* @param command
* @return
*/
public boolean hasCommand(String command) {
2011-02-01 10:03:18 +00:00
return commands.get(null).containsKey(command.toLowerCase());
2011-01-29 10:05:22 +00:00
}
/**
* Get a list of command descriptions. This is only for root commands.
2011-01-29 10:05:22 +00:00
*
* @return
*/
public Map<String, String> getCommands() {
return descs;
}
/**
* Get the usage string for a command.
*
2011-02-01 10:03:18 +00:00
* @param args
* @param level
* @param cmd
* @return
*/
protected String getUsage(String[] args, int level, Command cmd) {
2011-02-01 10:03:18 +00:00
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++) {
command.append(args[i] + " ");
}
command.append(cmd.flags().length() > 0 ? "[-" + cmd.flags() + "] " : "");
command.append(cmd.usage());
return command.toString();
}
/**
* Get the usage string for a nested command.
*
* @param args
* @param level
* @param method
* @param player
2011-02-01 10:03:18 +00:00
* @return
* @throws CommandException
2011-02-01 10:03:18 +00:00
*/
protected String getNestedUsage(String[] args, int level,
Method method, T player) throws CommandException {
2011-02-01 10:03:18 +00:00
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++) {
command.append(args[i] + " ");
}
Map<String, Method> map = commands.get(method);
boolean found = false;
2011-02-01 10:03:18 +00:00
command.append("<");
Set<String> allowedCommands = new HashSet<String>();
2011-02-01 10:03:18 +00:00
for (Map.Entry<String, Method> entry : map.entrySet()) {
Method childMethod = entry.getValue();
found = true;
2011-02-01 10:03:18 +00:00
if (hasPermission(childMethod, player)) {
Command childCmd = childMethod.getAnnotation(Command.class);
allowedCommands.add(childCmd.aliases()[0]);
}
}
if (allowedCommands.size() > 0) {
command.append(StringUtil.joinString(allowedCommands, "|", 0));
} else {
if (!found) {
command.append("?");
} else {
//command.append("action");
throw new CommandPermissionsException();
}
2011-02-01 10:03:18 +00:00
}
command.append(">");
return command.toString();
}
/**
* Attempt to execute a command. This version takes a separate command
* name (for the root command) and then a list of following arguments.
*
* @param cmd command to run
* @param args arguments
* @param player command source
* @param methodArgs method arguments
* @throws CommandException
*/
public void execute(String cmd, String[] args, T player,
Object ... methodArgs) throws CommandException {
String[] newArgs = new String[args.length + 1];
System.arraycopy(args, 0, newArgs, 1, args.length);
newArgs[0] = cmd;
Object[] newMethodArgs = new Object[methodArgs.length + 1];
System.arraycopy(methodArgs, 0, newMethodArgs, 1, methodArgs.length);
executeMethod(null, newArgs, player, newMethodArgs, 0);
}
2011-01-29 10:05:22 +00:00
/**
* Attempt to execute a command.
*
* @param args
* @param player
* @param methodArgs
* @throws CommandException
2011-01-29 10:05:22 +00:00
*/
public void execute(String[] args, T player,
Object ... methodArgs) throws CommandException {
Object[] newMethodArgs = new Object[methodArgs.length + 1];
System.arraycopy(methodArgs, 0, newMethodArgs, 1, methodArgs.length);
executeMethod(null, args, player, newMethodArgs, 0);
2011-02-01 10:03:18 +00:00
}
/**
* Attempt to execute a command.
*
* @param parent
* @param args
* @param player
* @param methodArgs
2011-02-01 10:03:18 +00:00
* @param level
* @throws CommandException
2011-02-01 10:03:18 +00:00
*/
public void executeMethod(Method parent, String[] args,
T player, Object[] methodArgs, int level) throws CommandException {
2011-02-01 10:03:18 +00:00
String cmdName = args[level];
Map<String, Method> map = commands.get(parent);
Method method = map.get(cmdName.toLowerCase());
2011-01-29 10:05:22 +00:00
if (method == null) {
2011-02-01 10:03:18 +00:00
if (parent == null) { // Root
throw new UnhandledCommandException();
2011-02-01 10:03:18 +00:00
} else {
throw new MissingNestedCommandException("Unknown command: " + cmdName,
getNestedUsage(args, level - 1, parent, player));
2011-02-01 10:03:18 +00:00
}
2011-01-29 10:05:22 +00:00
}
if (!hasPermission(method, player)) {
throw new CommandPermissionsException();
2011-01-29 10:05:22 +00:00
}
2011-02-01 10:03:18 +00:00
int argsCount = args.length - 1 - level;
if (method.isAnnotationPresent(NestedCommand.class)) {
if (argsCount == 0) {
throw new MissingNestedCommandException("Sub-command required.",
getNestedUsage(args, level, method, player));
2011-02-01 10:03:18 +00:00
} else {
executeMethod(method, args, player, methodArgs, level + 1);
2011-02-01 10:03:18 +00:00
}
} else {
Command cmd = method.getAnnotation(Command.class);
String[] newArgs = new String[args.length - level];
System.arraycopy(args, level, newArgs, 0, args.length - level);
CommandContext context = new CommandContext(newArgs);
if (context.argsLength() < cmd.min()) {
throw new CommandUsageException("Too few arguments.",
getUsage(args, level, cmd));
2011-02-01 10:03:18 +00:00
}
if (cmd.max() != -1 && context.argsLength() > cmd.max()) {
throw new CommandUsageException("Too many arguments.",
getUsage(args, level, cmd));
2011-02-01 10:03:18 +00:00
}
for (char flag : context.getFlags()) {
if (cmd.flags().indexOf(String.valueOf(flag)) == -1) {
throw new CommandUsageException("Unknown flag: " + flag,
getUsage(args, level, cmd));
2011-02-01 10:03:18 +00:00
}
}
methodArgs[0] = context;
2011-02-01 10:03:18 +00:00
try {
method.invoke(null, methodArgs);
2011-02-01 10:03:18 +00:00
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
throw new WrappedCommandException(e.getCause());
2011-02-01 10:03:18 +00:00
}
2011-01-29 10:05:22 +00:00
}
2011-02-01 10:03:18 +00:00
}
/**
* Returns whether a player has access to a command.
2011-02-01 10:03:18 +00:00
*
* @param method
* @param player
* @return
*/
protected boolean hasPermission(Method method, T player) {
CommandPermissions perms = method.getAnnotation(CommandPermissions.class);
if (perms == null) {
2011-01-29 10:05:22 +00:00
return true;
}
2011-02-01 10:03:18 +00:00
for (String perm : perms.value()) {
if (hasPermission(player, perm)) {
return true;
}
}
2011-02-01 10:03:18 +00:00
return false;
2011-01-29 10:05:22 +00:00
}
2011-02-01 10:03:18 +00:00
/**
* Returns whether a player permission..
2011-02-01 10:03:18 +00:00
*
* @param player
* @param perm
2011-02-01 10:03:18 +00:00
* @return
*/
public abstract boolean hasPermission(T player, String perm);
2011-01-29 10:05:22 +00:00
}