SteamWar/SpigotCore
Archiviert
13
0

Simplify SWCommand

Add SWCommandUtils.addMapper with class
Dieser Commit ist enthalten in:
yoyosource 2021-03-29 22:35:31 +02:00
Ursprung 9c7cea96d4
Commit 529b3daf6c
2 geänderte Dateien mit 22 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -81,32 +81,14 @@ public abstract class SWCommand {
}); });
for (Method method : getClass().getDeclaredMethods()) { for (Method method : getClass().getDeclaredMethods()) {
add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { addMapper(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> {
try { SWCommandUtils.addMapper(anno.value(), typeMapper);
method.setAccessible(true);
Object object = method.invoke(this);
SWCommandUtils.addMapper(anno.value(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
}); });
add(ClassMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { addMapper(ClassMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> {
try { SWCommandUtils.addMapper(anno.value().getTypeName(), typeMapper);
method.setAccessible(true);
Object object = method.invoke(this);
SWCommandUtils.addMapper(anno.value().getTypeName(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
}); });
add(LocalMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { addMapper(LocalMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> {
try { localTypeMapper.put(anno.value(), typeMapper);
method.setAccessible(true);
Object object = method.invoke(this);
localTypeMapper.put(anno.value(), (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
}); });
add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> {
if (!parameters[parameters.length - 1].isVarArgs()) { if (!parameters[parameters.length - 1].isVarArgs()) {
@ -160,6 +142,18 @@ public abstract class SWCommand {
consumer.accept(anno, parameters); consumer.accept(anno, parameters);
} }
private <T extends Annotation> void addMapper(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, TypeMapper<?>> consumer) {
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
try {
method.setAccessible(true);
Object object = method.invoke(this);
consumer.accept(anno, (TypeMapper<?>) object);
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
});
}
protected boolean isEnabled() { protected boolean isEnabled() {
return enabled; return enabled;
} }

Datei anzeigen

@ -116,6 +116,10 @@ public class SWCommandUtils {
return arguments; return arguments;
} }
public static <T> void addMapper(Class<T> clazz, TypeMapper<T> mapper) {
addMapper(clazz.getTypeName(), mapper);
}
public static void addMapper(String name, TypeMapper<?> mapper) { public static void addMapper(String name, TypeMapper<?> mapper) {
if (MAPPER_FUNCTIONS.containsKey(name)) return; if (MAPPER_FUNCTIONS.containsKey(name)) return;
MAPPER_FUNCTIONS.put(name, mapper); MAPPER_FUNCTIONS.put(name, mapper);