CommandFramework #7
@ -19,6 +19,7 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
@Deprecated
|
||||
@FunctionalInterface
|
||||
public interface AbstractGuardChecker<T> {
|
||||
/**
|
||||
|
@ -38,6 +38,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
|
||||
private final Map<String, AbstractTypeMapper<T, ?>> localTypeMapper = new HashMap<>();
|
||||
private final Map<String, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
|
||||
private final Map<String, AbstractValidator<T, ?>> localValidators = new HashMap<>();
|
||||
|
||||
protected AbstractSWCommand(Class<T> clazz, String command) {
|
||||
this(clazz, command, new String[0]);
|
||||
@ -64,11 +65,15 @@ public abstract class AbstractSWCommand<T> {
|
||||
System.out.println(message.get());
|
||||
}
|
||||
|
||||
protected void sendMessage(T sender, String message, Object[] args) {}
|
||||
|
||||
protected final void execute(T sender, String alias, String[] args) {
|
||||
initialize();
|
||||
List<Runnable> errors = new ArrayList<>();
|
||||
try {
|
||||
if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) {
|
||||
commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args));
|
||||
if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, args))) {
|
||||
errors.forEach(Runnable::run);
|
||||
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {}, sender, alias, args));
|
||||
}
|
||||
} catch (CommandNoHelpException e) {
|
||||
// Ignored
|
||||
@ -127,6 +132,20 @@ public abstract class AbstractSWCommand<T> {
|
||||
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
|
||||
}
|
||||
});
|
||||
addValidator(Validator.class, method, i -> i == 0, false, AbstractValidator.class, (anno, validator) -> {
|
||||
if (anno.local()) {
|
||||
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) -> {
|
||||
if (anno.local()) {
|
||||
localValidators.putIfAbsent(anno.value().getTypeName(), (AbstractValidator<T, ?>) validator);
|
||||
} else {
|
||||
SWCommandUtils.getVALIDATOR_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), validator);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (Method method : methods) {
|
||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||
@ -141,7 +160,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
||||
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) -> {
|
||||
@ -162,7 +181,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
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()));
|
||||
});
|
||||
}
|
||||
|
||||
@ -217,6 +236,18 @@ public abstract class AbstractSWCommand<T> {
|
||||
});
|
||||
}
|
||||
|
||||
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) {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private <T extends Annotation> void addGuard(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, AbstractGuardChecker<?>> consumer) {
|
||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
||||
try {
|
||||
@ -280,6 +311,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
boolean local() default false;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||
protected @interface Guard {
|
||||
@ -288,6 +320,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
boolean local() default false;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
protected @interface ClassGuard {
|
||||
@ -296,6 +329,22 @@ public abstract class AbstractSWCommand<T> {
|
||||
boolean local() default false;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||
protected @interface Validator {
|
||||
String value() default "";
|
||||
|
||||
boolean local() default false;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
protected @interface ClassValidator {
|
||||
Class<?> value();
|
||||
|
||||
boolean local() default false;
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
protected @interface StaticValue {
|
||||
@ -320,4 +369,13 @@ public abstract class AbstractSWCommand<T> {
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
protected @interface ErrorMessage {
|
||||
/**
|
||||
* Error message to be displayed when the parameter is invalid.
|
||||
*/
|
||||
String value();
|
||||
}
|
||||
}
|
||||
|
@ -20,12 +20,18 @@
|
||||
package de.steamwar.command;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface AbstractTypeMapper<K, T> {
|
||||
public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
|
||||
/**
|
||||
* The CommandSender can be null!
|
||||
*/
|
||||
T map(K sender, String[] previousArguments, String s);
|
||||
|
||||
@Override
|
||||
default boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Collection<String> tabCompletes(K sender, String[] previousArguments, String s);
|
||||
}
|
||||
|
36
src/de/steamwar/command/AbstractValidator.java
Normale Datei
36
src/de/steamwar/command/AbstractValidator.java
Normale Datei
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.function.BiConsumer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AbstractValidator<K, T> {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender);
|
||||
}
|
@ -23,9 +23,11 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class CommandPart<T> {
|
||||
|
||||
@ -37,7 +39,9 @@ class CommandPart<T> {
|
||||
private final Object value;
|
||||
}
|
||||
|
||||
private AbstractSWCommand<T> command;
|
||||
private AbstractTypeMapper<T, ?> typeMapper;
|
||||
private AbstractValidator<T, Object> validator;
|
||||
private AbstractGuardChecker<T> guardChecker;
|
||||
private Class<?> varArgType;
|
||||
private String optional;
|
||||
@ -48,8 +52,10 @@ class CommandPart<T> {
|
||||
@Setter
|
||||
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.validator = validator;
|
||||
this.guardChecker = guardChecker;
|
||||
this.varArgType = varArgType;
|
||||
this.optional = optional;
|
||||
@ -82,11 +88,11 @@ class CommandPart<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void generateArgumentArray(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) {
|
||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
||||
for (int i = startIndex; i < args.length; i++) {
|
||||
CheckArgumentResult validArgument = checkArgument(null, sender, args, i);
|
||||
CheckArgumentResult validArgument = checkArgument(errors, null, sender, args, i);
|
||||
if (!validArgument.success) {
|
||||
throw new CommandParseException();
|
||||
}
|
||||
@ -96,7 +102,7 @@ class CommandPart<T> {
|
||||
return;
|
||||
}
|
||||
|
||||
CheckArgumentResult validArgument = checkArgument(null, sender, args, startIndex);
|
||||
CheckArgumentResult validArgument = checkArgument(errors, null, sender, args, startIndex);
|
||||
if (!validArgument.success && optional == null) {
|
||||
throw new CommandParseException();
|
||||
}
|
||||
@ -105,7 +111,7 @@ class CommandPart<T> {
|
||||
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
|
||||
}
|
||||
if (next != null) {
|
||||
next.generateArgumentArray(current, sender, args, startIndex);
|
||||
next.generateArgumentArray(errors, current, sender, args, startIndex);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -113,7 +119,7 @@ class CommandPart<T> {
|
||||
current.add(validArgument.value);
|
||||
}
|
||||
if (next != null) {
|
||||
next.generateArgumentArray(current, sender, args, startIndex + 1);
|
||||
next.generateArgumentArray(errors, current, sender, args, startIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,7 +159,7 @@ class CommandPart<T> {
|
||||
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
|
||||
if (varArgType != null) {
|
||||
for (int i = startIndex; i < args.length - 1; i++) {
|
||||
CheckArgumentResult validArgument = checkArgument(GuardCheckType.TAB_COMPLETE, sender, args, i);
|
||||
CheckArgumentResult validArgument = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, i);
|
||||
if (!validArgument.success) {
|
||||
return;
|
||||
}
|
||||
@ -166,7 +172,7 @@ class CommandPart<T> {
|
||||
}
|
||||
|
||||
if (args.length - 1 > startIndex) {
|
||||
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, sender, args, startIndex);
|
||||
CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, startIndex);
|
||||
if (checkArgumentResult.success && next != null) {
|
||||
next.generateTabComplete(current, sender, args, startIndex + 1);
|
||||
return;
|
||||
@ -186,12 +192,23 @@ class CommandPart<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private CheckArgumentResult checkArgument(GuardCheckType guardCheckType, T sender, String[] args, int index) {
|
||||
private CheckArgumentResult checkArgument(Consumer<Runnable> errors, GuardCheckType guardCheckType, T sender, String[] args, int index) {
|
||||
try {
|
||||
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
|
||||
if (validator != null) {
|
||||
if (!validator.validate(sender, value, (s, objects) -> {
|
||||
errors.accept(() -> {
|
||||
command.sendMessage(sender, s, objects);
|
||||
});
|
||||
})) {
|
||||
return new CheckArgumentResult(false, null);
|
||||
}
|
||||
return new CheckArgumentResult(value != null, value);
|
||||
}
|
||||
if (value == null) {
|
||||
return new CheckArgumentResult(false, null);
|
||||
}
|
||||
|
||||
GuardResult guardResult = checkGuard(guardCheckType, sender, args, index);
|
||||
switch (guardResult) {
|
||||
case ALLOWED:
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
@Deprecated
|
||||
public enum GuardCheckType {
|
||||
COMMAND,
|
||||
HELP_COMMAND,
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
@Deprecated
|
||||
public enum GuardResult {
|
||||
ALLOWED,
|
||||
DENIED_WITH_HELP,
|
||||
|
@ -26,8 +26,10 @@ import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@UtilityClass
|
||||
public class SWCommandUtils {
|
||||
@ -36,8 +38,12 @@ public class SWCommandUtils {
|
||||
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||
|
||||
@Getter
|
||||
@Deprecated
|
||||
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
||||
|
||||
@Getter
|
||||
private final Map<String, AbstractValidator<?, ?>> VALIDATOR_FUNCTIONS = new HashMap<>();
|
||||
|
||||
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
|
||||
@Override
|
||||
public Object map(Object sender, String[] previousArguments, String s) {
|
||||
@ -72,11 +78,11 @@ public class SWCommandUtils {
|
||||
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> current = null;
|
||||
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);
|
||||
if (current != null) {
|
||||
current.setNext(commandPart);
|
||||
@ -89,11 +95,12 @@ public class SWCommandUtils {
|
||||
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);
|
||||
AbstractGuardChecker<T> guardChecker = getGuardChecker(parameter, localGuardChecker);
|
||||
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
||||
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) {
|
||||
current.setNext(commandPart);
|
||||
}
|
||||
@ -114,7 +121,7 @@ public class SWCommandUtils {
|
||||
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 (AbstractTypeMapper<T, ?>) createEnumMapper((Class<Enum<?>>) clazz);
|
||||
return createEnumMapper((Class<Enum<?>>) clazz);
|
||||
}
|
||||
|
||||
String name = clazz.getTypeName();
|
||||
@ -160,6 +167,47 @@ public class SWCommandUtils {
|
||||
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 && !validator.value().isEmpty()) {
|
||||
return getValidator(validator.value(), localValidator);
|
||||
}
|
||||
return getValidator(clazz.getTypeName(), localValidator);
|
||||
}
|
||||
|
||||
AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class);
|
||||
if (errorMessage != null) {
|
||||
return (AbstractValidator<T, String>) (sender, value, 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;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> AbstractGuardChecker<T> getGuardChecker(Parameter parameter, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
|
||||
Class<?> clazz = parameter.getType();
|
||||
if (parameter.isVarArgs()) {
|
||||
@ -184,6 +232,7 @@ public class SWCommandUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static <T> AbstractGuardChecker<T> getGuardChecker(String s, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
|
||||
AbstractGuardChecker<T> guardChecker = localGuardChecker.getOrDefault(s, (AbstractGuardChecker<T>) GUARD_FUNCTIONS.getOrDefault(s, null));
|
||||
if (guardChecker == null) {
|
||||
@ -200,17 +249,28 @@ public class SWCommandUtils {
|
||||
MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
|
||||
}
|
||||
|
||||
public static <T> void addValidator(Class<T> clazz, AbstractValidator<T, ?> validator) {
|
||||
addValidator(clazz.getTypeName(), validator);
|
||||
}
|
||||
|
||||
public static <T> void addValidator(String name, AbstractValidator<T, ?> validator) {
|
||||
VALIDATOR_FUNCTIONS.putIfAbsent(name, validator);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> void addGuard(Class<?> clazz, AbstractGuardChecker<T> guardChecker) {
|
||||
addGuard(clazz.getTypeName(), guardChecker);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static <T> void addGuard(String name, AbstractGuardChecker<T> guardChecker) {
|
||||
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
|
||||
}
|
||||
|
||||
public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
|
||||
List<String> strings = Arrays.asList(values);
|
||||
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
|
||||
List<String> strings = Arrays.stream(values).map(String::toLowerCase).collect(Collectors.toList());
|
||||
List<String> tabCompletes = Arrays.asList(values);
|
||||
return createMapper(s -> strings.contains(s.toLowerCase()) ? s : null, s -> tabCompletes);
|
||||
}
|
||||
|
||||
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {
|
||||
|
@ -25,6 +25,7 @@ import java.lang.reflect.Parameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@ -36,13 +37,14 @@ public class SubCommand<T> {
|
||||
String[] subCommand;
|
||||
private Predicate<T> senderPredicate;
|
||||
private Function<T, ?> senderFunction;
|
||||
AbstractValidator<T, T> validator;
|
||||
AbstractGuardChecker<T> guardChecker;
|
||||
boolean noTabComplete;
|
||||
int comparableValue;
|
||||
|
||||
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.method = method;
|
||||
this.subCommand = subCommand;
|
||||
@ -52,15 +54,16 @@ public class SubCommand<T> {
|
||||
Parameter[] parameters = method.getParameters();
|
||||
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);
|
||||
|
||||
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());
|
||||
senderFunction = t -> parameters[0].getType().cast(t);
|
||||
}
|
||||
|
||||
boolean invoke(T sender, String alias, String[] args) {
|
||||
boolean invoke(Consumer<Runnable> errors, T sender, String alias, String[] args) {
|
||||
try {
|
||||
if (!senderPredicate.test(sender)) {
|
||||
return false;
|
||||
@ -70,12 +73,36 @@ public class SubCommand<T> {
|
||||
if (args.length != 0) {
|
||||
return false;
|
||||
}
|
||||
if (validator != null) {
|
||||
if (!validator.validate(sender, sender, (s, objectArgs) -> {
|
||||
abstractSWCommand.sendMessage(sender, s, objectArgs);
|
||||
})) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
method.setAccessible(true);
|
||||
method.invoke(abstractSWCommand, senderFunction.apply(sender));
|
||||
} else {
|
||||
List<Object> objects = new ArrayList<>();
|
||||
commandPart.generateArgumentArray(objects, sender, args, 0);
|
||||
if (guardChecker != null) {
|
||||
commandPart.generateArgumentArray(errors, objects, sender, args, 0);
|
||||
if (validator != null) {
|
||||
if (!validator.validate(sender, sender, (s, objectArgs) -> {
|
||||
abstractSWCommand.sendMessage(sender, s, objectArgs);
|
||||
})) {
|
||||
throw new CommandNoHelpException();
|
||||
}
|
||||
} else if (guardChecker != null) {
|
||||
GuardResult guardResult = guardChecker.guard(sender, GuardCheckType.COMMAND, new String[0], null);
|
||||
switch (guardResult) {
|
||||
case ALLOWED:
|
||||
@ -105,7 +132,13 @@ public class SubCommand<T> {
|
||||
}
|
||||
|
||||
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) -> {
|
||||
// ignore
|
||||
})) {
|
||||
return null;
|
||||
}
|
||||
} else if (guardChecker != null && guardChecker.guard(sender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) {
|
||||
return null;
|
||||
}
|
||||
if (commandPart == null) {
|
||||
|
@ -35,7 +35,7 @@ public class GuardCommandTest {
|
||||
try {
|
||||
cmd.execute("test", "", new String[0]);
|
||||
} catch (Exception e) {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, "RunTypeMapper");
|
||||
assertThat(e.getMessage(), is("GuardChecker COMMAND"));
|
||||
}
|
||||
}
|
||||
|
||||
|
60
testsrc/de/steamwar/command/ValidatorCommand.java
Normale Datei
60
testsrc/de/steamwar/command/ValidatorCommand.java
Normale Datei
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.TestValidator;
|
||||
|
||||
public class ValidatorCommand extends TestSWCommand {
|
||||
|
||||
public ValidatorCommand() {
|
||||
super("testvalidator");
|
||||
}
|
||||
|
||||
@Register
|
||||
public void test(@Validator String sender) {
|
||||
throw new ExecutionIdentifier("RunTest");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void sendMessage(String sender, String message, Object[] args) {
|
||||
if (message.equals("Hello World")) {
|
||||
throw new ExecutionIdentifier("RunSendMessageWithHelloWorldParameter");
|
||||
}
|
||||
}
|
||||
|
||||
@Register
|
||||
public void onError(String sender, @ErrorMessage("Hello World") int error) {
|
||||
throw new ExecutionIdentifier("RunOnError");
|
||||
}
|
||||
|
||||
@Register
|
||||
public void onError(String sender, double error) {
|
||||
throw new ExecutionIdentifier("RunOnErrorDouble");
|
||||
}
|
||||
|
||||
@ClassValidator(value = String.class, local = true)
|
||||
public TestValidator<String> validator() {
|
||||
return (sender, value, messageSender) -> {
|
||||
throw new ExecutionIdentifier("RunValidator");
|
||||
};
|
||||
}
|
||||
}
|
63
testsrc/de/steamwar/command/ValidatorCommandTest.java
Normale Datei
63
testsrc/de/steamwar/command/ValidatorCommandTest.java
Normale Datei
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
public class ValidatorCommandTest {
|
||||
|
||||
@Test
|
||||
public void testValidator() {
|
||||
ValidatorCommand cmd = new ValidatorCommand();
|
||||
try {
|
||||
cmd.execute("test", "", new String[0]);
|
||||
assertThat(true, is(false));
|
||||
} catch (Exception e) {
|
||||
assertThat(e.getMessage(), is("RunValidator"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorMessage() {
|
||||
ValidatorCommand cmd = new ValidatorCommand();
|
||||
try {
|
||||
cmd.execute("test", "", new String[]{"Hello"});
|
||||
assertThat(true, is(false));
|
||||
} catch (Exception e) {
|
||||
assertThat(e.getMessage(), is("RunSendMessageWithHelloWorldParameter"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrorNoMessage() {
|
||||
ValidatorCommand cmd = new ValidatorCommand();
|
||||
try {
|
||||
cmd.execute("test", "", new String[]{"0.0"});
|
||||
assertThat(true, is(false));
|
||||
} catch (Exception e) {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, "RunOnErrorDouble");
|
||||
}
|
||||
}
|
||||
}
|
25
testsrc/de/steamwar/command/dto/TestValidator.java
Normale Datei
25
testsrc/de/steamwar/command/dto/TestValidator.java
Normale Datei
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.dto;
|
||||
|
||||
import de.steamwar.command.AbstractValidator;
|
||||
|
||||
public interface TestValidator<T> extends AbstractValidator<String, T> {
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren