From 529b3daf6c7df06e2819db347a3e819594610be6 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 29 Mar 2021 22:35:31 +0200 Subject: [PATCH] Simplify SWCommand Add SWCommandUtils.addMapper with class --- .../src/de/steamwar/command/SWCommand.java | 42 ++++++++----------- .../de/steamwar/command/SWCommandUtils.java | 4 ++ 2 files changed, 22 insertions(+), 24 deletions(-) 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);