Merge branch 'master' into network
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
YoyoNow 2022-06-07 20:19:31 +02:00
Commit cbc7750b4a
7 geänderte Dateien mit 102 neuen und 66 gelöschten Zeilen

Datei anzeigen

@ -90,18 +90,25 @@ task buildProject {
dependsOn build dependsOn build
} }
task finalizeProject { task testOutput {
description 'Finalize this project' description 'Print Test output'
group "Steamwar" group "Build"
doLast { doLast {
if ("${buildDir}" == null) { for (def file : new File("${buildDir}/test-results/test/").listFiles()) {
return 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) test.finalizedBy(testOutput)

Datei anzeigen

@ -43,7 +43,7 @@ public abstract class AbstractSWCommand<T> {
this(clazz, command, new String[0]); this(clazz, command, new String[0]);
} }
protected AbstractSWCommand(Class<T> clazz, String command, String[] aliases) { protected AbstractSWCommand(Class<T> clazz, String command, String... aliases) {
this.clazz = clazz; this.clazz = clazz;
createAndSafeCommand(command, aliases); createAndSafeCommand(command, aliases);
unregister(); unregister();
@ -127,7 +127,8 @@ public abstract class AbstractSWCommand<T> {
SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker); SWCommandUtils.getGUARD_FUNCTIONS().putIfAbsent(anno.value().getTypeName(), guardChecker);
} }
}); });
}
for (Method method : methods) {
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (!anno.help()) return; if (!anno.help()) return;
if (parameters.length != 2) { if (parameters.length != 2) {
@ -140,10 +141,9 @@ 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<>(), localGuardChecker, true, null, anno.noTabComplete()));
}); });
}
for (Method method : methods) {
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
if (anno.help()) return; if (anno.help()) return;
for (int i = 1; i < parameters.length; i++) { for (int i = 1; i < parameters.length; i++) {
@ -162,27 +162,27 @@ 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, 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);
}
}); });
} }
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; initialized = true;
} }
@ -199,7 +199,7 @@ public abstract class AbstractSWCommand<T> {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + clazz.getTypeName() + "'");
return; return;
} }
if (returnType != null && !method.getReturnType().isAssignableFrom(returnType)) { if (returnType != null && !returnType.isAssignableFrom(method.getReturnType())) {
commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); commandSystemWarning(() -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
return; return;
} }
@ -238,7 +238,7 @@ public abstract class AbstractSWCommand<T> {
private List<Method> methods() { private List<Method> methods() {
List<Method> methods = new ArrayList<>(); List<Method> methods = new ArrayList<>();
Class<?> current = getClass(); Class<?> current = getClass();
while (current.getSuperclass() != AbstractSWCommand.class) { while (current != AbstractSWCommand.class) {
methods.addAll(Arrays.asList(current.getDeclaredMethods())); methods.addAll(Arrays.asList(current.getDeclaredMethods()));
current = current.getSuperclass(); current = current.getSuperclass();
} }

Datei anzeigen

@ -38,6 +38,18 @@ public class SWCommandUtils {
@Getter @Getter
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>(); private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
private SWTypeMapperCreator swTypeMapperCreator = (mapper, tabCompleter) -> new AbstractTypeMapper<Object, Object>() {
@Override
public Object map(Object sender, String[] previousArguments, String s) {
return mapper.apply(s);
}
@Override
public Collection<String> tabCompletes(Object sender, String[] previousArguments, String s) {
return ((BiFunction<Object, Object, Collection<String>>) tabCompleter).apply(sender, s);
}
};
static { static {
addMapper(boolean.class, Boolean.class, createMapper(s -> { addMapper(boolean.class, Boolean.class, createMapper(s -> {
if (s.equalsIgnoreCase("true")) return true; if (s.equalsIgnoreCase("true")) return true;
@ -51,6 +63,10 @@ public class SWCommandUtils {
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
} }
public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) {
SWCommandUtils.swTypeMapperCreator = swTypeMapperCreator;
}
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, AbstractTypeMapper<?, ?> mapper) { private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, AbstractTypeMapper<?, ?> mapper) {
MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper); MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper);
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper); MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
@ -192,45 +208,25 @@ public class SWCommandUtils {
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker); GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
} }
public static <K> AbstractTypeMapper<K, String> createMapper(String... values) { public static <T extends AbstractTypeMapper<K, String>, K> T createMapper(String... values) {
List<String> strings = Arrays.asList(values); List<String> strings = Arrays.asList(values);
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings); 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) { public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s)); 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) { public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, BiFunction<K, String, Collection<String>> tabCompleter) {
return new AbstractTypeMapper<K, T>() { return (T) swTypeMapperCreator.createTypeMapper(mapper, tabCompleter);
@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) { public static <T extends AbstractTypeMapper<K, Enum<?>>, K> T createEnumMapper(Class<Enum<?>> enumClass) {
Map<String, Enum<?>> enumMap = new HashMap<>(); Map<String, Enum<?>> enumMap = new HashMap<>();
for (Enum<?> e : enumClass.getEnumConstants()) { for (Enum<?> e : enumClass.getEnumConstants()) {
enumMap.put(e.name(), e); enumMap.put(e.name().toLowerCase(), e);
} }
return new AbstractTypeMapper<Object, Enum<?>>() { return createMapper(s -> enumMap.get(s.toLowerCase()), (k, s) -> enumMap.keySet());
@Override
public Enum<?> map(Object commandSender, String[] previousArguments, String s) {
return enumMap.get(s);
}
@Override
public Collection<String> tabCompletes(Object commandSender, String[] previousArguments, String s) {
return enumMap.keySet();
}
};
} }
private static <T> Function<String, T> numberMapper(Function<String, T> mapper) { private static <T> Function<String, T> numberMapper(Function<String, T> mapper) {
@ -249,7 +245,7 @@ public class SWCommandUtils {
}; };
} }
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) { private static Function<String, Collection<String>> numberCompleter(Function<String, ?> mapper) {
return s -> numberMapper(mapper).apply(s) != null return s -> numberMapper(mapper).apply(s) != null
? Collections.singletonList(s) ? Collections.singletonList(s)
: Collections.emptyList(); : Collections.emptyList();

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public interface SWTypeMapperCreator<T extends AbstractTypeMapper<K, V>, K, V> {
T createTypeMapper(Function<String, V> mapper, BiFunction<K, String, List<String>> tabCompleter);
}

5
steamwarci.yml Normale Datei
Datei anzeigen

@ -0,0 +1,5 @@
build:
- "ln -s /home/gitea/lib"
- "cp ~/gradle.properties ."
- "chmod u+x build.gradle"
- "./gradlew buildProject"

Datei anzeigen

@ -39,7 +39,7 @@ public class ArgumentCommand extends TestSWCommand {
} }
@Register @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"); throw new ExecutionIdentifier("RunArgument with Double");
} }

Datei anzeigen

@ -63,7 +63,7 @@ public class ArgumentCommandTest {
public void testDouble() { public void testDouble() {
ArgumentCommand cmd = new ArgumentCommand(); ArgumentCommand cmd = new ArgumentCommand();
try { 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) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Double"); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Double");
} }