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.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,26 +77,16 @@ public abstract class SWCommand {
|
||||
|
||||
Set<Method> commandMethods = new HashSet<>();
|
||||
for (Method method : getClass().getDeclaredMethods()) {
|
||||
addMapper(method);
|
||||
addCommand(method, commandMethods);
|
||||
addHelp(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) {
|
||||
return;
|
||||
}
|
||||
|
||||
Parameter[] parameters = method.getParameters();
|
||||
if (parameters.length == 0) {
|
||||
return;
|
||||
}
|
||||
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||
return;
|
||||
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();
|
||||
@ -116,44 +104,36 @@ public abstract class SWCommand {
|
||||
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return;
|
||||
}
|
||||
commandMethods.add(method);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
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 addMapper(Method method) {
|
||||
Mapper mapper = getAnnotation(method, Mapper.class);
|
||||
if (mapper == 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 (method.getReturnType() != TypeMapper.class) {
|
||||
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren