Merge remote-tracking branch 'origin/nodemember' into nodemember
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed
Dieser Commit ist enthalten in:
Commit
27d6e9accf
0
build.gradle
Normale Datei → Ausführbare Datei
0
build.gradle
Normale Datei → Ausführbare Datei
@ -20,12 +20,13 @@
|
|||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
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.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.IntPredicate;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -35,7 +36,6 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
private boolean initialized = false;
|
private boolean initialized = false;
|
||||||
protected final List<SubCommand<T>> commandList = new ArrayList<>();
|
protected final List<SubCommand<T>> commandList = new ArrayList<>();
|
||||||
protected final List<SubCommand<T>> commandHelpList = new ArrayList<>();
|
|
||||||
|
|
||||||
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
||||||
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
||||||
@ -109,15 +109,8 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
List<Runnable> errors = new ArrayList<>();
|
List<Runnable> errors = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, finalArgs))) {
|
if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, finalArgs))) {
|
||||||
if (!errors.isEmpty()) {
|
errors.forEach(Runnable::run);
|
||||||
errors.forEach(Runnable::run);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {
|
|
||||||
}, sender, alias, finalArgs));
|
|
||||||
}
|
}
|
||||||
} catch (CommandNoHelpException e) {
|
|
||||||
// Ignored
|
|
||||||
} catch (CommandFrameworkException e) {
|
} catch (CommandFrameworkException e) {
|
||||||
commandSystemError(sender, e);
|
commandSystemError(sender, e);
|
||||||
throw e;
|
throw e;
|
||||||
@ -139,74 +132,34 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialize() {
|
private synchronized void initialize() {
|
||||||
if (initialized) return;
|
if (initialized) return;
|
||||||
createMapping();
|
List<Method> methods = methods().stream()
|
||||||
}
|
.filter(this::validateMethod)
|
||||||
|
.collect(Collectors.toList());
|
||||||
private synchronized void createMapping() {
|
|
||||||
List<Method> methods = methods();
|
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
Cached cached = method.getAnnotation(Cached.class);
|
Cached cached = method.getAnnotation(Cached.class);
|
||||||
addMapper(Mapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> {
|
this.<Mapper, AbstractTypeMapper<?, ?>>add(Mapper.class, method, (anno, typeMapper) -> {
|
||||||
TabCompletionCache.add(typeMapper, cached);
|
TabCompletionCache.add(typeMapper, cached);
|
||||||
if (anno.local()) {
|
(anno.local() ? ((Map) localTypeMapper) : SWCommandUtils.getMAPPER_FUNCTIONS()).put(anno.value(), typeMapper);
|
||||||
localTypeMapper.putIfAbsent(anno.value(), (AbstractTypeMapper<T, ?>) typeMapper);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getMAPPER_FUNCTIONS().putIfAbsent(anno.value(), typeMapper);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
addMapper(ClassMapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> {
|
this.<ClassMapper, AbstractTypeMapper<?, ?>>add(ClassMapper.class, method, (anno, typeMapper) -> {
|
||||||
TabCompletionCache.add(typeMapper, cached);
|
TabCompletionCache.add(typeMapper, cached);
|
||||||
if (anno.local()) {
|
(anno.local() ? ((Map) localTypeMapper) : SWCommandUtils.getMAPPER_FUNCTIONS()).put(anno.value().getName(), typeMapper);
|
||||||
localTypeMapper.putIfAbsent(anno.value().getTypeName(), (AbstractTypeMapper<T, ?>) typeMapper);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getMAPPER_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), typeMapper);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
this.<Validator, AbstractValidator<?, ?>>add(Validator.class, method, (anno, validator) -> {
|
||||||
if (anno.local()) {
|
(anno.local() ? ((Map) localValidators) : SWCommandUtils.getVALIDATOR_FUNCTIONS()).put(anno.value(), validator);
|
||||||
localValidators.putIfAbsent(anno.value(), (AbstractValidator<T, ?>) validator);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value(), validator);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
addValidator(ClassValidator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
this.<ClassValidator, AbstractValidator<?, ?>>add(ClassValidator.class, method, (anno, validator) -> {
|
||||||
if (anno.local()) {
|
(anno.local() ? ((Map) localValidators) : SWCommandUtils.getVALIDATOR_FUNCTIONS()).put(anno.value().getName(), validator);
|
||||||
localValidators.putIfAbsent(anno.value().getTypeName(), (AbstractValidator<T, ?>) validator);
|
|
||||||
} else {
|
|
||||||
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), validator);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
add(Register.class, method, true, (anno, parameters) -> {
|
||||||
if (!anno.help()) return;
|
|
||||||
boolean error = false;
|
|
||||||
if (parameters.length != 2) {
|
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking parameters or has too many");
|
|
||||||
error = true;
|
|
||||||
}
|
|
||||||
if (!parameters[parameters.length - 1].isVarArgs()) {
|
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
|
|
||||||
error = true;
|
|
||||||
}
|
|
||||||
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
|
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
|
||||||
error = true;
|
|
||||||
}
|
|
||||||
if (error) return;
|
|
||||||
commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), true, null, anno.noTabComplete()));
|
|
||||||
});
|
|
||||||
|
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
|
||||||
if (anno.help()) return;
|
|
||||||
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();
|
||||||
if (parameter.isVarArgs() && i == parameters.length - 1) {
|
if (parameter.isVarArgs()) clazz = clazz.getComponentType();
|
||||||
clazz = parameter.getType().getComponentType();
|
|
||||||
}
|
|
||||||
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
||||||
if (clazz.isEnum() && mapper == null && !SWCommandUtils.getMAPPER_FUNCTIONS().containsKey(clazz.getTypeName())) {
|
if (clazz.isEnum() && mapper == null && !SWCommandUtils.getMAPPER_FUNCTIONS().containsKey(clazz.getTypeName())) {
|
||||||
continue;
|
continue;
|
||||||
@ -217,66 +170,75 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localValidators, false, anno.description(), anno.noTabComplete()));
|
commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localValidators, anno.description(), anno.noTabComplete()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.commandList.sort((o1, o2) -> {
|
this.commandList.sort((o1, o2) -> {
|
||||||
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
||||||
if (compare != 0) {
|
if (compare == 0) return Integer.compare(o1.comparableValue, o2.comparableValue);
|
||||||
return compare;
|
return compare;
|
||||||
} else {
|
|
||||||
return Integer.compare(o1.comparableValue, o2.comparableValue);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
commandHelpList.sort((o1, o2) -> {
|
|
||||||
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
|
||||||
if (compare != 0) {
|
|
||||||
return compare;
|
|
||||||
} else {
|
|
||||||
return Integer.compare(o1.method.getDeclaringClass() == AbstractSWCommand.class ? 1 : 0,
|
|
||||||
o2.method.getDeclaringClass() == AbstractSWCommand.class ? 1 : 0);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
|
private boolean validateMethod(Method method) {
|
||||||
|
if (!checkType(method.getAnnotations(), method.getReturnType(), annotation -> {
|
||||||
|
CommandMetaData.Method methodMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Method.class);
|
||||||
|
if (methodMetaData == null) return null;
|
||||||
|
if (method.getParameterCount() > methodMetaData.maxParameterCount() || method.getParameterCount() < methodMetaData.minParameterCount()) return new Class[0];
|
||||||
|
return methodMetaData.value();
|
||||||
|
}, "The method '" + method + "'")) return false;
|
||||||
|
boolean valid = true;
|
||||||
|
for (Parameter parameter : method.getParameters()) {
|
||||||
|
Class<?> type = parameter.getType();
|
||||||
|
if (parameter.isVarArgs()) type = type.getComponentType();
|
||||||
|
if (!checkType(parameter.getAnnotations(), type, annotation -> {
|
||||||
|
CommandMetaData.Parameter parameterMetaData = annotation.annotationType().getAnnotation(CommandMetaData.Parameter.class);
|
||||||
|
if (parameterMetaData == null) return null;
|
||||||
|
return parameterMetaData.value();
|
||||||
|
}, "The parameter '" + parameter + "'")) valid = false;
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkType(Annotation[] annotations, Class<?> clazz, Function<Annotation, Class<?>[]> toApplicableTypes, String warning) {
|
||||||
|
boolean valid = true;
|
||||||
|
for (Annotation annotation : annotations) {
|
||||||
|
Class<?>[] types = toApplicableTypes.apply(annotation);
|
||||||
|
if (types == null) continue;
|
||||||
|
boolean applicable = false;
|
||||||
|
for (Class<?> type : types) {
|
||||||
|
if (type.isAssignableFrom(clazz)) {
|
||||||
|
applicable = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!applicable) {
|
||||||
|
commandSystemWarning(() -> warning + " is using an unsupported annotation of type '" + annotation.annotationType().getName() + "'");
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T extends Annotation> void add(Class<T> annotation, Method method, boolean firstParameter, BiConsumer<T, Parameter[]> consumer) {
|
||||||
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
|
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
|
||||||
if (anno == null || anno.length == 0) return;
|
if (anno == null || anno.length == 0) return;
|
||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
if (!parameterTester.test(parameters.length)) {
|
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking parameters or has too many");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (firstParameter && !clazz.isAssignableFrom(parameters[0].getType())) {
|
if (firstParameter && !clazz.isAssignableFrom(parameters[0].getType())) {
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'");
|
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) {
|
|
||||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
|
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Annotation> void addMapper(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractTypeMapper<?, ?>> consumer) {
|
private <T extends Annotation, K> void add(Class<T> annotation, Method method, BiConsumer<T, K> consumer) {
|
||||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
add(annotation, method, false, (anno, parameters) -> {
|
||||||
try {
|
try {
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
consumer.accept(anno, (AbstractTypeMapper<T, ?>) method.invoke(this));
|
consumer.accept(anno, (K) method.invoke(this));
|
||||||
} catch (Exception e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T extends Annotation> void addValidator(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractValidator<T, ?>> consumer) {
|
|
||||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
consumer.accept(anno, (AbstractValidator<T, ?>) method.invoke(this));
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new SecurityException(e.getMessage(), e);
|
throw new SecurityException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -300,12 +262,23 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Annotation for the command ---
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Annotation for registering a method as a command
|
||||||
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
@Repeatable(Register.Registeres.class)
|
@Repeatable(Register.Registeres.class)
|
||||||
|
@CommandMetaData.Method(value = void.class, minParameterCount = 1)
|
||||||
protected @interface Register {
|
protected @interface Register {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifier of subcommand
|
||||||
|
*/
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
boolean help() default false;
|
boolean help() default false;
|
||||||
|
|
||||||
String[] description() default {};
|
String[] description() default {};
|
||||||
@ -314,6 +287,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = void.class, minParameterCount = 1)
|
||||||
@interface Registeres {
|
@interface Registeres {
|
||||||
Register[] value();
|
Register[] value();
|
||||||
}
|
}
|
||||||
@ -321,14 +295,41 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = AbstractTypeMapper.class, maxParameterCount = 0)
|
||||||
|
@CommandMetaData.ImplicitTypeMapper(handler = Mapper.Handler.class)
|
||||||
protected @interface Mapper {
|
protected @interface Mapper {
|
||||||
String value();
|
String value();
|
||||||
|
|
||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractTypeMapper<T, Object> {
|
||||||
|
|
||||||
|
private AbstractTypeMapper<T, Object> inner;
|
||||||
|
|
||||||
|
public Handler(AbstractSWCommand.Mapper mapper, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper) {
|
||||||
|
inner = (AbstractTypeMapper<T, Object>) SWCommandUtils.getTypeMapper(mapper.value(), localTypeMapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object map(T sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return inner.map(sender, previousArguments, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||||
|
return inner.validate(sender, value, messageSender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> tabCompletes(T sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return inner.tabCompletes(sender, previousArguments, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = AbstractTypeMapper.class, maxParameterCount = 0)
|
||||||
protected @interface ClassMapper {
|
protected @interface ClassMapper {
|
||||||
Class<?> value();
|
Class<?> value();
|
||||||
|
|
||||||
@ -337,6 +338,7 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = AbstractTypeMapper.class, maxParameterCount = 0)
|
||||||
protected @interface Cached {
|
protected @interface Cached {
|
||||||
long cacheDuration() default 5;
|
long cacheDuration() default 5;
|
||||||
TimeUnit timeUnit() default TimeUnit.SECONDS;
|
TimeUnit timeUnit() default TimeUnit.SECONDS;
|
||||||
@ -345,22 +347,43 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = AbstractValidator.class, maxParameterCount = 0)
|
||||||
|
@CommandMetaData.ImplicitValidator(handler = Validator.Handler.class, order = 0)
|
||||||
protected @interface Validator {
|
protected @interface Validator {
|
||||||
String value() default "";
|
String value() default "";
|
||||||
|
|
||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractValidator<T, Object> {
|
||||||
|
|
||||||
|
private AbstractValidator<T, Object> inner;
|
||||||
|
|
||||||
|
public Handler(AbstractSWCommand.Validator validator, Class<?> clazz, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||||
|
inner = (AbstractValidator<T, Object>) SWCommandUtils.getValidator(validator, clazz, localValidator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||||
|
return inner.validate(sender, value, messageSender);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.METHOD})
|
@Target({ElementType.METHOD})
|
||||||
|
@CommandMetaData.Method(value = AbstractValidator.class, maxParameterCount = 0)
|
||||||
protected @interface ClassValidator {
|
protected @interface ClassValidator {
|
||||||
Class<?> value();
|
Class<?> value();
|
||||||
|
|
||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Implicit TypeMapper ---
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER})
|
@Target({ElementType.PARAMETER})
|
||||||
|
@CommandMetaData.Parameter({String.class, int.class, Integer.class, long.class, Long.class, boolean.class, Boolean.class})
|
||||||
|
@CommandMetaData.ImplicitTypeMapper(handler = StaticValue.Handler.class)
|
||||||
protected @interface StaticValue {
|
protected @interface StaticValue {
|
||||||
String[] value();
|
String[] value();
|
||||||
|
|
||||||
@ -375,6 +398,53 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean allowISE() default false;
|
boolean allowISE() default false;
|
||||||
|
|
||||||
int[] falseValues() default { 0 };
|
int[] falseValues() default { 0 };
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractTypeMapper<T, Object> {
|
||||||
|
|
||||||
|
private AbstractTypeMapper inner;
|
||||||
|
|
||||||
|
public Handler(StaticValue staticValue, Class<?> clazz) {
|
||||||
|
if (clazz == String.class) {
|
||||||
|
inner = SWCommandUtils.createMapper(staticValue.value());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!staticValue.allowISE()) {
|
||||||
|
throw new IllegalArgumentException("The parameter type '" + clazz.getTypeName() + "' is not supported by the StaticValue annotation");
|
||||||
|
}
|
||||||
|
if (clazz == boolean.class || clazz == Boolean.class) {
|
||||||
|
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
||||||
|
Set<Integer> falseValues = new HashSet<>();
|
||||||
|
for (int i : staticValue.falseValues()) falseValues.add(i);
|
||||||
|
inner = SWCommandUtils.createMapper(s -> {
|
||||||
|
int index = tabCompletes.indexOf(s);
|
||||||
|
return index == -1 ? null : !falseValues.contains(index);
|
||||||
|
}, (commandSender, s) -> tabCompletes);
|
||||||
|
} else if (clazz == int.class || clazz == Integer.class || clazz == long.class || clazz == Long.class) {
|
||||||
|
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
||||||
|
inner = SWCommandUtils.createMapper(s -> {
|
||||||
|
Number index = tabCompletes.indexOf(s);
|
||||||
|
return index.longValue() == -1 ? null : index;
|
||||||
|
}, (commandSender, s) -> tabCompletes);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("The parameter type '" + clazz.getTypeName() + "' is not supported by the StaticValue annotation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object map(T sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return inner.map(sender, previousArguments, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||||
|
return inner.validate(sender, value, messageSender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> tabCompletes(T sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return inner.tabCompletes(sender, previousArguments, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@ -391,8 +461,11 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
boolean onlyUINIG() default false;
|
boolean onlyUINIG() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Implicit Validator ---
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER})
|
@Target({ElementType.PARAMETER})
|
||||||
|
@CommandMetaData.ImplicitValidator(handler = ErrorMessage.Handler.class, order = 1)
|
||||||
protected @interface ErrorMessage {
|
protected @interface ErrorMessage {
|
||||||
/**
|
/**
|
||||||
* Error message to be displayed when the parameter is invalid.
|
* Error message to be displayed when the parameter is invalid.
|
||||||
@ -403,6 +476,25 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
* This is the short form for 'allowEmptyArrays'.
|
* This is the short form for 'allowEmptyArrays'.
|
||||||
*/
|
*/
|
||||||
boolean allowEAs() default true;
|
boolean allowEAs() default true;
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractValidator<T, Object> {
|
||||||
|
|
||||||
|
private AbstractSWCommand.ErrorMessage errorMessage;
|
||||||
|
|
||||||
|
public Handler(AbstractSWCommand.ErrorMessage errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||||
|
if (value == null) messageSender.send(errorMessage.value());
|
||||||
|
if (!errorMessage.allowEAs() && value != null && value.getClass().isArray() && Array.getLength(value) == 0) {
|
||||||
|
messageSender.send(errorMessage.value());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return value != null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@ -415,6 +507,79 @@ public abstract class AbstractSWCommand<T> {
|
|||||||
*/
|
*/
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.PARAMETER})
|
@Target({ElementType.PARAMETER})
|
||||||
|
@CommandMetaData.Parameter({String.class})
|
||||||
protected @interface Quotable {
|
protected @interface Quotable {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
@CommandMetaData.Parameter({int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class})
|
||||||
|
@CommandMetaData.ImplicitValidator(handler = Min.Handler.class, order = 2)
|
||||||
|
protected @interface Min {
|
||||||
|
int intValue() default Integer.MIN_VALUE;
|
||||||
|
long longValue() default Long.MIN_VALUE;
|
||||||
|
float floatValue() default Float.MIN_VALUE;
|
||||||
|
double doubleValue() default Double.MIN_VALUE;
|
||||||
|
|
||||||
|
boolean inclusive() default true;
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractValidator<T, Number> {
|
||||||
|
|
||||||
|
private int value;
|
||||||
|
private Function<Number, Number> comparator;
|
||||||
|
|
||||||
|
public Handler(AbstractSWCommand.Min min, Class<?> clazz) {
|
||||||
|
this.value = min.inclusive() ? 0 : 1;
|
||||||
|
this.comparator = createComparator("Min", clazz, min.intValue(), min.longValue(), min.floatValue(), min.doubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Number value, MessageSender messageSender) {
|
||||||
|
return (comparator.apply(value).intValue()) >= this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER})
|
||||||
|
@CommandMetaData.Parameter({int.class, Integer.class, long.class, Long.class, float.class, Float.class, double.class, Double.class})
|
||||||
|
@CommandMetaData.ImplicitValidator(handler = Max.Handler.class, order = 2)
|
||||||
|
protected @interface Max {
|
||||||
|
int intValue() default Integer.MAX_VALUE;
|
||||||
|
long longValue() default Long.MAX_VALUE;
|
||||||
|
float floatValue() default Float.MAX_VALUE;
|
||||||
|
double doubleValue() default Double.MAX_VALUE;
|
||||||
|
|
||||||
|
boolean inclusive() default true;
|
||||||
|
|
||||||
|
class Handler<T> implements AbstractValidator<T, Number> {
|
||||||
|
|
||||||
|
private int value;
|
||||||
|
private Function<Number, Number> comparator;
|
||||||
|
|
||||||
|
public Handler(AbstractSWCommand.Max max, Class<?> clazz) {
|
||||||
|
this.value = max.inclusive() ? 0 : -1;
|
||||||
|
this.comparator = createComparator("Max", clazz, max.intValue(), max.longValue(), max.floatValue(), max.doubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(T sender, Number value, MessageSender messageSender) {
|
||||||
|
return (comparator.apply(value).intValue()) <= this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Function<Number, Number> createComparator(String type, Class<?> clazz, int iValue, long lValue, float fValue, double dValue) {
|
||||||
|
if (clazz == int.class || clazz == Integer.class) {
|
||||||
|
return number -> Integer.compare(number.intValue(), iValue);
|
||||||
|
} else if (clazz == long.class || clazz == Long.class) {
|
||||||
|
return number -> Long.compare(number.longValue(), lValue);
|
||||||
|
} else if (clazz == float.class || clazz == Float.class) {
|
||||||
|
return number -> Float.compare(number.floatValue(), fValue);
|
||||||
|
} else if (clazz == double.class || clazz == Double.class) {
|
||||||
|
return number -> Double.compare(number.doubleValue(), dValue);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException(type + " annotation is not supported for " + clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,12 +25,29 @@ public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
|
|||||||
/**
|
/**
|
||||||
* The CommandSender can be null!
|
* The CommandSender can be null!
|
||||||
*/
|
*/
|
||||||
T map(K sender, String[] previousArguments, String s);
|
@Deprecated
|
||||||
|
default T map(K sender, String[] previousArguments, String s) {
|
||||||
|
throw new IllegalArgumentException("This method is deprecated and should not be used anymore!");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The CommandSender can be null!
|
||||||
|
*/
|
||||||
|
default T map(K sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return map(sender, previousArguments.userArgs, s);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default boolean validate(K sender, T value, MessageSender messageSender) {
|
default boolean validate(K sender, T value, MessageSender messageSender) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<String> tabCompletes(K sender, String[] previousArguments, String s);
|
@Deprecated
|
||||||
|
default Collection<String> tabCompletes(K sender, String[] previousArguments, String s) {
|
||||||
|
throw new IllegalArgumentException("This method is deprecated and should not be used anymore!");
|
||||||
|
}
|
||||||
|
|
||||||
|
default Collection<String> tabCompletes(K sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return tabCompletes(sender, previousArguments.userArgs, s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,12 @@ public interface AbstractValidator<K, T> {
|
|||||||
*/
|
*/
|
||||||
boolean validate(K sender, T value, MessageSender messageSender);
|
boolean validate(K sender, T value, MessageSender messageSender);
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
default <C> Validator<C> validate(C value, MessageSender messageSender) {
|
default <C> Validator<C> validate(C value, MessageSender messageSender) {
|
||||||
return new Validator<>(value, messageSender);
|
return new Validator<>(value, messageSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
class Validator<C> {
|
class Validator<C> {
|
||||||
private final C value;
|
private final C value;
|
||||||
|
@ -36,12 +36,6 @@ public class CommandFrameworkException extends RuntimeException {
|
|||||||
|
|
||||||
private String message;
|
private String message;
|
||||||
|
|
||||||
static CommandFrameworkException commandGetExceptions(String type, Class<?> clazzType, Executable executable, int index) {
|
|
||||||
return new CommandFrameworkException(throwable -> {
|
|
||||||
return CommandFrameworkException.class.getTypeName() + ": Error while getting " + type + " for " + clazzType.getTypeName() + " with parameter index " + index;
|
|
||||||
}, null, throwable -> Stream.empty(), executable.getDeclaringClass().getTypeName() + "." + executable.getName() + "(Unknown Source)");
|
|
||||||
}
|
|
||||||
|
|
||||||
static CommandFrameworkException commandPartExceptions(String type, Throwable cause, String current, Class<?> clazzType, Executable executable, int index) {
|
static CommandFrameworkException commandPartExceptions(String type, Throwable cause, String current, Class<?> clazzType, Executable executable, int index) {
|
||||||
return new CommandFrameworkException(e -> {
|
return new CommandFrameworkException(e -> {
|
||||||
return CommandFrameworkException.class.getTypeName() + ": Error while " + type + " (" + current + ") to type " + clazzType.getTypeName() + " with parameter index " + index;
|
return CommandFrameworkException.class.getTypeName() + ": Error while " + type + " (" + current + ") to type " + clazzType.getTypeName() + " with parameter index " + index;
|
||||||
|
94
src/de/steamwar/command/CommandMetaData.java
Normale Datei
94
src/de/steamwar/command/CommandMetaData.java
Normale Datei
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
public @interface CommandMetaData {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This annotation is only for internal use.
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
|
@interface Method {
|
||||||
|
Class<?>[] value();
|
||||||
|
int minParameterCount() default 0;
|
||||||
|
int maxParameterCount() default Integer.MAX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This annotation denotes what types are allowed as parameter types the annotation annotated with can use.
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
|
@interface Parameter {
|
||||||
|
Class<?>[] value();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This annotation can be used in conjunction with a class that implements {@link AbstractTypeMapper} to
|
||||||
|
* create a custom type mapper for a parameter. The class must have one of two constructors with the following
|
||||||
|
* types:
|
||||||
|
* <ul>
|
||||||
|
* <li>Annotation this annotation annotates</li>
|
||||||
|
* <li>{@link Class}</li>
|
||||||
|
* <li>{@link AbstractTypeMapper}, optional, if not present only one per parameter</li>
|
||||||
|
* <li>{@link java.util.Map} with types {@link String} and {@link AbstractValidator}</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
|
@interface ImplicitTypeMapper {
|
||||||
|
/**
|
||||||
|
* The validator class that should be used.
|
||||||
|
*/
|
||||||
|
Class<?> handler();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This annotation can be used in conjunction with a class that implements {@link AbstractValidator} to
|
||||||
|
* create a custom validator short hands for commands. The validator class must have one constructor with
|
||||||
|
* one of the following types:
|
||||||
|
* <ul>
|
||||||
|
* <li>Annotation this annotation annotates</li>
|
||||||
|
* <li>{@link Class}</li>
|
||||||
|
* <li>{@link java.util.Map} with types {@link String} and {@link AbstractValidator}</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.ANNOTATION_TYPE)
|
||||||
|
@interface ImplicitValidator {
|
||||||
|
/**
|
||||||
|
* The validator class that should be used.
|
||||||
|
*/
|
||||||
|
Class<?> handler();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines when this validator should be processed. Negative numbers denote that this will be
|
||||||
|
* processed before {@link AbstractSWCommand.Validator} and positive numbers
|
||||||
|
* denote that this will be processed after {@link AbstractSWCommand.Validator}.
|
||||||
|
*/
|
||||||
|
int order();
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,7 @@ import lombok.Setter;
|
|||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -31,7 +32,8 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
class CommandPart<T> {
|
class CommandPart<T> {
|
||||||
|
|
||||||
private static final String[] EMPTY_ARRAY = new String[0];
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||||
|
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
private static class CheckArgumentResult {
|
private static class CheckArgumentResult {
|
||||||
@ -41,7 +43,7 @@ class CommandPart<T> {
|
|||||||
|
|
||||||
private AbstractSWCommand<T> command;
|
private AbstractSWCommand<T> command;
|
||||||
private AbstractTypeMapper<T, ?> typeMapper;
|
private AbstractTypeMapper<T, ?> typeMapper;
|
||||||
private AbstractValidator<T, Object> validator;
|
private List<AbstractValidator<T, Object>> validators = new ArrayList<>();
|
||||||
private Class<?> varArgType;
|
private Class<?> varArgType;
|
||||||
private String optional;
|
private String optional;
|
||||||
|
|
||||||
@ -53,25 +55,25 @@ class CommandPart<T> {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean onlyUseIfNoneIsGiven = false;
|
private boolean onlyUseIfNoneIsGiven = false;
|
||||||
|
|
||||||
@Setter
|
|
||||||
private boolean allowNullValues = false;
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
private boolean quotable = false;
|
|
||||||
|
|
||||||
private Parameter parameter;
|
private Parameter parameter;
|
||||||
private int parameterIndex;
|
private int parameterIndex;
|
||||||
|
|
||||||
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, Class<?> varArgType, String optional, Parameter parameter, int parameterIndex) {
|
public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, Class<?> varArgType, String optional, Parameter parameter, int parameterIndex) {
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.typeMapper = typeMapper;
|
this.typeMapper = typeMapper;
|
||||||
this.validator = validator;
|
|
||||||
this.varArgType = varArgType;
|
this.varArgType = varArgType;
|
||||||
this.optional = optional;
|
this.optional = optional;
|
||||||
this.parameter = parameter;
|
this.parameter = parameter;
|
||||||
this.parameterIndex = parameterIndex;
|
this.parameterIndex = parameterIndex;
|
||||||
|
|
||||||
validatePart();
|
if (optional != null && varArgType != null) {
|
||||||
|
throw new IllegalArgumentException("A vararg part can't have an optional part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void addValidator(AbstractValidator<T, Object> validator) {
|
||||||
|
if (validator == null) return;
|
||||||
|
validators.add(validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNext(CommandPart<T> next) {
|
public void setNext(CommandPart<T> next) {
|
||||||
@ -81,41 +83,33 @@ class CommandPart<T> {
|
|||||||
this.next = next;
|
this.next = next;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validatePart() {
|
|
||||||
if (optional != null && varArgType != null) {
|
|
||||||
throw new IllegalArgumentException("A vararg part can't have an optional part! In method " + parameter.getDeclaringExecutable() + " with parameter " + parameterIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
|
public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
for (int i = startIndex; i < args.length; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
|
CheckArgumentResult validArgument = checkArgument(null, sender, args, current, i);
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) throw new CommandParseException();
|
||||||
throw new CommandParseException();
|
|
||||||
}
|
|
||||||
Array.set(array, i - startIndex, validArgument.value);
|
Array.set(array, i - startIndex, validArgument.value);
|
||||||
}
|
}
|
||||||
if (validator != null && !validator.validate(sender, array, (s, objects) -> {
|
for (AbstractValidator<T, Object> validator : validators) {
|
||||||
errors.accept(() -> command.sendMessage(sender, s, objects));
|
if (!validator.validate(sender, array, (s, objects) -> {
|
||||||
})) {
|
errors.accept(() -> command.sendMessage(sender, s, objects));
|
||||||
throw new CommandParseException();
|
})) throw new CommandParseException();
|
||||||
}
|
}
|
||||||
current.add(array);
|
current.add(array);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckArgumentResult validArgument = checkArgument(errors, sender, args, startIndex);
|
CheckArgumentResult validArgument = checkArgument(errors, sender, args, current, startIndex);
|
||||||
if (!validArgument.success && optional == null) {
|
if (!validArgument.success && optional == null) {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) {
|
||||||
if (!ignoreAsArgument) {
|
if (!ignoreAsArgument) {
|
||||||
if (!onlyUseIfNoneIsGiven) {
|
if (!onlyUseIfNoneIsGiven) {
|
||||||
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
|
current.add(typeMapper.map(sender, new PreviousArguments(EMPTY_STRING_ARRAY, EMPTY_OBJECT_ARRAY), optional));
|
||||||
} else if (startIndex >= args.length) {
|
} else if (startIndex >= args.length) {
|
||||||
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
|
current.add(typeMapper.map(sender, new PreviousArguments(EMPTY_STRING_ARRAY, EMPTY_OBJECT_ARRAY), optional));
|
||||||
} else {
|
} else {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
@ -133,15 +127,13 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
public void generateTabComplete(List<String> current, T sender, String[] args, List<Object> mappedArgs, int startIndex) {
|
||||||
if (varArgType != null) {
|
if (varArgType != null) {
|
||||||
for (int i = startIndex; i < args.length - 1; i++) {
|
for (int i = startIndex; i < args.length - 1; i++) {
|
||||||
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, i);
|
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, sender, args, mappedArgs, i);
|
||||||
if (!validArgument.success) {
|
if (!validArgument.success) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Collection<String> strings = tabCompletes(sender, args, args.length - 1);
|
Collection<String> strings = tabCompletes(sender, args, mappedArgs, args.length - 1);
|
||||||
if (strings != null) {
|
if (strings != null) {
|
||||||
current.addAll(strings);
|
current.addAll(strings);
|
||||||
}
|
}
|
||||||
@ -149,47 +141,47 @@ class CommandPart<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (args.length - 1 > startIndex) {
|
if (args.length - 1 > startIndex) {
|
||||||
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, sender, args, startIndex);
|
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, sender, args, mappedArgs, startIndex);
|
||||||
if (checkArgumentResult.success && next != null) {
|
if (checkArgumentResult.success && next != null) {
|
||||||
next.generateTabComplete(current, sender, args, startIndex + 1);
|
if (!ignoreAsArgument) {
|
||||||
|
mappedArgs.add(checkArgumentResult.value);
|
||||||
|
}
|
||||||
|
next.generateTabComplete(current, sender, args, mappedArgs, startIndex + 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (optional != null && next != null) {
|
if (optional != null && next != null) {
|
||||||
next.generateTabComplete(current, sender, args, startIndex);
|
next.generateTabComplete(current, sender, args, mappedArgs, startIndex);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<String> strings = tabCompletes(sender, args, startIndex);
|
Collection<String> strings = tabCompletes(sender, args, mappedArgs, startIndex);
|
||||||
if (strings != null) {
|
if (strings != null) {
|
||||||
current.addAll(strings);
|
current.addAll(strings);
|
||||||
}
|
}
|
||||||
if (optional != null && next != null) {
|
if (optional != null && next != null) {
|
||||||
next.generateTabComplete(current, sender, args, startIndex);
|
next.generateTabComplete(current, sender, args, mappedArgs, startIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<String> tabCompletes(T sender, String[] args, int startIndex) {
|
private Collection<String> tabCompletes(T sender, String[] args, List<Object> mappedArgs, int startIndex) {
|
||||||
return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> {
|
return TabCompletionCache.tabComplete(sender, typeMapper, command, () -> {
|
||||||
try {
|
try {
|
||||||
return typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]);
|
return typeMapper.tabCompletes(sender, new PreviousArguments(Arrays.copyOf(args, startIndex), mappedArgs.toArray()), args[startIndex]);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
throw CommandFrameworkException.commandPartExceptions("tabcompleting", e, args[startIndex], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
|
throw CommandFrameworkException.commandPartExceptions("tabcompleting", e, args[startIndex], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, T sender, String[] args, int index) {
|
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, T sender, String[] args, List<Object> mappedArgs, int index) {
|
||||||
Object value;
|
Object value;
|
||||||
try {
|
try {
|
||||||
value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
|
value = typeMapper.map(sender, new PreviousArguments(Arrays.copyOf(args, index), mappedArgs.toArray()), args[index]);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return new CheckArgumentResult(false, null);
|
return new CheckArgumentResult(false, null);
|
||||||
}
|
}
|
||||||
if (value instanceof String && !quotable && ((String) value).contains(" ")) {
|
for (AbstractValidator<T, Object> validator : validators) {
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
if (validator != null && errors != null) {
|
|
||||||
try {
|
try {
|
||||||
if (!validator.validate(sender, value, (s, objects) -> {
|
if (!validator.validate(sender, value, (s, objects) -> {
|
||||||
errors.accept(() -> {
|
errors.accept(() -> {
|
||||||
@ -202,6 +194,10 @@ class CommandPart<T> {
|
|||||||
throw CommandFrameworkException.commandPartExceptions("validating", e, args[index], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
|
throw CommandFrameworkException.commandPartExceptions("validating", e, args[index], (varArgType != null ? varArgType : parameter.getType()), parameter.getDeclaringExecutable(), parameterIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new CheckArgumentResult(allowNullValues || value != null, value);
|
return new CheckArgumentResult(true, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getType() {
|
||||||
|
return varArgType != null ? varArgType : parameter.getType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
62
src/de/steamwar/command/PreviousArguments.java
Normale Datei
62
src/de/steamwar/command/PreviousArguments.java
Normale Datei
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class PreviousArguments {
|
||||||
|
|
||||||
|
public final String[] userArgs;
|
||||||
|
public final Object[] mappedArgs;
|
||||||
|
|
||||||
|
public PreviousArguments(String[] userArgs, Object[] mappedArgs) {
|
||||||
|
this.userArgs = userArgs;
|
||||||
|
this.mappedArgs = mappedArgs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserArg(int index) {
|
||||||
|
return userArgs[userArgs.length - index - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getMappedArg(int index) {
|
||||||
|
return (T) mappedArgs[mappedArgs.length - index - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Optional<T> getFirst(Class<T> clazz) {
|
||||||
|
for (Object o : mappedArgs) {
|
||||||
|
if (clazz.isInstance(o)) {
|
||||||
|
return Optional.of((T) o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> List<T> getAll(Class<T> clazz) {
|
||||||
|
List<T> list = new ArrayList<>();
|
||||||
|
for (Object o : mappedArgs) {
|
||||||
|
if (clazz.isInstance(o)) {
|
||||||
|
list.add((T) o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,6 @@ import lombok.Getter;
|
|||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.Array;
|
|
||||||
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.*;
|
||||||
@ -42,12 +41,12 @@ public class SWCommandUtils {
|
|||||||
|
|
||||||
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
|
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
|
||||||
@Override
|
@Override
|
||||||
public Object map(Object sender, String[] previousArguments, String s) {
|
public Object map(Object sender, PreviousArguments previousArguments, String s) {
|
||||||
return mapper.apply(s);
|
return mapper.apply(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> tabCompletes(Object sender, String[] previousArguments, String s) {
|
public Collection<String> tabCompletes(Object sender, PreviousArguments previousArguments, String s) {
|
||||||
return ((BiFunction<Object, Object, Collection<String>>) tabCompleter).apply(sender, s);
|
return ((BiFunction<Object, Object, Collection<String>>) tabCompleter).apply(sender, s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -82,87 +81,7 @@ public class SWCommandUtils {
|
|||||||
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
|
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
static <T> CommandPart<T> generateCommandPart(AbstractSWCommand<T> command, boolean help, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator) {
|
public static <T> AbstractTypeMapper<T, ?> getTypeMapper(String name, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper) {
|
||||||
CommandPart<T> first = null;
|
|
||||||
CommandPart<T> current = null;
|
|
||||||
for (String s : subCommand) {
|
|
||||||
CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null, null, -1);
|
|
||||||
commandPart.setIgnoreAsArgument(true);
|
|
||||||
if (current != null) {
|
|
||||||
current.setNext(commandPart);
|
|
||||||
}
|
|
||||||
current = commandPart;
|
|
||||||
if (first == null) {
|
|
||||||
first = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
|
||||||
Parameter parameter = parameters[i];
|
|
||||||
AbstractTypeMapper<T, ?> typeMapper = getTypeMapper(parameter, localTypeMapper);
|
|
||||||
AbstractValidator<T, Object> validator = (AbstractValidator<T, Object>) getValidator(parameter, localValidator);
|
|
||||||
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
|
||||||
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
|
|
||||||
AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class);
|
|
||||||
AbstractSWCommand.Quotable quotable = parameter.getAnnotation(AbstractSWCommand.Quotable.class);
|
|
||||||
|
|
||||||
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i);
|
|
||||||
commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG());
|
|
||||||
commandPart.setAllowNullValues(allowNull != null);
|
|
||||||
commandPart.setQuotable(quotable != null);
|
|
||||||
if (current != null) {
|
|
||||||
current.setNext(commandPart);
|
|
||||||
}
|
|
||||||
current = commandPart;
|
|
||||||
if (first == null) {
|
|
||||||
first = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return first;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> AbstractTypeMapper<T, ?> getTypeMapper(Parameter parameter, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper) {
|
|
||||||
Class<?> clazz = parameter.getType();
|
|
||||||
if (parameter.isVarArgs()) {
|
|
||||||
clazz = clazz.getComponentType();
|
|
||||||
}
|
|
||||||
|
|
||||||
AbstractSWCommand.ClassMapper classMapper = parameter.getAnnotation(AbstractSWCommand.ClassMapper.class);
|
|
||||||
AbstractSWCommand.Mapper mapper = parameter.getAnnotation(AbstractSWCommand.Mapper.class);
|
|
||||||
if (clazz.isEnum() && classMapper == null && mapper == null && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
|
|
||||||
return createEnumMapper((Class<Enum<?>>) clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = clazz.getTypeName();
|
|
||||||
if (classMapper != null) {
|
|
||||||
name = classMapper.value().getTypeName();
|
|
||||||
} else if (mapper != null) {
|
|
||||||
name = mapper.value();
|
|
||||||
} else {
|
|
||||||
AbstractSWCommand.StaticValue staticValue = parameter.getAnnotation(AbstractSWCommand.StaticValue.class);
|
|
||||||
if (staticValue != null) {
|
|
||||||
if (parameter.getType() == String.class) {
|
|
||||||
return createMapper(staticValue.value());
|
|
||||||
}
|
|
||||||
if (staticValue.allowISE()) {
|
|
||||||
if ((parameter.getType() == boolean.class || parameter.getType() == Boolean.class)) {
|
|
||||||
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
|
||||||
Set<Integer> falseValues = new HashSet<>();
|
|
||||||
for (int i : staticValue.falseValues()) falseValues.add(i);
|
|
||||||
return createMapper(s -> {
|
|
||||||
int index = tabCompletes.indexOf(s);
|
|
||||||
return index == -1 ? null : !falseValues.contains(index);
|
|
||||||
}, (commandSender, s) -> tabCompletes);
|
|
||||||
}
|
|
||||||
if ((parameter.getType() == int.class || parameter.getType() == Integer.class || parameter.getType() == long.class || parameter.getType() == Long.class) && staticValue.value().length >= 2) {
|
|
||||||
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
|
||||||
return createMapper(s -> {
|
|
||||||
Number index = tabCompletes.indexOf(s);
|
|
||||||
return index.longValue() == -1 ? null : index;
|
|
||||||
}, (commandSender, s) -> tabCompletes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AbstractTypeMapper<T, ?> typeMapper = localTypeMapper.getOrDefault(name, (AbstractTypeMapper<T, ?>) MAPPER_FUNCTIONS.getOrDefault(name, null));
|
AbstractTypeMapper<T, ?> typeMapper = localTypeMapper.getOrDefault(name, (AbstractTypeMapper<T, ?>) MAPPER_FUNCTIONS.getOrDefault(name, null));
|
||||||
if (typeMapper == null) {
|
if (typeMapper == null) {
|
||||||
throw new IllegalArgumentException("No mapper found for " + name);
|
throw new IllegalArgumentException("No mapper found for " + name);
|
||||||
@ -170,45 +89,24 @@ public class SWCommandUtils {
|
|||||||
return typeMapper;
|
return typeMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> AbstractValidator<T, ?> getValidator(Parameter parameter, Map<String, AbstractValidator<T, ?>> localValidator) {
|
public static <T> AbstractTypeMapper<T, ?> getTypeMapper(Parameter parameter, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper) {
|
||||||
Class<?> clazz = parameter.getType();
|
Class<?> clazz = parameter.getType();
|
||||||
|
if (parameter.isVarArgs()) {
|
||||||
AbstractSWCommand.ClassValidator classValidator = parameter.getAnnotation(AbstractSWCommand.ClassValidator.class);
|
clazz = clazz.getComponentType();
|
||||||
if (classValidator != null) {
|
|
||||||
if (classValidator.value() != null) {
|
|
||||||
return getValidator(classValidator.value().getTypeName(), localValidator);
|
|
||||||
}
|
|
||||||
return getValidator(clazz.getTypeName(), localValidator);
|
|
||||||
}
|
}
|
||||||
|
if (clazz.isEnum() && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
|
||||||
AbstractSWCommand.Validator validator = parameter.getAnnotation(AbstractSWCommand.Validator.class);
|
return createEnumMapper((Class<Enum<?>>) clazz);
|
||||||
if (validator != null) {
|
|
||||||
if (validator.value() != null && !validator.value().isEmpty()) {
|
|
||||||
return getValidator(validator.value(), localValidator);
|
|
||||||
}
|
|
||||||
return getValidator(clazz.getTypeName(), localValidator);
|
|
||||||
}
|
}
|
||||||
|
return getTypeMapper(clazz.getTypeName(), localTypeMapper);
|
||||||
AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class);
|
|
||||||
if (errorMessage != null) {
|
|
||||||
return (AbstractValidator<T, Object>) (sender, value, messageSender) -> {
|
|
||||||
if (value == null) messageSender.send(errorMessage.value());
|
|
||||||
if (!errorMessage.allowEAs() && value != null && value.getClass().isArray() && Array.getLength(value) == 0) {
|
|
||||||
messageSender.send(errorMessage.value());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return value != null;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static <T> AbstractValidator<T, ?> getValidator(String s, Map<String, AbstractValidator<T, ?>> localGuardChecker) {
|
public static <T> AbstractValidator<T, ?> getValidator(AbstractSWCommand.Validator validator, Class<?> type, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||||
AbstractValidator<T, ?> validator = localGuardChecker.getOrDefault(s, (AbstractValidator<T, ?>) VALIDATOR_FUNCTIONS.getOrDefault(s, null));
|
String s = validator.value() != null && !validator.value().isEmpty() ? validator.value() : type.getTypeName();
|
||||||
if (validator == null) {
|
AbstractValidator<T, ?> concreteValidator = localValidator.getOrDefault(s, (AbstractValidator<T, ?>) VALIDATOR_FUNCTIONS.getOrDefault(s, null));
|
||||||
|
if (concreteValidator == null) {
|
||||||
throw new IllegalArgumentException("No validator found for " + s);
|
throw new IllegalArgumentException("No validator found for " + s);
|
||||||
}
|
}
|
||||||
return validator;
|
return concreteValidator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <K, T> void addMapper(Class<T> clazz, AbstractTypeMapper<K, T> mapper) {
|
public static <K, T> void addMapper(Class<T> clazz, AbstractTypeMapper<K, T> mapper) {
|
||||||
|
@ -19,10 +19,13 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -43,7 +46,7 @@ public class SubCommand<T> {
|
|||||||
|
|
||||||
private CommandPart<T> commandPart;
|
private CommandPart<T> commandPart;
|
||||||
|
|
||||||
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, boolean help, String[] description, boolean noTabComplete) {
|
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, String[] description, boolean noTabComplete) {
|
||||||
this.abstractSWCommand = abstractSWCommand;
|
this.abstractSWCommand = abstractSWCommand;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
try {
|
try {
|
||||||
@ -58,9 +61,12 @@ public class SubCommand<T> {
|
|||||||
Parameter[] parameters = method.getParameters();
|
Parameter[] parameters = method.getParameters();
|
||||||
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
|
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
|
||||||
|
|
||||||
validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(parameters[0], localValidator);
|
AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class);
|
||||||
|
if (validator != null) {
|
||||||
|
this.validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
|
||||||
|
}
|
||||||
|
|
||||||
commandPart = SWCommandUtils.generateCommandPart(abstractSWCommand, help, subCommand, parameters, localTypeMapper, localValidator);
|
commandPart = generateCommandPart(abstractSWCommand, subCommand, parameters, localTypeMapper, localValidator);
|
||||||
|
|
||||||
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
|
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
|
||||||
senderFunction = t -> parameters[0].getType().cast(t);
|
senderFunction = t -> parameters[0].getType().cast(t);
|
||||||
@ -97,7 +103,7 @@ public class SubCommand<T> {
|
|||||||
objects.add(0, senderFunction.apply(sender));
|
objects.add(0, senderFunction.apply(sender));
|
||||||
method.invoke(abstractSWCommand, objects.toArray());
|
method.invoke(abstractSWCommand, objects.toArray());
|
||||||
}
|
}
|
||||||
} catch (CommandNoHelpException | CommandFrameworkException e) {
|
} catch (CommandFrameworkException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (CommandParseException e) {
|
} catch (CommandParseException e) {
|
||||||
return false;
|
return false;
|
||||||
@ -110,18 +116,163 @@ public class SubCommand<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> tabComplete(T sender, String[] args) {
|
List<String> tabComplete(T sender, String[] args) {
|
||||||
if (validator != null) {
|
if (validator != null && !validator.validate(sender, sender, (s, objects) -> {})) {
|
||||||
if (!validator.validate(sender, sender, (s, objects) -> {
|
return null;
|
||||||
// ignore
|
|
||||||
})) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (commandPart == null) {
|
if (commandPart == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
commandPart.generateTabComplete(list, sender, args, 0);
|
commandPart.generateTabComplete(list, sender, args, new ArrayList<>(), 0);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static <T> CommandPart<T> generateCommandPart(AbstractSWCommand<T> command, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||||
|
CommandPart<T> first = null;
|
||||||
|
CommandPart<T> current = null;
|
||||||
|
for (String s : subCommand) {
|
||||||
|
CommandPart commandPart = new CommandPart(command, SWCommandUtils.createMapper(s), null, null, null, -1);
|
||||||
|
commandPart.addValidator(NULL_FILTER);
|
||||||
|
commandPart.setIgnoreAsArgument(true);
|
||||||
|
if (current != null) {
|
||||||
|
current.setNext(commandPart);
|
||||||
|
}
|
||||||
|
current = commandPart;
|
||||||
|
if (first == null) {
|
||||||
|
first = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 1; i < parameters.length; i++) {
|
||||||
|
Parameter parameter = parameters[i];
|
||||||
|
AbstractTypeMapper<T, ?> typeMapper = handleImplicitTypeMapper(parameter, localTypeMapper);
|
||||||
|
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
||||||
|
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
|
||||||
|
|
||||||
|
CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, varArgType, optionalValue != null ? optionalValue.value() : null, parameter, i);
|
||||||
|
commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG());
|
||||||
|
if (parameter.getAnnotation(AbstractSWCommand.Quotable.class) == null) {
|
||||||
|
commandPart.addValidator((AbstractValidator<T, Object>) STRING_SPACE_FILTER);
|
||||||
|
}
|
||||||
|
handleImplicitTypeValidator(parameter, commandPart, localValidator);
|
||||||
|
if (parameter.getAnnotation(AbstractSWCommand.AllowNull.class) == null) {
|
||||||
|
commandPart.addValidator((AbstractValidator<T, Object>) NULL_FILTER);
|
||||||
|
}
|
||||||
|
if (current != null) {
|
||||||
|
current.setNext(commandPart);
|
||||||
|
}
|
||||||
|
current = commandPart;
|
||||||
|
if (first == null) {
|
||||||
|
first = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> AbstractTypeMapper<T, Object> handleImplicitTypeMapper(Parameter parameter, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper) {
|
||||||
|
Class<?> type = parameter.getType();
|
||||||
|
if (parameter.isVarArgs()) {
|
||||||
|
type = type.getComponentType();
|
||||||
|
}
|
||||||
|
|
||||||
|
Annotation[] annotations = parameter.getAnnotations();
|
||||||
|
Constructor<?> sourceConstructor = null;
|
||||||
|
Annotation sourceAnnotation = null;
|
||||||
|
List<Constructor<?>> parentConstructors = new ArrayList<>();
|
||||||
|
List<Annotation> parentAnnotations = new ArrayList<>();
|
||||||
|
for (Annotation annotation : annotations) {
|
||||||
|
CommandMetaData.ImplicitTypeMapper implicitTypeMapper = annotation.annotationType().getAnnotation(CommandMetaData.ImplicitTypeMapper.class);
|
||||||
|
if (implicitTypeMapper == null) continue;
|
||||||
|
Class<?> clazz = implicitTypeMapper.handler();
|
||||||
|
if (!AbstractTypeMapper.class.isAssignableFrom(clazz)) continue;
|
||||||
|
Constructor<?>[] constructors = clazz.getConstructors();
|
||||||
|
if (constructors.length != 1) continue;
|
||||||
|
Constructor<?> constructor = constructors[0];
|
||||||
|
if (needsTypeMapper(constructor)) {
|
||||||
|
parentConstructors.add(constructor);
|
||||||
|
parentAnnotations.add(annotation);
|
||||||
|
} else {
|
||||||
|
if (sourceAnnotation != null) {
|
||||||
|
throw new IllegalArgumentException("Multiple source type mappers found for parameter " + parameter);
|
||||||
|
}
|
||||||
|
sourceConstructor = constructor;
|
||||||
|
sourceAnnotation = annotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AbstractTypeMapper<T, Object> current;
|
||||||
|
if (sourceAnnotation != null) {
|
||||||
|
current = createInstance(sourceConstructor, sourceAnnotation, type, localTypeMapper);
|
||||||
|
} else {
|
||||||
|
current = (AbstractTypeMapper<T, Object>) SWCommandUtils.getTypeMapper(parameter, localTypeMapper);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < parentConstructors.size(); i++) {
|
||||||
|
Constructor<?> constructor = parentConstructors.get(i);
|
||||||
|
Annotation annotation = parentAnnotations.get(i);
|
||||||
|
current = createInstance(constructor, annotation, type, localTypeMapper, current);
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean needsTypeMapper(Constructor<?> constructor) {
|
||||||
|
Class<?>[] parameterTypes = constructor.getParameterTypes();
|
||||||
|
for (Class<?> parameterType : parameterTypes) {
|
||||||
|
if (AbstractTypeMapper.class.isAssignableFrom(parameterType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void handleImplicitTypeValidator(Parameter parameter, CommandPart<T> commandPart, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||||
|
Annotation[] annotations = parameter.getAnnotations();
|
||||||
|
Map<Integer, List<AbstractValidator<T, Object>>> validators = new HashMap<>();
|
||||||
|
for (Annotation annotation : annotations) {
|
||||||
|
CommandMetaData.ImplicitValidator implicitValidator = annotation.annotationType().getAnnotation(CommandMetaData.ImplicitValidator.class);
|
||||||
|
if (implicitValidator == null) continue;
|
||||||
|
Class<?> clazz = implicitValidator.handler();
|
||||||
|
if (!AbstractValidator.class.isAssignableFrom(clazz)) continue;
|
||||||
|
Constructor<?>[] constructors = clazz.getConstructors();
|
||||||
|
if (constructors.length != 1) continue;
|
||||||
|
AbstractValidator<T, Object> validator = createInstance(constructors[0], annotation, commandPart.getType(), localValidator);
|
||||||
|
validators.computeIfAbsent(implicitValidator.order(), integer -> new ArrayList<>()).add(validator);
|
||||||
|
}
|
||||||
|
List<Integer> keys = new ArrayList<>(validators.keySet());
|
||||||
|
keys.sort(Integer::compareTo);
|
||||||
|
for (Integer key : keys) {
|
||||||
|
List<AbstractValidator<T, Object>> list = validators.get(key);
|
||||||
|
for (AbstractValidator<T, Object> validator : list) {
|
||||||
|
commandPart.addValidator(validator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> T createInstance(Constructor<?> constructor, Object... parameter) {
|
||||||
|
Class<?>[] types = constructor.getParameterTypes();
|
||||||
|
List<Object> objects = new ArrayList<>();
|
||||||
|
for (Class<?> clazz : types) {
|
||||||
|
boolean found = false;
|
||||||
|
for (Object o : parameter) {
|
||||||
|
if (clazz.isAssignableFrom(o.getClass())) {
|
||||||
|
objects.add(o);
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
throw new RuntimeException("Could not find type " + clazz + " for constructor " + constructor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return (T) constructor.newInstance(objects.toArray());
|
||||||
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final AbstractValidator<?, Object> NULL_FILTER = (sender, value, messageSender) -> value != null;
|
||||||
|
|
||||||
|
private static final AbstractValidator<?, Object> STRING_SPACE_FILTER = (sender, value, messageSender) -> {
|
||||||
|
if (!(value instanceof String)) return true;
|
||||||
|
return !((String) value).contains(" ");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -160,32 +160,36 @@ public class Statement implements AutoCloseable {
|
|||||||
|
|
||||||
private <T> T withConnection(SQLRunnable<T> runnable, Object... objects) {
|
private <T> T withConnection(SQLRunnable<T> runnable, Object... objects) {
|
||||||
Connection connection = aquireConnection();
|
Connection connection = aquireConnection();
|
||||||
|
T result;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
try {
|
result = tryWithConnection(connection, runnable, objects);
|
||||||
return tryWithConnection(connection, runnable, objects);
|
} catch (Throwable e) {
|
||||||
} finally {
|
|
||||||
if(connectionInvalid(connection)) {
|
|
||||||
closeConnection(connection);
|
|
||||||
} else {
|
|
||||||
synchronized (connections) {
|
|
||||||
connections.push(connection);
|
|
||||||
connections.notify();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
if(connectionInvalid(connection)) {
|
if(connectionInvalid(connection)) {
|
||||||
|
closeConnection(connection);
|
||||||
|
|
||||||
return withConnection(runnable, objects);
|
return withConnection(runnable, objects);
|
||||||
} else {
|
} else {
|
||||||
|
synchronized (connections) {
|
||||||
|
connections.push(connection);
|
||||||
|
connections.notify();
|
||||||
|
}
|
||||||
|
|
||||||
throw new SecurityException("Failing sql statement", e);
|
throw new SecurityException("Failing sql statement", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (connections) {
|
||||||
|
connections.push(connection);
|
||||||
|
connections.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean connectionInvalid(Connection connection) {
|
private boolean connectionInvalid(Connection connection) {
|
||||||
try {
|
try {
|
||||||
return connection.isClosed();
|
return connection.isClosed() || !connection.isValid(1);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
logger.log(Level.INFO, "Could not check SQL connection status", e); // No database logging possible at this state
|
logger.log(Level.INFO, "Could not check SQL connection status", e); // No database logging possible at this state
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
build:
|
setup:
|
||||||
- "ln -s /home/gitea/lib"
|
- "ln -s /home/gitea/lib"
|
||||||
- "cp ~/gradle.properties ."
|
|
||||||
- "chmod u+x build.gradle"
|
build:
|
||||||
- "./gradlew buildProject"
|
- "./gradlew buildProject"
|
||||||
|
- "./gradlew --stop"
|
||||||
|
@ -39,17 +39,18 @@ public class BetterExceptionCommand extends TestSWCommand {
|
|||||||
public TestTypeMapper<String> tabCompleteException() {
|
public TestTypeMapper<String> tabCompleteException() {
|
||||||
return new TestTypeMapper<String>() {
|
return new TestTypeMapper<String>() {
|
||||||
@Override
|
@Override
|
||||||
public String map(String sender, String[] previousArguments, String s) {
|
public String map(String sender, PreviousArguments previousArguments, String s) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(String sender, String value, MessageSender messageSender) {
|
public boolean validate(String sender, String value, MessageSender messageSender) {
|
||||||
|
System.out.println("Validate: " + value);
|
||||||
throw new SecurityException();
|
throw new SecurityException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> tabCompletes(String sender, String[] previousArguments, String s) {
|
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||||
throw new SecurityException();
|
throw new SecurityException();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -44,12 +44,12 @@ public class CacheCommand extends TestSWCommand {
|
|||||||
System.out.println("TypeMapper register");
|
System.out.println("TypeMapper register");
|
||||||
return new TestTypeMapper<Integer>() {
|
return new TestTypeMapper<Integer>() {
|
||||||
@Override
|
@Override
|
||||||
public Integer map(String sender, String[] previousArguments, String s) {
|
public Integer map(String sender, PreviousArguments previousArguments, String s) {
|
||||||
return Integer.parseInt(s);
|
return Integer.parseInt(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> tabCompletes(String sender, String[] previousArguments, String s) {
|
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||||
return Arrays.asList(count.getAndIncrement() + "");
|
return Arrays.asList(count.getAndIncrement() + "");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -43,7 +43,7 @@ public class NullMapperCommand extends TestSWCommand {
|
|||||||
public TestTypeMapper<String> typeMapper() {
|
public TestTypeMapper<String> typeMapper() {
|
||||||
return new TestTypeMapper<String>() {
|
return new TestTypeMapper<String>() {
|
||||||
@Override
|
@Override
|
||||||
public String map(String sender, String[] previousArguments, String s) {
|
public String map(String sender, PreviousArguments previousArguments, String s) {
|
||||||
if (s.equals("Hello World")) {
|
if (s.equals("Hello World")) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ public class NullMapperCommand extends TestSWCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> tabCompletes(String sender, String[] previousArguments, String s) {
|
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* This file is a part of the SteamWar software.
|
* This file is a part of the SteamWar software.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -19,7 +19,17 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
class CommandNoHelpException extends RuntimeException {
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import de.steamwar.command.dto.TestSWCommand;
|
||||||
|
|
||||||
CommandNoHelpException() {}
|
public class NumberValidatorCommand extends TestSWCommand {
|
||||||
|
|
||||||
|
public NumberValidatorCommand() {
|
||||||
|
super("numberValidator");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void test(String sender, @Min(intValue = 0) @Max(intValue = 10) int i) {
|
||||||
|
throw new ExecutionIdentifier("RunNumberValidator with int");
|
||||||
|
}
|
||||||
}
|
}
|
51
testsrc/de/steamwar/command/NumberValidatorCommandTest.java
Normale Datei
51
testsrc/de/steamwar/command/NumberValidatorCommandTest.java
Normale Datei
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static de.steamwar.AssertionUtils.assertCMDFramework;
|
||||||
|
|
||||||
|
public class NumberValidatorCommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinValue() {
|
||||||
|
NumberValidatorCommand command = new NumberValidatorCommand();
|
||||||
|
command.execute("sender", "", new String[]{"-1"});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMaxValue() {
|
||||||
|
NumberValidatorCommand command = new NumberValidatorCommand();
|
||||||
|
command.execute("sender", "", new String[]{"11"});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidValue() {
|
||||||
|
try {
|
||||||
|
NumberValidatorCommand command = new NumberValidatorCommand();
|
||||||
|
command.execute("sender", "", new String[]{"2"});
|
||||||
|
assert false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertCMDFramework(e, ExecutionIdentifier.class, "RunNumberValidator with int");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
54
testsrc/de/steamwar/command/PreviousArgumentCommand.java
Normale Datei
54
testsrc/de/steamwar/command/PreviousArgumentCommand.java
Normale Datei
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import de.steamwar.command.dto.TestSWCommand;
|
||||||
|
import de.steamwar.command.dto.TestTypeMapper;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
public class PreviousArgumentCommand extends TestSWCommand {
|
||||||
|
|
||||||
|
public PreviousArgumentCommand() {
|
||||||
|
super("previous");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void genericCommand(String s, int i, String l) {
|
||||||
|
throw new ExecutionIdentifier(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ClassMapper(value = String.class, local = true)
|
||||||
|
public TestTypeMapper<String> typeMapper() {
|
||||||
|
return new TestTypeMapper<String>() {
|
||||||
|
@Override
|
||||||
|
public String map(String sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return "RunTypeMapper_" + previousArguments.getMappedArg(0) + "_" + previousArguments.getMappedArg(0).getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> tabCompletes(String sender, PreviousArguments previousArguments, String s) {
|
||||||
|
return Arrays.asList(previousArguments.getMappedArg(0) + "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
56
testsrc/de/steamwar/command/PreviousArgumentCommandTest.java
Normale Datei
56
testsrc/de/steamwar/command/PreviousArgumentCommandTest.java
Normale Datei
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static de.steamwar.AssertionUtils.assertCMDFramework;
|
||||||
|
import static de.steamwar.AssertionUtils.assertTabCompletes;
|
||||||
|
|
||||||
|
public class PreviousArgumentCommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrevArg1() {
|
||||||
|
PreviousArgumentCommand command = new PreviousArgumentCommand();
|
||||||
|
List<String> strings = command.tabComplete("", "", new String[]{"1", ""});
|
||||||
|
assertTabCompletes(strings, "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrevArg2() {
|
||||||
|
PreviousArgumentCommand command = new PreviousArgumentCommand();
|
||||||
|
List<String> strings = command.tabComplete("", "", new String[]{"2", ""});
|
||||||
|
assertTabCompletes(strings, "2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrevArgExecute() {
|
||||||
|
PreviousArgumentCommand command = new PreviousArgumentCommand();
|
||||||
|
try {
|
||||||
|
command.execute("", "", new String[]{"2", "2"});
|
||||||
|
assert false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertCMDFramework(e, ExecutionIdentifier.class, "RunTypeMapper_2_Integer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
import de.steamwar.command.AbstractSWCommand.Register;
|
|
||||||
import de.steamwar.command.dto.ExecutionIdentifier;
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
import de.steamwar.command.dto.TestSWCommand;
|
import de.steamwar.command.dto.TestSWCommand;
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ public class SimpleCommand extends TestSWCommand {
|
|||||||
super("simple");
|
super("simple");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Register(value = "a", help = true)
|
@Register(value = "a", noTabComplete = true)
|
||||||
public void test(String s, String... varargs) {
|
public void test(String s, String... varargs) {
|
||||||
throw new ExecutionIdentifier("RunSimple with Varargs");
|
throw new ExecutionIdentifier("RunSimple with Varargs");
|
||||||
}
|
}
|
||||||
|
@ -51,9 +51,11 @@ public class TestSWCommand extends AbstractSWCommand<String> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void commandSystemError(String sender, CommandFrameworkException e) {
|
protected void commandSystemError(String sender, CommandFrameworkException e) {
|
||||||
|
System.out.println("CommandSystemError: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void commandSystemWarning(Supplier<String> message) {
|
protected void commandSystemWarning(Supplier<String> message) {
|
||||||
|
System.out.println("CommandSystemWarning: " + message.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren