From a35c42faa2cd3d66e51bb24dd6f60e96aa529a6b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 2 Apr 2021 17:29:28 +0200 Subject: [PATCH] Fix SWCommandUtils for Number mapping --- .../de/steamwar/command/SWCommandUtils.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 1536d51..bdd965e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -58,9 +58,9 @@ public class SWCommandUtils { static { addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false"))); - 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))); + 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))); MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList)); MAPPER_FUNCTIONS.put(Player.class.getTypeName(), createMapper(Bukkit::getPlayer, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()))); MAPPER_FUNCTIONS.put(GameMode.class.getTypeName(), createMapper(s -> { @@ -69,7 +69,7 @@ public class SWCommandUtils { if (s.equals("c") || s.equals("creative") || s.equals("1")) return GameMode.CREATIVE; if (s.equals("sp") || s.equals("spectator") || s.equals("3")) return GameMode.SPECTATOR; if (s.equals("a") || s.equals("adventure") || s.equals("2")) return GameMode.ADVENTURE; - throw new SecurityException(); + return null; }, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "specator", "3", "a", "adventure", "2"))); } @@ -167,6 +167,16 @@ public class SWCommandUtils { }; } + private static Function numberMapper(Function mapper) { + return s -> { + try { + return mapper.apply(s); + } catch (NumberFormatException e) { + return null; + } + }; + } + private static Function> numberCompleter(Function mapper) { return s -> { try {