diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index f48bf08..ebbcfaf 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -29,6 +29,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.Collectors; class SWCommandUtils { @@ -60,87 +61,33 @@ class SWCommandUtils { }; static { - addMapper(boolean.class, 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"); - } - }); - addMapper(float.class, 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(); - } - } - }); - addMapper(double.class, 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(); - } - } - }); - addMapper(int.class, Integer.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(String.class.getTypeName(), new TypeMapper() { - @Override - public String map(String s) { - return s; - } - - @Override - public List tabCompletes(String s) { + 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(); } - }); - MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), new TypeMapper() { - @Override - public StringBuilder map(String s) { - return new StringBuilder(s); - } - - @Override - public List tabCompletes(String s) { + })); + 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(StringBuilder.class.getTypeName(), createMapper(StringBuilder::new, Collections::singletonList)); MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper() { @Override public Player map(String s) { @@ -234,6 +181,20 @@ class SWCommandUtils { MAPPER_FUNCTIONS.put(name, mapper); } + public static TypeMapper createMapper(Function mapper, Function> tabCompleter) { + return new TypeMapper() { + @Override + public T map(String s) { + return mapper.apply(s); + } + + @Override + public List tabCompletes(String s) { + return tabCompleter.apply(s); + } + }; + } + static T getAnnotation(Method method, Class annotation) { if (method.getAnnotations().length != 1) return null; return method.getAnnotation(annotation);