SteamWar/SpigotCore
Archiviert
13
0

Optimize SWCommand

Dieser Commit ist enthalten in:
yoyosource 2021-03-26 08:57:37 +01:00
Ursprung abc48d9215
Commit 7020d6b8f3

Datei anzeigen

@ -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,26 +77,16 @@ 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);
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method))); SWCommandUtils.addMapper(anno.value(), (TypeMapper<?>) object);
} } catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
private void addCommand(Method method, Set<Method> commandMethods) {
Register register = getAnnotation(method, Register.class);
if (register == null) {
return;
}
Parameter[] parameters = method.getParameters();
if (parameters.length == 0) {
return;
}
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
return;
} }
});
add(Register.class, method, i -> i == 0, true, null, (anno, method1, 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();
@ -116,44 +104,36 @@ public abstract class SWCommand {
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
} }
commandMethods.add(method); commandMethods.add(method);
} });
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, method1, 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)); commandHelpSet.add(new SubCommand(this, method));
});
}
commandMethods.forEach(method -> commandSet.add(new SubCommand(this, method)));
} }
private void addMapper(Method method) { private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, SWConsumer<T> consumer) {
Mapper mapper = getAnnotation(method, Mapper.class); T anno = getAnnotation(method, annotation);
if (mapper == 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 (method.getReturnType() != TypeMapper.class) { if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
return; return;
} }
try { if (returnType != null && method.getReturnType() != returnType) {
method.setAccessible(true); return;
Object object = method.invoke(this);
SWCommandUtils.addMapper(mapper.value(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
} }
consumer.consume(anno, method, parameters);
}
@FunctionalInterface
private interface SWConsumer<T extends Annotation> {
void consume(T anno, Method method, Parameter[] parameters);
} }
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)