CommandFramework3 #94
@ -22,13 +22,11 @@ package de.steamwar.command;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
import static de.steamwar.command.SWCommandUtils.getAnnotation;
|
||||
|
||||
@ -79,81 +77,63 @@ public abstract class SWCommand {
|
||||
|
||||
Set<Method> commandMethods = new HashSet<>();
|
||||
for (Method method : getClass().getDeclaredMethods()) {
|
||||
addMapper(method);
|
||||
addCommand(method, commandMethods);
|
||||
addHelp(method);
|
||||
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, method1, parameters) -> {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
Object object = method.invoke(this);
|
||||
SWCommandUtils.addMapper(anno.value(), (TypeMapper<?>) object);
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
add(Register.class, method, i -> i == 0, true, null, (anno, method1, parameters) -> {
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
Class<?> clazz = parameter.getType();
|
||||
if (parameter.isVarArgs() && i == parameters.length - 1) {
|
||||
clazz = parameter.getType().getComponentType();
|
||||
}
|
||||
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
||||
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
|
||||
continue;
|
||||
}
|
||||
String name = clazz.getTypeName();
|
||||
if (mapper != null) {
|
||||
name = mapper.value();
|
||||
}
|
||||
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
|
||||
}
|
||||
commandMethods.add(method);
|
||||
});
|
||||
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, method1, parameters) -> {
|
||||
commandHelpSet.add(new SubCommand(this, method));
|
||||
});
|
||||
}
|
||||
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
|
||||
}
|
||||
|
||||
private void addCommand(Method method, Set<Method> commandMethods) {
|
||||
Register register = getAnnotation(method, Register.class);
|
||||
if (register == null) {
|
||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, SWConsumer<T> consumer) {
|
||||
T anno = getAnnotation(method, annotation);
|
||||
if (anno == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (parameters.length == 0) {
|
||||
if (parameterTester.test(parameters.length)) {
|
||||
return;
|
||||
}
|
||||
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||
return;
|
||||
}
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
Class<?> clazz = parameter.getType();
|
||||
if (parameter.isVarArgs() && i == parameters.length - 1) {
|
||||
clazz = parameter.getType().getComponentType();
|
||||
}
|
||||
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
||||
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
|
||||
continue;
|
||||
}
|
||||
String name = clazz.getTypeName();
|
||||
if (mapper != null) {
|
||||
name = mapper.value();
|
||||
}
|
||||
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
|
||||
if (returnType != null && method.getReturnType() != returnType) {
|
||||
return;
|
||||
}
|
||||
commandMethods.add(method);
|
||||
consumer.consume(anno, method, parameters);
|
||||
}
|
||||
|
||||
private void addHelp(Method method) {
|
||||
RegisterHelp registerHelp = getAnnotation(method, RegisterHelp.class);
|
||||
if (registerHelp == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (parameters.length != 1) {
|
||||
return;
|
||||
}
|
||||
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||
return;
|
||||
}
|
||||
commandHelpSet.add(new SubCommand(this, method));
|
||||
}
|
||||
|
||||
private void addMapper(Method method) {
|
||||
Mapper mapper = getAnnotation(method, Mapper.class);
|
||||
if (mapper == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (parameters.length != 0) {
|
||||
return;
|
||||
}
|
||||
if (method.getReturnType() != TypeMapper.class) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
Object object = method.invoke(this);
|
||||
SWCommandUtils.addMapper(mapper.value(), (TypeMapper<?>) object);
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
@FunctionalInterface
|
||||
private interface SWConsumer<T extends Annotation> {
|
||||
void consume(T anno, Method method, Parameter[] parameters);
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren