From 8d1b5c5f6658bd617bf6bf68b4994144741dc99d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 18 Dec 2022 13:31:33 +0100 Subject: [PATCH] Update number type mapper --- src/de/steamwar/command/SWCommandUtils.java | 31 +++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 32c46c4..b31ed20 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -58,10 +58,10 @@ public class SWCommandUtils { 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))); - addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong))); + addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat, true))); + addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble, true))); + addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt, false))); + addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong, false))); MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); } @@ -255,10 +255,25 @@ public class SWCommandUtils { }; } - private static Function> numberCompleter(Function mapper) { - return s -> numberMapper(mapper).apply(s) != null - ? Collections.singletonList(s) - : Collections.emptyList(); + private static Function> numberCompleter(Function mapper, boolean comma) { + return s -> { + if (numberMapper(mapper).apply(s) == null) { + return Collections.emptyList(); + } + List strings = new ArrayList<>(); + if (s.length() == 0) { + strings.add("-"); + } else { + strings.add(s); + } + for (int i = 0; i < 10; i++) { + strings.add(s + i); + } + if (comma && (!s.contains(".") || !s.contains(","))) { + strings.add(s + "."); + } + return strings; + }; } static T[] getAnnotation(Method method, Class annotation) {