SteamWar/SpigotCore
Archiviert
13
0

Optimize SWCommandUtils

Dieser Commit ist enthalten in:
yoyosource 2021-03-26 08:44:41 +01:00
Ursprung 9b01cbf7bf
Commit abc48d9215

Datei anzeigen

@ -32,7 +32,7 @@ import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
class SWCommandUtils { public class SWCommandUtils {
private SWCommandUtils() { private SWCommandUtils() {
throw new IllegalStateException("Utility Class"); throw new IllegalStateException("Utility Class");
@ -62,30 +62,9 @@ class SWCommandUtils {
static { static {
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false"))); addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
addMapper(float.class, Float.class, createMapper(Float::parseFloat, s -> { addMapper(float.class, Float.class, createMapper(Float::parseFloat, numberCompleter(Float::parseFloat)));
try { addMapper(double.class, Double.class, createMapper(Double::parseDouble, numberCompleter(Double::parseDouble)));
Float.parseFloat(s); addMapper(int.class, Integer.class, createMapper(Integer::parseInt, numberCompleter(Integer::parseInt)));
return Collections.singletonList(s);
} catch (NumberFormatException e) {
return Collections.emptyList();
}
}));
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(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), createMapper(StringBuilder::new, Collections::singletonList)); MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), createMapper(StringBuilder::new, Collections::singletonList));
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), createMapper(Bukkit::getPlayer, s -> { MAPPER_FUNCTIONS.put(Player.class.getTypeName(), createMapper(Bukkit::getPlayer, s -> {
@ -127,7 +106,7 @@ class SWCommandUtils {
} }
} }
public static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, boolean varArgs, String[] subCommand) { static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, boolean varArgs, String[] subCommand) {
Object[] arguments = new Object[parameters.length + 1]; Object[] arguments = new Object[parameters.length + 1];
int index = 0; int index = 0;
while (index < subCommand.length) { while (index < subCommand.length) {
@ -174,6 +153,17 @@ class SWCommandUtils {
}; };
} }
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) {
return s -> {
try {
mapper.apply(s);
return Collections.singletonList(s);
} catch (Exception e) {
return Collections.emptyList();
}
};
}
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);