diff --git a/build.gradle b/build.gradle index 376b05d..41ba86c 100644 --- a/build.gradle +++ b/build.gradle @@ -90,18 +90,25 @@ task buildProject { dependsOn build } -task finalizeProject { - description 'Finalize this project' - group "Steamwar" +task testOutput { + description 'Print Test output' + group "Build" doLast { - if ("${buildDir}" == null) { - return + for (def file : new File("${buildDir}/test-results/test/").listFiles()) { + if (file.isDirectory()) { + continue + } + def strings = file.readLines() + if (strings.get(1).contains("failures=\"0\"")) { + continue + } + println "Content ${file}:" + strings.each { + line -> println ": $line" + } + println "" } - delete fileTree("${libs}").matching { - exclude("${uberJarName}") - } - file(libs + "/" + uberJarName).renameTo(file(libs + "/" + jarName)) } } -build.finalizedBy(finalizeProject) \ No newline at end of file +test.finalizedBy(testOutput) \ No newline at end of file diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index fd371df..d654df7 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -43,7 +43,7 @@ public abstract class AbstractSWCommand { this(clazz, command, new String[0]); } - protected AbstractSWCommand(Class clazz, String command, String[] aliases) { + protected AbstractSWCommand(Class clazz, String command, String... aliases) { this.clazz = clazz; createAndSafeCommand(command, aliases); unregister(); @@ -127,7 +127,8 @@ public abstract class AbstractSWCommand { SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker); } }); - + } + for (Method method : methods) { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { if (!anno.help()) return; if (parameters.length != 2) { @@ -140,10 +141,9 @@ public abstract class AbstractSWCommand { 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<>(), 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++) { @@ -162,27 +162,27 @@ public abstract class AbstractSWCommand { 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); - } + 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; } @@ -199,7 +199,7 @@ public abstract class AbstractSWCommand { commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'"); return; } - if (returnType != null && !method.getReturnType().isAssignableFrom(returnType)) { + if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) { commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); return; } @@ -238,7 +238,7 @@ public abstract class AbstractSWCommand { private List methods() { List methods = new ArrayList<>(); Class current = getClass(); - while (current.getSuperclass() != AbstractSWCommand.class) { + while (current != AbstractSWCommand.class) { methods.addAll(Arrays.asList(current.getDeclaredMethods())); current = current.getSuperclass(); } diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 067bc35..aaf0c7a 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -38,6 +38,18 @@ public class SWCommandUtils { @Getter private final Map> GUARD_FUNCTIONS = new HashMap<>(); + private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper() { + @Override + public Object map(Object sender, String[] previousArguments, String s) { + return mapper.apply(s); + } + + @Override + public Collection tabCompletes(Object sender, String[] previousArguments, String s) { + return ((BiFunction>) tabCompleter).apply(sender, s); + } + }; + static { addMapper(boolean.class, Boolean.class, createMapper(s -> { if (s.equalsIgnoreCase("true")) return true; @@ -51,6 +63,10 @@ public class SWCommandUtils { MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); } + public static , K, V> void init(SWTypeMapperCreator swTypeMapperCreator) { + SWCommandUtils.swTypeMapperCreator = swTypeMapperCreator; + } + private static void addMapper(Class clazz, Class alternativeClazz, AbstractTypeMapper mapper) { MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper); MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper); @@ -192,45 +208,25 @@ public class SWCommandUtils { GUARD_FUNCTIONS.putIfAbsent(name, guardChecker); } - public static AbstractTypeMapper createMapper(String... values) { + public static , K> T createMapper(String... values) { List strings = Arrays.asList(values); return createMapper((s) -> strings.contains(s) ? s : null, s -> strings); } - public static AbstractTypeMapper createMapper(Function mapper, Function> tabCompleter) { + public static , K, V> T createMapper(Function mapper, Function> tabCompleter) { return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s)); } - public static AbstractTypeMapper createMapper(Function mapper, BiFunction> tabCompleter) { - return new AbstractTypeMapper() { - @Override - public T map(K commandSender, String[] previousArguments, String s) { - return mapper.apply(s); - } - - @Override - public List tabCompletes(K commandSender, String[] previous, String s) { - return tabCompleter.apply(commandSender, s); - } - }; + public static , K, V> T createMapper(Function mapper, BiFunction> tabCompleter) { + return (T) swTypeMapperCreator.createTypeMapper(mapper, tabCompleter); } - public static AbstractTypeMapper> createEnumMapper(Class> enumClass) { + public static >, K> T createEnumMapper(Class> enumClass) { Map> enumMap = new HashMap<>(); for (Enum e : enumClass.getEnumConstants()) { - enumMap.put(e.name(), e); + enumMap.put(e.name().toLowerCase(), e); } - return new AbstractTypeMapper>() { - @Override - public Enum map(Object commandSender, String[] previousArguments, String s) { - return enumMap.get(s); - } - - @Override - public Collection tabCompletes(Object commandSender, String[] previousArguments, String s) { - return enumMap.keySet(); - } - }; + return createMapper(s -> enumMap.get(s.toLowerCase()), (k, s) -> enumMap.keySet()); } private static Function numberMapper(Function mapper) { @@ -249,7 +245,7 @@ public class SWCommandUtils { }; } - private static Function> numberCompleter(Function mapper) { + private static Function> numberCompleter(Function mapper) { return s -> numberMapper(mapper).apply(s) != null ? Collections.singletonList(s) : Collections.emptyList(); diff --git a/src/de/steamwar/command/SWTypeMapperCreator.java b/src/de/steamwar/command/SWTypeMapperCreator.java new file mode 100644 index 0000000..33c5dcb --- /dev/null +++ b/src/de/steamwar/command/SWTypeMapperCreator.java @@ -0,0 +1,28 @@ +/* + * 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 . + */ + +package de.steamwar.command; + +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; + +public interface SWTypeMapperCreator, K, V> { + T createTypeMapper(Function mapper, BiFunction> tabCompleter); +} diff --git a/steamwarci.yml b/steamwarci.yml new file mode 100644 index 0000000..73f8039 --- /dev/null +++ b/steamwarci.yml @@ -0,0 +1,5 @@ +build: + - "ln -s /home/gitea/lib" + - "cp ~/gradle.properties ." + - "chmod u+x build.gradle" + - "./gradlew buildProject" diff --git a/testsrc/de/steamwar/command/ArgumentCommand.java b/testsrc/de/steamwar/command/ArgumentCommand.java index b4ce500..a107270 100644 --- a/testsrc/de/steamwar/command/ArgumentCommand.java +++ b/testsrc/de/steamwar/command/ArgumentCommand.java @@ -39,7 +39,7 @@ public class ArgumentCommand extends TestSWCommand { } @Register - public void argument(String sender, double d, double d2) { + public void argument(String sender, double d, double d2, double d3, double d4) { throw new ExecutionIdentifier("RunArgument with Double"); } diff --git a/testsrc/de/steamwar/command/ArgumentCommandTest.java b/testsrc/de/steamwar/command/ArgumentCommandTest.java index c546127..e57edd0 100644 --- a/testsrc/de/steamwar/command/ArgumentCommandTest.java +++ b/testsrc/de/steamwar/command/ArgumentCommandTest.java @@ -63,7 +63,7 @@ public class ArgumentCommandTest { public void testDouble() { ArgumentCommand cmd = new ArgumentCommand(); try { - cmd.execute("test", "", new String[]{"0.0", "0.0"}); + cmd.execute("test", "", new String[]{"0.0", "0.0", "0.0", "0.0"}); } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Double"); }