Update validators
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-06-16 15:59:28 +02:00
Ursprung b8d659d1c6
Commit fd6612ac1b
6 geänderte Dateien mit 146 neuen und 18 gelöschten Zeilen

Datei anzeigen

@ -65,6 +65,8 @@ public abstract class AbstractSWCommand<T> {
System.out.println(message.get()); System.out.println(message.get());
} }
protected void sendMessage(T sender, String message, Object[] args) {}
protected final void execute(T sender, String alias, String[] args) { protected final void execute(T sender, String alias, String[] args) {
initialize(); initialize();
try { try {
@ -156,7 +158,7 @@ public abstract class AbstractSWCommand<T> {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return; return;
} }
commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete())); commandHelpList.add(new SubCommand<>(this, method, anno.value(), new HashMap<>(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete()));
}); });
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
@ -177,7 +179,7 @@ public abstract class AbstractSWCommand<T> {
return; return;
} }
} }
commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description(), anno.noTabComplete())); commandList.add(new SubCommand<>(this, method, anno.value(), localTypeMapper, localValidators, localGuardChecker, false, anno.description(), anno.noTabComplete()));
}); });
} }

Datei anzeigen

@ -19,8 +19,19 @@
package de.steamwar.command; package de.steamwar.command;
import java.util.function.BiConsumer;
public interface AbstractValidator<K, T> { public interface AbstractValidator<K, T> {
default boolean validate(K sender, T value) {
/**
* Validates the given value.
*
* @param sender The sender of the command.
* @param value The value to validate or null if mapping returned null.
* @param messageSender The message sender to send messages to the player. Never send messages directly to the player.
* @return The result of the validation.
*/
default boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender) {
return true; return true;
} }
} }

Datei anzeigen

@ -37,7 +37,9 @@ class CommandPart<T> {
private final Object value; private final Object value;
} }
private AbstractSWCommand<T> command;
private AbstractTypeMapper<T, ?> typeMapper; private AbstractTypeMapper<T, ?> typeMapper;
private AbstractValidator<T, Object> validator;
private AbstractGuardChecker<T> guardChecker; private AbstractGuardChecker<T> guardChecker;
private Class<?> varArgType; private Class<?> varArgType;
private String optional; private String optional;
@ -48,8 +50,10 @@ class CommandPart<T> {
@Setter @Setter
private boolean ignoreAsArgument = false; private boolean ignoreAsArgument = false;
public CommandPart(AbstractTypeMapper<T, ?> typeMapper, AbstractGuardChecker<T> guardChecker, Class<?> varArgType, String optional, GuardCheckType guardCheckType) { public CommandPart(AbstractSWCommand<T> command, AbstractTypeMapper<T, ?> typeMapper, AbstractValidator<T, Object> validator, AbstractGuardChecker<T> guardChecker, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
this.command = command;
this.typeMapper = typeMapper; this.typeMapper = typeMapper;
this.validator = validator;
this.guardChecker = guardChecker; this.guardChecker = guardChecker;
this.varArgType = varArgType; this.varArgType = varArgType;
this.optional = optional; this.optional = optional;
@ -117,6 +121,30 @@ class CommandPart<T> {
} }
} }
public boolean validate(T sender, List<Object> values, int startIndex) {
if (varArgType != null) {
for (int i = startIndex; i < values.size(); i++) {
Object value = values.get(i);
if (validator != null && !validator.validate(sender, value, (s, objects) -> command.sendMessage(sender, s, objects))) {
return false;
}
}
return true;
}
Object value = values.get(startIndex);
if (validator != null && !validator.validate(sender, value, (s, objects) -> command.sendMessage(sender, s, objects))) {
if (optional != null && next != null) {
return next.validate(sender, values, startIndex);
}
return false;
}
if (next != null) {
return next.validate(sender, values, startIndex + 1);
}
return true;
}
public boolean guardCheck(T sender, String[] args, int startIndex) { public boolean guardCheck(T sender, String[] args, int startIndex) {
if (varArgType != null) { if (varArgType != null) {
for (int i = startIndex; i < args.length; i++) { for (int i = startIndex; i < args.length; i++) {
@ -192,6 +220,15 @@ class CommandPart<T> {
if (value == null) { if (value == null) {
return new CheckArgumentResult(false, null); return new CheckArgumentResult(false, null);
} }
if (validator != null) {
if (!validator.validate(sender, value, (s, objects) -> {
// ignore
})) {
return new CheckArgumentResult(false, null);
}
return new CheckArgumentResult(true, value);
}
GuardResult guardResult = checkGuard(guardCheckType, sender, args, index); GuardResult guardResult = checkGuard(guardCheckType, sender, args, index);
switch (guardResult) { switch (guardResult) {
case ALLOWED: case ALLOWED:

Datei anzeigen

@ -26,6 +26,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -77,11 +78,11 @@ public class SWCommandUtils {
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper); MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
} }
static <T> CommandPart<T> generateCommandPart(boolean help, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractGuardChecker<T>> localGuardChecker) { static <T> CommandPart<T> generateCommandPart(AbstractSWCommand<T> command, boolean help, String[] subCommand, Parameter[] parameters, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
CommandPart<T> first = null; CommandPart<T> first = null;
CommandPart<T> current = null; CommandPart<T> current = null;
for (String s : subCommand) { for (String s : subCommand) {
CommandPart commandPart = new CommandPart(createMapper(s), null, null, null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND); CommandPart commandPart = new CommandPart(command, createMapper(s), null, null, null, null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
commandPart.setIgnoreAsArgument(true); commandPart.setIgnoreAsArgument(true);
if (current != null) { if (current != null) {
current.setNext(commandPart); current.setNext(commandPart);
@ -94,11 +95,12 @@ public class SWCommandUtils {
for (int i = 1; i < parameters.length; i++) { for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i]; Parameter parameter = parameters[i];
AbstractTypeMapper<T, ?> typeMapper = getTypeMapper(parameter, localTypeMapper); AbstractTypeMapper<T, ?> typeMapper = getTypeMapper(parameter, localTypeMapper);
AbstractValidator<T, Object> validator = (AbstractValidator<T, Object>) getValidator(parameter, localValidator);
AbstractGuardChecker<T> guardChecker = getGuardChecker(parameter, localGuardChecker); AbstractGuardChecker<T> guardChecker = getGuardChecker(parameter, localGuardChecker);
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null; Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class); AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class);
CommandPart<T> commandPart = new CommandPart<>(typeMapper, guardChecker, varArgType, optionalValue != null ? optionalValue.value() : null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND); CommandPart<T> commandPart = new CommandPart<>(command, typeMapper, validator, guardChecker, varArgType, optionalValue != null ? optionalValue.value() : null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
if (current != null) { if (current != null) {
current.setNext(commandPart); current.setNext(commandPart);
} }
@ -119,7 +121,7 @@ public class SWCommandUtils {
AbstractSWCommand.ClassMapper classMapper = parameter.getAnnotation(AbstractSWCommand.ClassMapper.class); AbstractSWCommand.ClassMapper classMapper = parameter.getAnnotation(AbstractSWCommand.ClassMapper.class);
AbstractSWCommand.Mapper mapper = parameter.getAnnotation(AbstractSWCommand.Mapper.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())) { if (clazz.isEnum() && classMapper == null && mapper == null && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
return (AbstractTypeMapper<T, ?>) createEnumMapper((Class<Enum<?>>) clazz); return createEnumMapper((Class<Enum<?>>) clazz);
} }
String name = clazz.getTypeName(); String name = clazz.getTypeName();
@ -165,6 +167,49 @@ public class SWCommandUtils {
return typeMapper; return typeMapper;
} }
public static <T> AbstractValidator<T, ?> getValidator(Parameter parameter, Map<String, AbstractValidator<T, ?>> localValidator) {
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs()) {
clazz = clazz.getComponentType();
}
AbstractSWCommand.ClassValidator classValidator = parameter.getAnnotation(AbstractSWCommand.ClassValidator.class);
if (classValidator != null) {
if (classValidator.value() != null) {
return getValidator(classValidator.value().getTypeName(), localValidator);
}
return getValidator(clazz.getTypeName(), localValidator);
}
AbstractSWCommand.Validator validator = parameter.getAnnotation(AbstractSWCommand.Validator.class);
if (validator != null) {
if (validator.value() != null) {
return getValidator(validator.value(), localValidator);
}
return getValidator(clazz.getTypeName(), localValidator);
}
AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class);
if (errorMessage != null) {
return new AbstractValidator<T, String>() {
@Override
public boolean validate(T sender, String value, BiConsumer<String, Object[]> messageSender) {
if (value == null) messageSender.accept(errorMessage.value(), new Object[0]);
return value != null;
}
};
}
return null;
}
private static <T> AbstractValidator<T, ?> getValidator(String s, Map<String, AbstractValidator<T, ?>> localGuardChecker) {
AbstractValidator<T, ?> validator = localGuardChecker.getOrDefault(s, (AbstractValidator<T, ?>) VALIDATOR_FUNCTIONS.getOrDefault(s, null));
if (validator == null) {
throw new IllegalArgumentException("No validator found for " + s);
}
return validator;
}
public static <T> AbstractGuardChecker<T> getGuardChecker(Parameter parameter, Map<String, AbstractGuardChecker<T>> localGuardChecker) { public static <T> AbstractGuardChecker<T> getGuardChecker(Parameter parameter, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
Class<?> clazz = parameter.getType(); Class<?> clazz = parameter.getType();
if (parameter.isVarArgs()) { if (parameter.isVarArgs()) {

Datei anzeigen

@ -36,13 +36,14 @@ public class SubCommand<T> {
String[] subCommand; String[] subCommand;
private Predicate<T> senderPredicate; private Predicate<T> senderPredicate;
private Function<T, ?> senderFunction; private Function<T, ?> senderFunction;
AbstractValidator<T, T> validator;
AbstractGuardChecker<T> guardChecker; AbstractGuardChecker<T> guardChecker;
boolean noTabComplete; boolean noTabComplete;
int comparableValue; int comparableValue;
private CommandPart<T> commandPart; private CommandPart<T> commandPart;
SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractGuardChecker<T>> localGuardChecker, boolean help, String[] description, boolean noTabComplete) { SubCommand(AbstractSWCommand<T> abstractSWCommand, Method method, String[] subCommand, Map<String, AbstractTypeMapper<T, ?>> localTypeMapper, Map<String, AbstractValidator<T, ?>> localValidator, Map<String, AbstractGuardChecker<T>> localGuardChecker, boolean help, String[] description, boolean noTabComplete) {
this.abstractSWCommand = abstractSWCommand; this.abstractSWCommand = abstractSWCommand;
this.method = method; this.method = method;
this.subCommand = subCommand; this.subCommand = subCommand;
@ -52,9 +53,10 @@ 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);
guardChecker = SWCommandUtils.getGuardChecker(parameters[0], localGuardChecker); guardChecker = SWCommandUtils.getGuardChecker(parameters[0], localGuardChecker);
commandPart = SWCommandUtils.generateCommandPart(help, subCommand, parameters, localTypeMapper, localGuardChecker); commandPart = SWCommandUtils.generateCommandPart(abstractSWCommand, help, subCommand, parameters, localTypeMapper, localValidator, localGuardChecker);
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);
@ -70,12 +72,13 @@ public class SubCommand<T> {
if (args.length != 0) { if (args.length != 0) {
return false; return false;
} }
method.setAccessible(true); if (validator != null) {
method.invoke(abstractSWCommand, senderFunction.apply(sender)); if (!validator.validate(sender, sender, (s, objectArgs) -> {
} else { // TODO: implement
List<Object> objects = new ArrayList<>(); })) {
commandPart.generateArgumentArray(objects, sender, args, 0); throw new CommandNoHelpException();
if (guardChecker != null) { }
} else if (guardChecker != null) {
GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null); GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null);
switch (guardResult) { switch (guardResult) {
case ALLOWED: case ALLOWED:
@ -87,6 +90,30 @@ public class SubCommand<T> {
return true; return true;
} }
} }
method.setAccessible(true);
method.invoke(abstractSWCommand, senderFunction.apply(sender));
} else {
List<Object> objects = new ArrayList<>();
commandPart.generateArgumentArray(objects, sender, args, 0);
if (validator != null) {
if (!validator.validate(sender, sender, (s, objectArgs) -> {
// TODO: implement
})) {
throw new CommandNoHelpException();
}
} else if (guardChecker != null) {
GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null);
switch (guardResult) {
case ALLOWED:
break;
case DENIED:
throw new CommandNoHelpException();
case DENIED_WITH_HELP:
default:
return true;
}
}
commandPart.validate(sender, objects, 0);
commandPart.guardCheck(sender, args, 0); commandPart.guardCheck(sender, args, 0);
objects.add(0, senderFunction.apply(sender)); objects.add(0, senderFunction.apply(sender));
method.setAccessible(true); method.setAccessible(true);
@ -105,7 +132,13 @@ public class SubCommand<T> {
} }
List<String> tabComplete(T sender, String[] args) { List<String> tabComplete(T sender, String[] args) {
if (guardChecker != null && guardChecker.guard(sender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) { if (validator != null) {
if (!validator.validate(sender, sender, (s, objects) -> {
// TODO: implement
})) {
return null;
}
} else if (guardChecker != null && guardChecker.guard(sender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) {
return null; return null;
} }
if (commandPart == null) { if (commandPart == null) {

Datei anzeigen

@ -35,7 +35,7 @@ public class GuardCommandTest {
try { try {
cmd.execute("test", "", new String[0]); cmd.execute("test", "", new String[0]);
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunTypeMapper"); assertThat(e.getMessage(), is("GuardChecker COMMAND"));
} }
} }