SteamWar/SpigotCore
Archiviert
13
0

Optimize SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-03-26 08:59:12 +01:00
Ursprung 7020d6b8f3
Commit 5bd160b0c9

Datei anzeigen

@ -26,6 +26,7 @@ import java.lang.annotation.*;
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.BiConsumer;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import static de.steamwar.command.SWCommandUtils.getAnnotation; import static de.steamwar.command.SWCommandUtils.getAnnotation;
@ -77,7 +78,7 @@ public abstract class SWCommand {
Set<Method> commandMethods = new HashSet<>(); Set<Method> commandMethods = new HashSet<>();
for (Method method : getClass().getDeclaredMethods()) { 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 { try {
method.setAccessible(true); method.setAccessible(true);
Object object = method.invoke(this); Object object = method.invoke(this);
@ -86,7 +87,7 @@ public abstract class SWCommand {
throw new SecurityException(e.getMessage(), e); 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++) { for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i]; Parameter parameter = parameters[i];
Class<?> clazz = parameter.getType(); Class<?> clazz = parameter.getType();
@ -105,14 +106,14 @@ public abstract class SWCommand {
} }
commandMethods.add(method); 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)); 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 <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); T anno = getAnnotation(method, annotation);
if (anno == null) { if (anno == null) {
return; return;
@ -128,12 +129,7 @@ public abstract class SWCommand {
if (returnType != null && method.getReturnType() != returnType) { if (returnType != null && method.getReturnType() != returnType) {
return; return;
} }
consumer.consume(anno, method, parameters); consumer.accept(anno, parameters);
}
@FunctionalInterface
private interface SWConsumer<T extends Annotation> {
void consume(T anno, Method method, Parameter[] parameters);
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)