mirror of
https://github.com/aikar/commands.git
synced 2026-06-11 18:40:37 +00:00
More cleanup and refactoring
All annotations should be read for most part at registration now
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
package co.aikar.commands;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
@@ -31,147 +32,41 @@ import java.util.regex.Pattern;
|
||||
|
||||
abstract class AnnotationLookups {
|
||||
|
||||
|
||||
//
|
||||
// METHODS
|
||||
//
|
||||
|
||||
|
||||
boolean hasAnnotation(Method method, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(method, annoClass, Annotations.NOTHING) != null;
|
||||
boolean hasAnnotation(AnnotatedElement object, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(object, annoClass, Annotations.NOTHING) != null;
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Method method, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValues(method, annoClass, ACFPatterns.PIPE, Annotations.REPLACEMENTS);
|
||||
}
|
||||
String[] getAnnotationValues(Method method, Class<? extends Annotation> annoClass, Pattern pattern) {
|
||||
return getAnnotationValues(method, annoClass, pattern, Annotations.REPLACEMENTS);
|
||||
boolean hasAnnotation(AnnotatedElement object, Class<? extends Annotation> annoClass, boolean allowEmpty) {
|
||||
return getAnnotationValue(object, annoClass, Annotations.NOTHING | (allowEmpty ? 0 : Annotations.NO_EMPTY)) != null;
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Method method, Class<? extends Annotation> annoClass, int options) {
|
||||
return getAnnotationValues(method, annoClass, ACFPatterns.PIPE, options);
|
||||
String[] getAnnotationValues(AnnotatedElement object, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValues(object, annoClass, ACFPatterns.PIPE, Annotations.REPLACEMENTS);
|
||||
}
|
||||
String[] getAnnotationValues(AnnotatedElement object, Class<? extends Annotation> annoClass, Pattern pattern) {
|
||||
return getAnnotationValues(object, annoClass, pattern, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Method method, Class<? extends Annotation> annoClass, Pattern pattern, int options) {
|
||||
String value = getAnnotationValue(method, annoClass, options);
|
||||
String[] getAnnotationValues(AnnotatedElement object, Class<? extends Annotation> annoClass, int options) {
|
||||
return getAnnotationValues(object, annoClass, ACFPatterns.PIPE, options);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(AnnotatedElement object, Class<? extends Annotation> annoClass, Pattern pattern, int options) {
|
||||
String value = getAnnotationValue(object, annoClass, options);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return pattern.split(value);
|
||||
}
|
||||
|
||||
String getAnnotationValue(Method method, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(method, annoClass, Annotations.REPLACEMENTS);
|
||||
String getAnnotationValue(AnnotatedElement object, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(object, annoClass, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String getAnnotationValue(Method method, Class<? extends Annotation> annoClass, int options) {
|
||||
return getValue(method.getAnnotation(annoClass), annoClass, options);
|
||||
String getAnnotationValue(AnnotatedElement object, Class<? extends Annotation> annoClass, int options) {
|
||||
return getValue(object.getAnnotation(annoClass), annoClass, options);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// PARAMETERS
|
||||
//
|
||||
|
||||
|
||||
String[] getAnnotationValues(Parameter param, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValues(param, annoClass, ACFPatterns.PIPE, Annotations.REPLACEMENTS);
|
||||
}
|
||||
String[] getAnnotationValues(Parameter param, Class<? extends Annotation> annoClass, Pattern pattern) {
|
||||
return getAnnotationValues(param, annoClass, pattern, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Parameter param, Class<? extends Annotation> annoClass, int options) {
|
||||
return getAnnotationValues(param, annoClass, ACFPatterns.PIPE, options);
|
||||
}
|
||||
String[] getAnnotationValues(Parameter param, Class<? extends Annotation> annoClass, Pattern pattern, int options) {
|
||||
String value = getAnnotationValue(param, annoClass, options);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return pattern.split(value);
|
||||
}
|
||||
|
||||
boolean hasAnnotation(Parameter param, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(param, annoClass, Annotations.NOTHING) != null;
|
||||
}
|
||||
|
||||
String getAnnotationValue(Parameter param, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(param, annoClass, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String getAnnotationValue(Parameter param, Class<? extends Annotation> annoClass, int options) {
|
||||
return getValue(param.getAnnotation(annoClass), annoClass, options);
|
||||
}
|
||||
|
||||
//
|
||||
// FIELDS
|
||||
//
|
||||
|
||||
String[] getAnnotationValues(Field field, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValues(field, annoClass, ACFPatterns.PIPE, Annotations.REPLACEMENTS);
|
||||
}
|
||||
String[] getAnnotationValues(Field field, Class<? extends Annotation> annoClass, Pattern pattern) {
|
||||
return getAnnotationValues(field, annoClass, pattern, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Field field, Class<? extends Annotation> annoClass, int options) {
|
||||
return getAnnotationValues(field, annoClass, ACFPatterns.PIPE, options);
|
||||
}
|
||||
String[] getAnnotationValues(Field field, Class<? extends Annotation> annoClass, Pattern pattern, int options) {
|
||||
String value = getAnnotationValue(field, annoClass, options);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return pattern.split(value);
|
||||
}
|
||||
|
||||
boolean hasAnnotation(Field field, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(field, annoClass, Annotations.NOTHING) != null;
|
||||
}
|
||||
|
||||
String getAnnotationValue(Field field, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(field, annoClass, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String getAnnotationValue(Field field, Class<? extends Annotation> annoClass, int options) {
|
||||
return getValue(field.getAnnotation(annoClass), annoClass, options);
|
||||
}
|
||||
|
||||
//
|
||||
// CLASSES
|
||||
//
|
||||
|
||||
|
||||
String[] getAnnotationValues(Class<? extends Object> clazz, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValues(clazz, annoClass, ACFPatterns.PIPE, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Class<? extends Object> clazz, Class<? extends Annotation> annoClass, Pattern pattern) {
|
||||
return getAnnotationValues(clazz, annoClass, pattern, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String[] getAnnotationValues(Class<? extends Object> clazz, Class<? extends Annotation> annoClass, int options) {
|
||||
return getAnnotationValues(clazz, annoClass, ACFPatterns.PIPE, options);
|
||||
}
|
||||
String[] getAnnotationValues(Class<? extends Object> clazz, Class<? extends Annotation> annoClass, Pattern pattern, int options) {
|
||||
String value = getAnnotationValue(clazz, annoClass, options);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return pattern.split(value);
|
||||
}
|
||||
boolean hasAnnotation(Class<? extends Object> clazz, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(clazz, annoClass, Annotations.NOTHING) != null;
|
||||
}
|
||||
|
||||
String getAnnotationValue(Class<? extends Object> clazz, Class<? extends Annotation> annoClass) {
|
||||
return getAnnotationValue(clazz, annoClass, Annotations.REPLACEMENTS);
|
||||
}
|
||||
|
||||
String getAnnotationValue(Class<? extends Object> clazz, Class<? extends Annotation> annoClass, int options) {
|
||||
return getValue(clazz.getAnnotation(annoClass), annoClass, options);
|
||||
}
|
||||
|
||||
abstract String getValue(Annotation annotation, Class<? extends Annotation> annoClass, int options);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import co.aikar.commands.annotation.CatchAll;
|
||||
import co.aikar.commands.annotation.CatchUnknown;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Conditions;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.HelpCommand;
|
||||
import co.aikar.commands.annotation.PreCommand;
|
||||
@@ -82,6 +83,7 @@ public abstract class BaseCommand {
|
||||
String commandName;
|
||||
String usageMessage;
|
||||
String permission;
|
||||
String conditions;
|
||||
|
||||
private ExceptionHandler exceptionHandler = null;
|
||||
CommandOperationContext lastCommandOperationContext;
|
||||
@@ -139,7 +141,8 @@ public abstract class BaseCommand {
|
||||
this.permission = annotations.getAnnotationValue(self, CommandPermission.class, Annotations.REPLACEMENTS);
|
||||
this.description = this.commandName + " commands";
|
||||
this.usageMessage = "/" + this.commandName;
|
||||
this.parentSubcommand = getParentSubcommand(this.getClass());
|
||||
this.parentSubcommand = getParentSubcommand(self);
|
||||
this.conditions = annotations.getAnnotationValue(self, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
|
||||
registerSubcommands();
|
||||
|
||||
|
||||
@@ -54,21 +54,15 @@ public class CommandConditions <
|
||||
return this.paramConditions.put(clazz, id.toLowerCase(), handler);
|
||||
}
|
||||
|
||||
void validateConditions(CEC cec, Object value) throws InvalidCommandArgument {
|
||||
String conditions = manager.getAnnotations().getAnnotationValue(cec.getParam(), Conditions.class);
|
||||
validateConditions(conditions, cec, value);
|
||||
}
|
||||
|
||||
void validateConditions(CommandOperationContext context) throws InvalidCommandArgument {
|
||||
RegisteredCommand cmd = context.getRegisteredCommand();
|
||||
String conditions = manager.getAnnotations().getAnnotationValue(cmd.method, Conditions.class);
|
||||
validateConditions(conditions, context);
|
||||
|
||||
validateConditions(cmd.conditions, context);
|
||||
validateConditions(cmd.scope, context);
|
||||
}
|
||||
|
||||
private void validateConditions(BaseCommand scope, CommandOperationContext operationContext) throws InvalidCommandArgument {
|
||||
String conditions = manager.getAnnotations().getAnnotationValue(scope.getClass(), Conditions.class);
|
||||
validateConditions(conditions, operationContext);
|
||||
validateConditions(scope.conditions, operationContext);
|
||||
|
||||
if (scope.parentCommand != null) {
|
||||
validateConditions(scope.parentCommand, operationContext);
|
||||
@@ -100,7 +94,8 @@ public class CommandConditions <
|
||||
}
|
||||
}
|
||||
|
||||
private void validateConditions(String conditions, CEC execContext, Object value) throws InvalidCommandArgument {
|
||||
void validateConditions(CEC execContext, Object value) throws InvalidCommandArgument {
|
||||
String conditions = execContext.getCommandParameter().getConditions();
|
||||
if (conditions == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ public class CommandContexts <R extends CommandExecutionContext<?, ? extends Com
|
||||
} else {
|
||||
val = c.popFirstArg();
|
||||
}
|
||||
String split = c.getAnnotationValue(Split.class);
|
||||
String split = c.getAnnotationValue(Split.class, Annotations.NOTHING | Annotations.NO_EMPTY);
|
||||
if (split != null) {
|
||||
if (val.isEmpty()) {
|
||||
throw new InvalidCommandArgument();
|
||||
|
||||
@@ -195,6 +195,12 @@ public class CommandExecutionContext <CEC extends CommandExecutionContext, I ext
|
||||
return this.cmd;
|
||||
}
|
||||
|
||||
@UnstableAPI
|
||||
CommandParameter getCommandParameter() {
|
||||
return this.param;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Parameter getParam() {
|
||||
return this.param.getParameter();
|
||||
}
|
||||
|
||||
@@ -449,7 +449,7 @@ public abstract class CommandManager <
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (annotations.hasAnnotation(field, Dependency.class)) {
|
||||
String dependency = annotations.getAnnotationValue(field, Dependency.class);
|
||||
String key = (key = dependency).equals("") ? field.getType().getName() : key;
|
||||
String key = (key = dependency).isEmpty() ? field.getType().getName() : key;
|
||||
Object object = dependencies.row(field.getType()).get(key);
|
||||
if (object == null) {
|
||||
throw new UnresolvedDependencyException("Could not find a registered instance of " +
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
package co.aikar.commands;
|
||||
|
||||
import co.aikar.commands.annotation.Conditions;
|
||||
import co.aikar.commands.annotation.Default;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.Flags;
|
||||
@@ -50,6 +51,7 @@ public class CommandParameter <CEC extends CommandExecutionContext<CEC, ? extend
|
||||
private String description;
|
||||
private String defaultValue;
|
||||
private String syntax;
|
||||
private String conditions;
|
||||
private boolean requiresInput;
|
||||
private boolean commandIssuer;
|
||||
private String[] values;
|
||||
@@ -64,11 +66,11 @@ public class CommandParameter <CEC extends CommandExecutionContext<CEC, ? extend
|
||||
//noinspection unchecked
|
||||
this.manager = command.manager;
|
||||
this.paramIndex = paramIndex;
|
||||
CommandReplacements replacements = manager.getCommandReplacements();
|
||||
Annotations annotations = manager.getAnnotations();
|
||||
|
||||
this.defaultValue = annotations.getAnnotationValue(param, Default.class, Annotations.REPLACEMENTS | (type != String.class ? Annotations.NO_EMPTY : 0));
|
||||
this.description = annotations.getAnnotationValue(param, Description.class);
|
||||
this.conditions = annotations.getAnnotationValue(param, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
|
||||
//noinspection unchecked
|
||||
this.resolver = manager.getCommandContexts().getResolver(type);
|
||||
@@ -85,7 +87,7 @@ public class CommandParameter <CEC extends CommandExecutionContext<CEC, ? extend
|
||||
this.commandIssuer = manager.isCommandIssuer(type);
|
||||
this.canConsumeInput = !(resolver instanceof IssuerOnlyContextResolver);
|
||||
|
||||
this.values = annotations.getAnnotationValues(param, Values.class);
|
||||
this.values = annotations.getAnnotationValues(param, Values.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
|
||||
this.syntax = null;
|
||||
if (!commandIssuer) {
|
||||
@@ -244,4 +246,12 @@ public class CommandParameter <CEC extends CommandExecutionContext<CEC, ? extend
|
||||
public void setSyntax(String syntax) {
|
||||
this.syntax = syntax;
|
||||
}
|
||||
|
||||
public String getConditions() {
|
||||
return conditions;
|
||||
}
|
||||
|
||||
public void setConditions(String conditions) {
|
||||
this.conditions = conditions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,12 +28,10 @@ import co.aikar.commands.annotation.HelpSearchTags;
|
||||
public class HelpEntry {
|
||||
|
||||
private final RegisteredCommand command;
|
||||
private final String searchTags;
|
||||
private int searchScore = 1;
|
||||
|
||||
HelpEntry(RegisteredCommand command) {
|
||||
this.command = command;
|
||||
this.searchTags = command.manager.getAnnotations().getAnnotationValue(command.method, HelpSearchTags.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
}
|
||||
|
||||
RegisteredCommand getRegisteredCommand() {
|
||||
@@ -66,6 +64,6 @@ public class HelpEntry {
|
||||
}
|
||||
|
||||
public String getSearchTags() {
|
||||
return searchTags;
|
||||
return command.helpSearchTags;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@ package co.aikar.commands;
|
||||
import co.aikar.commands.annotation.CommandAlias;
|
||||
import co.aikar.commands.annotation.CommandCompletion;
|
||||
import co.aikar.commands.annotation.CommandPermission;
|
||||
import co.aikar.commands.annotation.Conditions;
|
||||
import co.aikar.commands.annotation.Description;
|
||||
import co.aikar.commands.annotation.HelpSearchTags;
|
||||
import co.aikar.commands.annotation.Syntax;
|
||||
import co.aikar.commands.contexts.ContextResolver;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
@@ -48,19 +50,22 @@ import java.util.stream.Collectors;
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class RegisteredCommand <CEC extends CommandExecutionContext<CEC, ? extends CommandIssuer>> {
|
||||
final BaseCommand scope;
|
||||
final String command;
|
||||
final Method method;
|
||||
final String prefSubCommand;
|
||||
final CommandParameter<CEC>[] parameters;
|
||||
final String syntaxText;
|
||||
final String helpText;
|
||||
final CommandManager manager;
|
||||
final List<String> registeredSubcommands = new ArrayList<>();
|
||||
|
||||
String command;
|
||||
String prefSubCommand;
|
||||
String syntaxText;
|
||||
String helpText;
|
||||
String permission;
|
||||
String complete;
|
||||
String conditions;
|
||||
|
||||
private final String permission;
|
||||
final String complete;
|
||||
final int requiredResolvers;
|
||||
final int optionalResolvers;
|
||||
final List<String> registeredSubcommands = new ArrayList<>();
|
||||
final CommandManager manager;
|
||||
public String helpSearchTags;
|
||||
|
||||
RegisteredCommand(BaseCommand scope, String command, Method method, String prefSubCommand) {
|
||||
this.scope = scope;
|
||||
@@ -70,13 +75,15 @@ public class RegisteredCommand <CEC extends CommandExecutionContext<CEC, ? exten
|
||||
if (BaseCommand.CATCHUNKNOWN.equals(prefSubCommand) || BaseCommand.DEFAULT.equals(prefSubCommand)) {
|
||||
prefSubCommand = "";
|
||||
}
|
||||
this.command = command + (!annotations.hasAnnotation(method, CommandAlias.class) && !prefSubCommand.isEmpty() ? prefSubCommand : "");
|
||||
this.command = command + (!annotations.hasAnnotation(method, CommandAlias.class, false) && !prefSubCommand.isEmpty() ? prefSubCommand : "");
|
||||
this.method = method;
|
||||
this.prefSubCommand = prefSubCommand;
|
||||
|
||||
this.permission = annotations.getAnnotationValue(method, CommandPermission.class);
|
||||
this.permission = annotations.getAnnotationValue(method, CommandPermission.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
this.complete = annotations.getAnnotationValue(method, CommandCompletion.class);
|
||||
this.helpText = annotations.getAnnotationValue(method, Description.class);
|
||||
this.helpText = annotations.getAnnotationValue(method, Description.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
this.conditions = annotations.getAnnotationValue(method, Conditions.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
this.helpSearchTags = annotations.getAnnotationValue(method, HelpSearchTags.class, Annotations.REPLACEMENTS | Annotations.NO_EMPTY);
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
//noinspection unchecked
|
||||
|
||||
Reference in New Issue
Block a user