Add SWCommandUtils.createMapper
Dieser Commit ist enthalten in:
Ursprung
4bedbe2275
Commit
a45a83e2d5
@ -29,6 +29,7 @@ import java.lang.reflect.Field;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
class SWCommandUtils {
|
class SWCommandUtils {
|
||||||
@ -60,87 +61,33 @@ class SWCommandUtils {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static {
|
static {
|
||||||
addMapper(boolean.class, Boolean.class, new TypeMapper<Boolean>() {
|
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
|
||||||
@Override
|
addMapper(float.class, Float.class, createMapper(Float::parseFloat, s -> {
|
||||||
public Boolean map(String s) {
|
|
||||||
return Boolean.parseBoolean(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
return Arrays.asList("true", "false");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
addMapper(float.class, Float.class, new TypeMapper<Float>() {
|
|
||||||
@Override
|
|
||||||
public Float map(String s) {
|
|
||||||
return Float.parseFloat(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
try {
|
try {
|
||||||
Float.parseFloat(s);
|
Float.parseFloat(s);
|
||||||
return Collections.singletonList(s);
|
return Collections.singletonList(s);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
});
|
addMapper(double.class, Double.class, createMapper(Double::parseDouble, s -> {
|
||||||
addMapper(double.class, Double.class, new TypeMapper<Double>() {
|
|
||||||
@Override
|
|
||||||
public Double map(String s) {
|
|
||||||
return Double.parseDouble(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
try {
|
try {
|
||||||
Double.parseDouble(s);
|
Double.parseDouble(s);
|
||||||
return Collections.singletonList(s);
|
return Collections.singletonList(s);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
});
|
addMapper(int.class, Integer.class, createMapper(Integer::parseInt, s -> {
|
||||||
addMapper(int.class, Integer.class, new TypeMapper<Integer>() {
|
|
||||||
@Override
|
|
||||||
public Integer map(String s) {
|
|
||||||
return Integer.parseInt(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
try {
|
try {
|
||||||
Integer.parseInt(s);
|
Integer.parseInt(s);
|
||||||
return Collections.singletonList(s);
|
return Collections.singletonList(s);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}));
|
||||||
});
|
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
|
||||||
MAPPER_FUNCTIONS.put(String.class.getTypeName(), new TypeMapper<String>() {
|
MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), createMapper(StringBuilder::new, Collections::singletonList));
|
||||||
@Override
|
|
||||||
public String map(String s) {
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
return Collections.singletonList(s);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), new TypeMapper<StringBuilder>() {
|
|
||||||
@Override
|
|
||||||
public StringBuilder map(String s) {
|
|
||||||
return new StringBuilder(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(String s) {
|
|
||||||
return Collections.singletonList(s);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper<Player>() {
|
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper<Player>() {
|
||||||
@Override
|
@Override
|
||||||
public Player map(String s) {
|
public Player map(String s) {
|
||||||
@ -234,6 +181,20 @@ class SWCommandUtils {
|
|||||||
MAPPER_FUNCTIONS.put(name, mapper);
|
MAPPER_FUNCTIONS.put(name, mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {
|
||||||
|
return new TypeMapper<T>() {
|
||||||
|
@Override
|
||||||
|
public T map(String s) {
|
||||||
|
return mapper.apply(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> tabCompletes(String s) {
|
||||||
|
return tabCompleter.apply(s);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
static <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
|
static <T extends Annotation> T getAnnotation(Method method, Class<T> annotation) {
|
||||||
if (method.getAnnotations().length != 1) return null;
|
if (method.getAnnotations().length != 1) return null;
|
||||||
return method.getAnnotation(annotation);
|
return method.getAnnotation(annotation);
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren