Add CommandFramework (needs Message System for completion)?

Dieser Commit ist enthalten in:
yoyosource 2022-04-21 22:43:14 +02:00
Ursprung d992d9cae7
Commit 40eaa180c6
11 geänderte Dateien mit 1040 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,9 @@
package de.steamwar.command;
@FunctionalInterface
public interface AbstractGuardChecker<T> {
/**
* While guarding the first parameter of the command the parameter s of this method is {@code null}
*/
GuardResult guard(T t, GuardCheckType guardCheckType, String[] previousArguments, String s);
}

Datei anzeigen

@ -0,0 +1,304 @@
package de.steamwar.command;
import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.IntPredicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public abstract class AbstractSWCommand<T> {
private Class<?> clazz; // This is used in createMappings()
private boolean initialized = false;
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, AbstractGuardChecker<T>> localGuardChecker = new HashMap<>();
protected AbstractSWCommand(Class<T> clazz, String command) {
this(clazz, command, new String[0]);
}
protected AbstractSWCommand(Class<T> clazz, String command, String[] aliases) {
this.clazz = clazz;
createAndSafeCommand(command, aliases);
unregister();
register();
}
protected abstract void createAndSafeCommand(String command, String[] aliases);
public abstract void unregister();
public abstract void register();
protected void commandSystemError(T sender, CommandFrameworkException e) {
e.printStackTrace();
}
protected void commandSystemWarning(Supplier<String> message) {
System.out.println(message.get());
}
protected final void execute(T sender, String alias, String[] args) {
initialize();
try {
if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) {
commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args));
}
} catch (CommandNoHelpException e) {
// Ignored
} catch (CommandFrameworkException e) {
commandSystemError(sender, e);
throw e;
}
}
protected final List<String> tabComplete(T sender, String alias, String[] args) throws IllegalArgumentException {
initialize();
String string = args[args.length - 1].toLowerCase();
return commandList.stream()
.filter(s -> !s.noTabComplete)
.map(s -> s.tabComplete(sender, args))
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(s -> !s.isEmpty())
.filter(s -> s.toLowerCase().startsWith(string))
.collect(Collectors.toList());
}
private void initialize() {
if (initialized) return;
createMapping();
}
private synchronized void createMapping() {
List<Method> methods = methods();
for (Method method : methods) {
addMapper(Mapper.class, method, i -> i == 0, false, AbstractTypeMapper.class, (anno, typeMapper) -> {
if (anno.local()) {
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) -> {
if (anno.local()) {
localTypeMapper.putIfAbsent(anno.value().getTypeName(), (AbstractTypeMapper<T, ?>) typeMapper);
} else {
SWCommandUtils.getMAPPER_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), typeMapper);
}
});
addGuard(Guard.class, method, i -> i == 0, false, AbstractGuardChecker.class, (anno, guardChecker) -> {
if (anno.local()) {
localGuardChecker.putIfAbsent(anno.value(), (AbstractGuardChecker<T>) guardChecker);
} else {
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value(), guardChecker);
}
});
addGuard(ClassGuard.class, method, i -> i == 0, false, AbstractGuardChecker.class, (anno, guardChecker) -> {
if (anno.local()) {
localGuardChecker.putIfAbsent(anno.value().getTypeName(), (AbstractGuardChecker<T>) guardChecker);
} else {
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
}
});
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (!anno.help()) return;
if (parameters.length != 2) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking parameters or has too many");
}
if (!parameters[parameters.length - 1].isVarArgs()) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
}
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");
return;
}
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete()));
});
}
for (Method method : methods) {
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (anno.help()) return;
for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i];
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs() && i == parameters.length - 1) {
clazz = parameter.getType().getComponentType();
}
Mapper mapper = parameter.getAnnotation(Mapper.class);
if (clazz.isEnum() && mapper == null && !SWCommandUtils.getMAPPER_FUNCTIONS().containsKey(clazz.getTypeName())) {
continue;
}
String name = mapper != null ? mapper.value() : clazz.getTypeName();
if (!SWCommandUtils.getMAPPER_FUNCTIONS().containsKey(name) && !localTypeMapper.containsKey(name)) {
commandSystemWarning(() -> "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'");
return;
}
}
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description(), anno.noTabComplete()));
});
this.commandList.sort((o1, o2) -> {
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
if (compare != 0) {
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;
}
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
if (anno == null || anno.length == 0) return;
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())) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'");
return;
}
if (returnType != null && method.getReturnType() != returnType) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
return;
}
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) {
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
try {
method.setAccessible(true);
consumer.accept(anno, (AbstractTypeMapper<T, ?>) method.invoke(this));
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
});
}
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 {
method.setAccessible(true);
consumer.accept(anno, (AbstractGuardChecker<T>) method.invoke(this));
} catch (Exception e) {
throw new SecurityException(e.getMessage(), e);
}
});
}
// TODO: Implement this when Message System is ready
/*
public void addDefaultHelpMessage(String message) {
defaultHelpMessages.add(message);
}
*/
private List<Method> methods() {
List<Method> methods = new ArrayList<>();
Class<?> current = getClass();
while (current.getSuperclass() != AbstractSWCommand.class) {
methods.addAll(Arrays.asList(current.getDeclaredMethods()));
current = current.getSuperclass();
}
return methods;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Repeatable(Register.Registeres.class)
protected @interface Register {
String[] value() default {};
boolean help() default false;
String[] description() default {};
boolean noTabComplete() default false;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@interface Registeres {
Register[] value();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Mapper {
String value();
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface ClassMapper {
Class<?> value();
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
protected @interface Guard {
String value() default "";
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
protected @interface ClassGuard {
Class<?> value();
boolean local() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface StaticValue {
String[] value();
/**
* This is the short form for 'allowImplicitSwitchExpressions'
* and can be set to true if you want to allow int as well as boolean as annotated parameter types.
* The value array needs to be at least 2 long for this flag to be considered.
* While using an int, the value will represent the index into the value array.
* While using a boolean, the value array must only be 2 long and the value will be {@code false}
* for the first index and {@code true} for the second index.
*/
boolean allowISE() default false;
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
protected @interface OptionalValue {
/**
* Will pe parsed against the TypeMapper specified by the parameter or annotation.
*/
String value();
}
}

Datei anzeigen

@ -0,0 +1,12 @@
package de.steamwar.command;
import java.util.List;
public interface AbstractTypeMapper<K, T> {
/**
* The CommandSender can be null!
*/
T map(K sender, String[] previousArguments, String s);
List<String> tabCompletes(K sender, String[] previousArguments, String s);
}

Datei anzeigen

@ -0,0 +1,76 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
public class CommandFrameworkException extends RuntimeException {
private InvocationTargetException invocationTargetException;
private String alias;
private String[] args;
private String message;
CommandFrameworkException(InvocationTargetException invocationTargetException, String alias, String[] args) {
super(invocationTargetException);
this.invocationTargetException = invocationTargetException;
this.alias = alias;
this.args = args;
}
public synchronized String getBuildStackTrace() {
if (message != null) {
return message;
}
StackTraceElement[] stackTraceElements = invocationTargetException.getCause().getStackTrace();
StringBuilder st = new StringBuilder();
st.append(invocationTargetException.getCause().getClass().getTypeName());
if (invocationTargetException.getCause().getMessage() != null) {
st.append(": ").append(invocationTargetException.getCause().getMessage());
}
st.append("\n");
if (alias != null && !alias.isEmpty()) {
st.append("Performed command: ").append(alias).append(" ").append(String.join(" ", args)).append("\n");
}
for (int i = 0; i < stackTraceElements.length - invocationTargetException.getStackTrace().length; i++) {
st.append("\tat ").append(stackTraceElements[i].toString()).append("\n");
}
message = st.toString();
return message;
}
@Override
public void printStackTrace() {
printStackTrace(System.err);
}
@Override
public void printStackTrace(PrintStream s) {
s.print(getBuildStackTrace());
}
@Override
public void printStackTrace(PrintWriter s) {
s.print(getBuildStackTrace());
}
}

Datei anzeigen

@ -0,0 +1,25 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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;
class CommandNoHelpException extends RuntimeException {
CommandNoHelpException() {}
}

Datei anzeigen

@ -0,0 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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;
public class CommandParseException extends RuntimeException {
public CommandParseException() {
}
}

Datei anzeigen

@ -0,0 +1,196 @@
package de.steamwar.command;
import lombok.AllArgsConstructor;
import lombok.Setter;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
class CommandPart<T> {
private static final String[] EMPTY_ARRAY = new String[0];
@AllArgsConstructor
private static class CheckArgumentResult {
private final boolean success;
private final Object value;
}
private AbstractTypeMapper<T, ?> typeMapper;
private AbstractGuardChecker<T> guardChecker;
private Class<?> varArgType;
private String optional;
private GuardCheckType guardCheckType;
private CommandPart<T> next = null;
@Setter
private boolean ignoreAsArgument = false;
public CommandPart(AbstractTypeMapper<T, ?> typeMapper, AbstractGuardChecker<T> guardChecker, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
this.typeMapper = typeMapper;
this.guardChecker = guardChecker;
this.varArgType = varArgType;
this.optional = optional;
this.guardCheckType = guardCheckType;
validatePart();
}
public void setNext(CommandPart<T> next) {
if (varArgType != null) {
throw new IllegalArgumentException("There can't be a next part if this is a vararg part!");
}
this.next = next;
}
private void validatePart() {
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
}
if (optional != null && varArgType != null) {
throw new IllegalArgumentException("A vararg part can't have an optional part!");
}
if (optional != null) {
try {
typeMapper.map(null, EMPTY_ARRAY, optional);
} catch (Exception e) {
throw new IllegalArgumentException("The optional part is not valid!");
}
}
}
public void generateArgumentArray(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);
if (!validArgument.success) {
throw new CommandParseException();
}
Array.set(array, i - startIndex, validArgument.value);
}
current.add(array);
return;
}
CheckArgumentResult validArgument = checkArgument(null, sender, args, startIndex);
if (!validArgument.success && optional == null) {
throw new CommandParseException();
}
if (!validArgument.success) {
if (!ignoreAsArgument) {
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
}
if (next != null) {
next.generateArgumentArray(current, sender, args, startIndex);
}
return;
}
if (!ignoreAsArgument) {
current.add(validArgument.value);
}
if (next != null) {
next.generateArgumentArray(current, sender, args, startIndex + 1);
}
}
public boolean guardCheck(T sender, String[] args, int startIndex) {
if (varArgType != null) {
for (int i = startIndex; i < args.length; i++) {
GuardResult guardResult = checkGuard(guardCheckType, sender, args, i);
if (guardResult == GuardResult.DENIED) {
throw new CommandNoHelpException();
}
if (guardResult == GuardResult.DENIED_WITH_HELP) {
return false;
}
}
return true;
}
GuardResult guardResult = checkGuard(guardCheckType, sender, args, startIndex);
if (guardResult == GuardResult.DENIED) {
if (optional != null && next != null) {
return next.guardCheck(sender, args, startIndex);
}
throw new CommandNoHelpException();
}
if (guardResult == GuardResult.DENIED_WITH_HELP) {
if (optional != null && next != null) {
return next.guardCheck(sender, args, startIndex);
}
return false;
}
if (next != null) {
return next.guardCheck(sender, args, startIndex + 1);
}
return true;
}
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(null, sender, args, i);
if (!validArgument.success) {
return;
}
}
List<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]);
if (strings != null) {
current.addAll(strings);
}
return;
}
if (args.length - 1 > startIndex) {
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, sender, args, startIndex);
if (checkArgumentResult.success && next != null) {
next.generateTabComplete(current, sender, args, startIndex + 1);
return;
}
if (optional != null && next != null) {
next.generateTabComplete(current, sender, args, startIndex);
}
return;
}
List<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]);
if (strings != null) {
current.addAll(strings);
}
if (optional != null && next != null) {
next.generateTabComplete(current, sender, args, startIndex);
}
}
private CheckArgumentResult checkArgument(GuardCheckType guardCheckType, T sender, String[] args, int index) {
try {
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
if (value == null) {
return new CheckArgumentResult(false, null);
}
GuardResult guardResult = checkGuard(guardCheckType, sender, args, index);
switch (guardResult) {
case ALLOWED:
return new CheckArgumentResult(true, value);
case DENIED:
throw new CommandNoHelpException();
case DENIED_WITH_HELP:
default:
return new CheckArgumentResult(false, null);
}
} catch (Exception e) {
return new CheckArgumentResult(false, null);
}
}
private GuardResult checkGuard(GuardCheckType guardCheckType, T sender, String[] args, int index) {
if (guardChecker != null && guardCheckType != null) {
return guardChecker.guard(sender, guardCheckType, Arrays.copyOf(args, index), args[index]);
}
return GuardResult.ALLOWED;
}
}

Datei anzeigen

@ -0,0 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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;
public enum GuardCheckType {
COMMAND,
HELP_COMMAND,
TAB_COMPLETE
}

Datei anzeigen

@ -0,0 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 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;
public enum GuardResult {
ALLOWED,
DENIED_WITH_HELP,
DENIED
}

Datei anzeigen

@ -0,0 +1,241 @@
package de.steamwar.command;
import lombok.Getter;
import lombok.experimental.UtilityClass;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
@UtilityClass
public class SWCommandUtils {
@Getter
private final Map<String, AbstractTypeMapper<?, ?>> MAPPER_FUNCTIONS = new HashMap<>();
@Getter
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
static {
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat)));
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble)));
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt)));
addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong)));
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
}
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, AbstractTypeMapper<?, ?> mapper) {
MAPPER_FUNCTIONS.put(clazz.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) {
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.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);
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);
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 (AbstractTypeMapper<T, ?>) 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) && staticValue.value().length == 2) {
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
return createMapper(s -> {
int index = tabCompletes.indexOf(s);
return index == -1 ? null : index != 0;
}, (commandSender, s) -> tabCompletes);
}
if ((parameter.getType() == int.class || parameter.getType() == Integer.class) && staticValue.value().length >= 2) {
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
return createMapper(s -> {
int index = tabCompletes.indexOf(s);
return index == -1 ? null : index;
}, (commandSender, s) -> tabCompletes);
}
if ((parameter.getType() == long.class || parameter.getType() == Long.class) && staticValue.value().length >= 2) {
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
return createMapper(s -> {
long index = tabCompletes.indexOf(s);
return index == -1 ? null : index;
}, (commandSender, s) -> tabCompletes);
}
}
}
}
AbstractTypeMapper<T, ?> typeMapper = localTypeMapper.getOrDefault(name, (AbstractTypeMapper<T, ?>) MAPPER_FUNCTIONS.getOrDefault(name, null));
if (typeMapper == null) {
throw new IllegalArgumentException("No mapper found for " + name);
}
return typeMapper;
}
public static <T> AbstractGuardChecker<T> getGuardChecker(Parameter parameter, Map<String, AbstractGuardChecker<T>> localGuardChecker) {
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs()) {
clazz = clazz.getComponentType();
}
AbstractSWCommand.ClassGuard classGuard = parameter.getAnnotation(AbstractSWCommand.ClassGuard.class);
if (classGuard != null) {
if (classGuard.value() != null) {
return getGuardChecker(classGuard.value().getTypeName(), localGuardChecker);
}
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
}
AbstractSWCommand.Guard guard = parameter.getAnnotation(AbstractSWCommand.Guard.class);
if (guard != null) {
if (guard.value() != null && !guard.value().isEmpty()) {
return getGuardChecker(guard.value(), localGuardChecker);
}
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
}
return null;
}
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) {
throw new IllegalArgumentException("No guard found for " + s);
}
return guardChecker;
}
public static <K, T> void addMapper(Class<T> clazz, AbstractTypeMapper<K, T> mapper) {
addMapper(clazz.getTypeName(), mapper);
}
public static <T> void addMapper(String name, AbstractTypeMapper<T, ?> mapper) {
MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
}
public static <T> void addGuard(Class<?> clazz, AbstractGuardChecker<T> guardChecker) {
addGuard(clazz.getTypeName(), guardChecker);
}
public static <T> void addGuard(String name, AbstractGuardChecker<T> guardChecker) {
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
}
public static <K> AbstractTypeMapper<K, String> createMapper(String... values) {
List<String> strings = Arrays.asList(values);
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
}
public static <T, K> AbstractTypeMapper<K, T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s));
}
public static <T, K> AbstractTypeMapper<K, T> createMapper(Function<String, T> mapper, BiFunction<K, String, List<String>> tabCompleter) {
return new AbstractTypeMapper<K, T>() {
@Override
public T map(K commandSender, String[] previousArguments, String s) {
return mapper.apply(s);
}
@Override
public List<String> tabCompletes(K commandSender, String[] previous, String s) {
return tabCompleter.apply(commandSender, s);
}
};
}
public static AbstractTypeMapper<?, Enum<?>> createEnumMapper(Class<Enum<?>> enumClass) {
Enum<?>[] enums = enumClass.getEnumConstants();
List<String> strings = Arrays.stream(enums).map(Enum::name).map(String::toLowerCase).collect(Collectors.toList());
return new AbstractTypeMapper<Object, Enum<?>>() {
@Override
public Enum<?> map(Object commandSender, String[] previousArguments, String s) {
for (Enum<?> e : enums) {
if (e.name().equalsIgnoreCase(s)) return e;
}
return null;
}
@Override
public List<String> tabCompletes(Object commandSender, String[] previousArguments, String s) {
return strings;
}
};
}
private static <T> Function<String, T> numberMapper(Function<String, T> mapper) {
return s -> {
if (s.equalsIgnoreCase("nan")) return null;
try {
return mapper.apply(s);
} catch (NumberFormatException e) {
// Ignored
}
try {
return mapper.apply(s.replace(',', '.'));
} catch (NumberFormatException e) {
return null;
}
};
}
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) {
return s -> numberMapper(mapper).apply(s) != null
? Collections.singletonList(s)
: Collections.emptyList();
}
static <T extends Annotation> T[] getAnnotation(Method method, Class<T> annotation) {
if (method.getAnnotations().length != 1) return null;
return method.getDeclaredAnnotationsByType(annotation);
}
}

Datei anzeigen

@ -0,0 +1,99 @@
package de.steamwar.command;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;
public class SubCommand<T> {
private AbstractSWCommand<T> abstractSWCommand;
Method method;
String[] description;
String[] subCommand;
private Predicate<T> senderPredicate;
private Function<T, ?> senderFunction;
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) {
this.abstractSWCommand = abstractSWCommand;
this.method = method;
this.subCommand = subCommand;
this.description = description;
this.noTabComplete = noTabComplete;
Parameter[] parameters = method.getParameters();
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
guardChecker = SWCommandUtils.getGuardChecker(parameters[0], localGuardChecker);
commandPart = SWCommandUtils.generateCommandPart(help, subCommand, parameters, localTypeMapper, localGuardChecker);
senderPredicate = t -> parameters[0].getType().isAssignableFrom(t.getClass());
senderFunction = t -> parameters[0].getType().cast(t);
}
boolean invoke(T sender, String alias, String[] args) {
try {
if (!senderPredicate.test(sender)) {
return false;
}
if (commandPart == null) {
if (args.length != 0) {
return false;
}
method.setAccessible(true);
method.invoke(abstractSWCommand, senderFunction.apply(sender));
} else {
List<Object> objects = new ArrayList<>();
commandPart.generateArgumentArray(objects, sender, args, 0);
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.guardCheck(sender, args, 0);
objects.add(0, senderFunction.apply(sender));
method.setAccessible(true);
method.invoke(abstractSWCommand, objects.toArray());
}
} catch (CommandNoHelpException e) {
throw e;
} catch (CommandParseException e) {
return false;
} catch (InvocationTargetException e) {
throw new CommandFrameworkException(e, alias, args);
} catch (IllegalAccessException | RuntimeException e) {
throw new SecurityException(e.getMessage(), e);
}
return true;
}
List<String> tabComplete(T sender, String[] args) {
if (guardChecker != null && guardChecker.guard(sender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) {
return null;
}
if (commandPart == null) {
return null;
}
List<String> list = new ArrayList<>();
commandPart.generateTabComplete(list, sender, args, 0);
return list;
}
}