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.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class SWCommandUtils {
|
||||
@ -60,87 +61,33 @@ class SWCommandUtils {
|
||||
};
|
||||
|
||||
static {
|
||||
addMapper(boolean.class, Boolean.class, new TypeMapper<Boolean>() {
|
||||
@Override
|
||||
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 {
|
||||
Float.parseFloat(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
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 {
|
||||
Double.parseDouble(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
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 {
|
||||
Integer.parseInt(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(String.class.getTypeName(), new TypeMapper<String>() {
|
||||
@Override
|
||||
public String map(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
|
||||
addMapper(float.class, Float.class, createMapper(Float::parseFloat, s -> {
|
||||
try {
|
||||
Float.parseFloat(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
});
|
||||
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) {
|
||||
}));
|
||||
addMapper(double.class, Double.class, createMapper(Double::parseDouble, s -> {
|
||||
try {
|
||||
Double.parseDouble(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
});
|
||||
}));
|
||||
addMapper(int.class, Integer.class, createMapper(Integer::parseInt, s -> {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}));
|
||||
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
|
||||
MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), createMapper(StringBuilder::new, Collections::singletonList));
|
||||
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper<Player>() {
|
||||
@Override
|
||||
public Player map(String s) {
|
||||
@ -234,6 +181,20 @@ class SWCommandUtils {
|
||||
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) {
|
||||
if (method.getAnnotations().length != 1) return null;
|
||||
return method.getAnnotation(annotation);
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren