diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index ac3f65d..47b4253 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -81,32 +81,14 @@ public abstract class SWCommand { }); for (Method method : getClass().getDeclaredMethods()) { - add(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { - try { - method.setAccessible(true); - Object object = method.invoke(this); - SWCommandUtils.addMapper(anno.value(), (TypeMapper) object); - } catch (Exception e) { - throw new SecurityException(e.getMessage(), e); - } + addMapper(Mapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> { + SWCommandUtils.addMapper(anno.value(), typeMapper); }); - add(ClassMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { - try { - method.setAccessible(true); - Object object = method.invoke(this); - SWCommandUtils.addMapper(anno.value().getTypeName(), (TypeMapper) object); - } catch (Exception e) { - throw new SecurityException(e.getMessage(), e); - } + addMapper(ClassMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> { + SWCommandUtils.addMapper(anno.value().getTypeName(), typeMapper); }); - add(LocalMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, parameters) -> { - try { - method.setAccessible(true); - Object object = method.invoke(this); - localTypeMapper.put(anno.value(), (TypeMapper) object); - } catch (Exception e) { - throw new SecurityException(e.getMessage(), e); - } + addMapper(LocalMapper.class, method, i -> i != 0, false, TypeMapper.class, (anno, typeMapper) -> { + localTypeMapper.put(anno.value(), typeMapper); }); add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { if (!parameters[parameters.length - 1].isVarArgs()) { @@ -160,6 +142,18 @@ public abstract class SWCommand { consumer.accept(anno, parameters); } + private void addMapper(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer> consumer) { + add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> { + try { + method.setAccessible(true); + Object object = method.invoke(this); + consumer.accept(anno, (TypeMapper) object); + } catch (Exception e) { + throw new SecurityException(e.getMessage(), e); + } + }); + } + protected boolean isEnabled() { return enabled; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 93f6ec0..deb4523 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -116,6 +116,10 @@ public class SWCommandUtils { return arguments; } + public static void addMapper(Class clazz, TypeMapper mapper) { + addMapper(clazz.getTypeName(), mapper); + } + public static void addMapper(String name, TypeMapper mapper) { if (MAPPER_FUNCTIONS.containsKey(name)) return; MAPPER_FUNCTIONS.put(name, mapper);