Optimize SWCommand
Dieser Commit ist enthalten in:
Ursprung
7020d6b8f3
Commit
5bd160b0c9
@ -26,6 +26,7 @@ import java.lang.annotation.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.IntPredicate;
|
||||
|
||||
import static de.steamwar.command.SWCommandUtils.getAnnotation;
|
||||
@ -77,7 +78,7 @@ public abstract class SWCommand {
|
||||
|
||||
Set<Method> commandMethods = new HashSet<>();
|
||||
for (Method method : getClass().getDeclaredMethods()) {
|
||||
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, method1, parameters) -> {
|
||||
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
Object object = method.invoke(this);
|
||||
@ -86,7 +87,7 @@ public abstract class SWCommand {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
add(Register.class, method, i -> i == 0, true, null, (anno, method1, parameters) -> {
|
||||
add(Register.class, method, i -> i == 0, true, null, (anno, parameters) -> {
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
Class<?> clazz = parameter.getType();
|
||||
@ -105,14 +106,14 @@ public abstract class SWCommand {
|
||||
}
|
||||
commandMethods.add(method);
|
||||
});
|
||||
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, method1, parameters) -> {
|
||||
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
|
||||
commandHelpSet.add(new SubCommand(this, method));
|
||||
});
|
||||
}
|
||||
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
|
||||
}
|
||||
|
||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, SWConsumer<T> consumer) {
|
||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
|
||||
T anno = getAnnotation(method, annotation);
|
||||
if (anno == null) {
|
||||
return;
|
||||
@ -128,12 +129,7 @@ public abstract class SWCommand {
|
||||
if (returnType != null && method.getReturnType() != returnType) {
|
||||
return;
|
||||
}
|
||||
consumer.consume(anno, method, parameters);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface SWConsumer<T extends Annotation> {
|
||||
void consume(T anno, Method method, Parameter[] parameters);
|
||||
consumer.accept(anno, parameters);
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren