From 03bbdcfe29b8a405f017ad495bfe829efbff81d3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 13 Mar 2021 16:02:44 +0100 Subject: [PATCH] Add SWCommandUtils.addMapper --- .../de/steamwar/command/SWCommandUtils.java | 72 +++---------------- 1 file changed, 9 insertions(+), 63 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index de7c1d2..2ee5b0b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -61,7 +61,7 @@ class SWCommandUtils { }; static { - MAPPER_FUNCTIONS.put(boolean.class, new TypeMapper() { + addMapper(boolean.class, Boolean.class, new TypeMapper() { @Override public Boolean map(String s) { return Boolean.parseBoolean(s); @@ -72,18 +72,7 @@ class SWCommandUtils { return Arrays.asList("true", "false"); } }); - MAPPER_FUNCTIONS.put(Boolean.class, new TypeMapper() { - @Override - public Boolean map(String s) { - return Boolean.parseBoolean(s); - } - - @Override - public List tabCompletes(String s) { - return Arrays.asList("true", "false"); - } - }); - MAPPER_FUNCTIONS.put(float.class, new TypeMapper() { + addMapper(float.class, Float.class, new TypeMapper() { @Override public Float map(String s) { return Float.parseFloat(s); @@ -99,23 +88,7 @@ class SWCommandUtils { } } }); - MAPPER_FUNCTIONS.put(Float.class, new TypeMapper() { - @Override - public Float map(String s) { - return Float.parseFloat(s); - } - - @Override - public List tabCompletes(String s) { - try { - Float.parseFloat(s); - return Collections.singletonList(s); - } catch (NumberFormatException e) { - return Collections.emptyList(); - } - } - }); - MAPPER_FUNCTIONS.put(double.class, new TypeMapper() { + addMapper(double.class, Double.class, new TypeMapper() { @Override public Double map(String s) { return Double.parseDouble(s); @@ -131,39 +104,7 @@ class SWCommandUtils { } } }); - MAPPER_FUNCTIONS.put(Double.class, new TypeMapper() { - @Override - public Double map(String s) { - return Double.parseDouble(s); - } - - @Override - public List tabCompletes(String s) { - try { - Double.parseDouble(s); - return Collections.singletonList(s); - } catch (NumberFormatException e) { - return Collections.emptyList(); - } - } - }); - MAPPER_FUNCTIONS.put(int.class, new TypeMapper() { - @Override - public Integer map(String s) { - return Integer.parseInt(s); - } - - @Override - public List tabCompletes(String s) { - try { - Integer.parseInt(s); - return Collections.singletonList(s); - } catch (NumberFormatException e) { - return Collections.emptyList(); - } - } - }); - MAPPER_FUNCTIONS.put(Integer.class, new TypeMapper() { + addMapper(int.class, Integer.class, new TypeMapper() { @Override public Integer map(String s) { return Integer.parseInt(s); @@ -214,6 +155,11 @@ class SWCommandUtils { }); } + private static void addMapper(Class clazz, Class alternativeClazz, TypeMapper mapper) { + MAPPER_FUNCTIONS.put(clazz, mapper); + MAPPER_FUNCTIONS.put(alternativeClazz, mapper); + } + static final CommandMap commandMap; static {