Optimize SWCommand
Dieser Commit ist enthalten in:
Ursprung
abc48d9215
Commit
7020d6b8f3
@ -22,13 +22,11 @@ package de.steamwar.command;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
import java.lang.annotation.*;
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.function.IntPredicate;
|
||||||
|
|
||||||
import static de.steamwar.command.SWCommandUtils.getAnnotation;
|
import static de.steamwar.command.SWCommandUtils.getAnnotation;
|
||||||
|
|
||||||
@ -79,81 +77,63 @@ public abstract class SWCommand {
|
|||||||
|
|
||||||
Set<Method> commandMethods = new HashSet<>();
|
Set<Method> commandMethods = new HashSet<>();
|
||||||
for (Method method : getClass().getDeclaredMethods()) {
|
for (Method method : getClass().getDeclaredMethods()) {
|
||||||
addMapper(method);
|
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, method1, parameters) -> {
|
||||||
addCommand(method, commandMethods);
|
try {
|
||||||
addHelp(method);
|
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)));
|
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCommand(Method method, Set<Method> commandMethods) {
|
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, SWConsumer<T> consumer) {
|
||||||
Register register = getAnnotation(method, Register.class);
|
T anno = getAnnotation(method, annotation);
|
||||||
if (register == null) {
|
if (anno == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
if (parameters.length == 0) {
|
if (parameterTester.test(parameters.length)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
if (returnType != null && method.getReturnType() != returnType) {
|
||||||
Parameter parameter = parameters[i];
|
return;
|
||||||
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);
|
consumer.consume(anno, method, parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addHelp(Method method) {
|
@FunctionalInterface
|
||||||
RegisterHelp registerHelp = getAnnotation(method, RegisterHelp.class);
|
private interface SWConsumer<T extends Annotation> {
|
||||||
if (registerHelp == null) {
|
void consume(T anno, Method method, Parameter[] parameters);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren