diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 3c3f9b1..d2fbc45 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -32,7 +32,7 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; -class SWCommandUtils { +public class SWCommandUtils { private SWCommandUtils() { throw new IllegalStateException("Utility Class"); @@ -62,30 +62,9 @@ class SWCommandUtils { static { 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(); - } - })); - 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(); - } - })); + addMapper(float.class, Float.class, createMapper(Float::parseFloat, numberCompleter(Float::parseFloat))); + addMapper(double.class, Double.class, createMapper(Double::parseDouble, numberCompleter(Double::parseDouble))); + addMapper(int.class, Integer.class, createMapper(Integer::parseInt, numberCompleter(Integer::parseInt))); 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(), 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]; int index = 0; while (index < subCommand.length) { @@ -174,6 +153,17 @@ class SWCommandUtils { }; } + private static Function> numberCompleter(Function mapper) { + return s -> { + try { + mapper.apply(s); + return Collections.singletonList(s); + } catch (Exception e) { + return Collections.emptyList(); + } + }; + } + static T getAnnotation(Method method, Class annotation) { if (method.getAnnotations().length != 1) return null; return method.getAnnotation(annotation);