Add CommandFramework (needs Message System for completion)? #1

Zusammengeführt
Lixfel hat 9 Commits von CommandFramework nach master 2022-05-01 22:42:55 +02:00 zusammengeführt
11 geänderte Dateien mit 254 neuen und 1 gelöschten Zeilen
Nur Änderungen aus Commit af7cd51937 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -62,6 +62,16 @@ sourceSets {
exclude '**/*.java', '**/*.kt'
}
}
test {
java {
srcDirs = ['testsrc']
}
resources {
srcDirs = ['testsrc']
exclude '**/*.java', '**/*.kt'
}
}
}
repositories {
@ -73,6 +83,9 @@ dependencies {
testCompileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
}
task buildProject {

Datei anzeigen

@ -21,7 +21,11 @@ public class SWCommandUtils {
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
static {
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
addMapper(boolean.class, Boolean.class, createMapper(s -> {
if (s.equalsIgnoreCase("true")) return true;
if (s.equalsIgnoreCase("false")) return false;
return null;
}, 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)));

Datei anzeigen

@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

Datei anzeigen

@ -0,0 +1,41 @@
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand;
public class ArgumentCommand extends TestSWCommand {
public ArgumentCommand() {
super("argument");
}
@Register
public void argument(String sender, boolean b, boolean b2) {
throw new ExecutionIdentifier("RunArgument with Boolean");
}
@Register
public void argument(String sender, float f, float f2, float f3) {
throw new ExecutionIdentifier("RunArgument with Float");
}
@Register
public void argument(String sender, double d, double d2) {
throw new ExecutionIdentifier("RunArgument with Double");
}
@Register
public void argument(String sender, int i) {
throw new ExecutionIdentifier("RunArgument with Integer");
}
@Register
public void argument(String sender, long l, long l2) {
throw new ExecutionIdentifier("RunArgument with Long");
}
@Register
public void argument(String sender, String arg) {
throw new ExecutionIdentifier("RunArgument with String");
}
}

Datei anzeigen

@ -0,0 +1,87 @@
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class ArgumentCommandTest {
@Test
public void testNoArgs() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[0]);
} catch (Exception e) {
throw new AssertionError("No exception expected");
}
}
@Test
public void testBoolean() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"true", "false"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Boolean"));
}
}
@Test
public void testFloat() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"0.0", "0.0", "0.0"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Float"));
}
}
@Test
public void testDouble() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"0.0", "0.0"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Double"));
}
}
@Test
public void testInt() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"0"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Integer"));
}
}
@Test
public void testLong() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"0", "0"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Long"));
}
}
@Test
public void testString() {
ArgumentCommand cmd = new ArgumentCommand();
try {
cmd.execute("test", "", new String[]{"Hello World"});
} catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with String"));
}
}
}

Datei anzeigen

@ -0,0 +1,16 @@
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand;
public class SimpleCommand extends TestSWCommand {
public SimpleCommand() {
super("simple");
}
@Register
public void simple(String s) {
throw new ExecutionIdentifier("RunSimple with noArgs");
}
}

Datei anzeigen

@ -0,0 +1,32 @@
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class SimpleCommandTest {
@Test
public void testSimpleParsing() {
SimpleCommand cmd = new SimpleCommand();
try {
cmd.execute("test", "", new String[0]);
} catch (CommandFrameworkException e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
assertThat(e.getCause().getCause().getMessage(), is("RunSimple with noArgs"));
}
}
@Test
public void testSimpleParsingNoResult() {
SimpleCommand cmd = new SimpleCommand();
try {
cmd.execute("test", "", new String[]{"Hello"});
} catch (CommandFrameworkException e) {
throw new AssertionError("No exception expected");
}
}
}

Datei anzeigen

@ -0,0 +1,7 @@
package de.steamwar.command.dto;
public class ExecutionIdentifier extends RuntimeException {
public ExecutionIdentifier(String message) {
super(message);
}
}

Datei anzeigen

@ -0,0 +1,6 @@
package de.steamwar.command.dto;
import de.steamwar.command.AbstractGuardChecker;
public interface TestGuardChecker extends AbstractGuardChecker<String> {
}

Datei anzeigen

@ -0,0 +1,40 @@
package de.steamwar.command.dto;
import de.steamwar.command.AbstractSWCommand;
import de.steamwar.command.CommandFrameworkException;
import java.util.function.Supplier;
public class TestSWCommand extends AbstractSWCommand<String> {
protected TestSWCommand(String command) {
super(String.class, command);
}
protected TestSWCommand(String command, String[] aliases) {
super(String.class, command, aliases);
}
@Override
protected void createAndSafeCommand(String command, String[] aliases) {
}
@Override
public void unregister() {
}
@Override
public void register() {
}
@Override
protected void commandSystemError(String sender, CommandFrameworkException e) {
}
@Override
protected void commandSystemWarning(Supplier<String> message) {
}
}

Datei anzeigen

@ -0,0 +1,6 @@
package de.steamwar.command.dto;
import de.steamwar.command.AbstractTypeMapper;
public interface TestTypeChecker<T> extends AbstractTypeMapper<String, T> {
}