From 9c7cea96d4a461b05c3b512da1a2585d5922853a Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 29 Mar 2021 22:28:33 +0200 Subject: [PATCH] Add SWCommand.LocalMapper only defined in current class scope overrides any global mapper for this class only --- .../src/de/steamwar/command/SWCommand.java | 19 +++++++++++++++++-- .../src/de/steamwar/command/SubCommand.java | 15 +++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 3b4b6e7..ac3f65d 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,6 +36,7 @@ public abstract class SWCommand { private boolean enabled = true; private final Set commandSet = new HashSet<>(); private final Set commandHelpSet = new HashSet<>(); + private final Map> localTypeMapper = new HashMap<>(); protected SWCommand(String command) { this(command, new String[0]); @@ -64,7 +65,7 @@ public abstract class SWCommand { if (!enabled) return Collections.emptyList(); List strings = new ArrayList<>(); for (SubCommand subCommand : commandSet) { - List tabCompletes = subCommand.tabComplete(sender, args); + List tabCompletes = subCommand.tabComplete(args); if (tabCompletes != null) { strings.addAll(tabCompletes); } @@ -98,6 +99,15 @@ public abstract class SWCommand { throw new SecurityException(e.getMessage(), e); } }); + 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); + } + }); add(RegisterHelp.class, method, i -> i != 1, true, null, (anno, parameters) -> { if (!parameters[parameters.length - 1].isVarArgs()) { return; @@ -176,11 +186,16 @@ public abstract class SWCommand { String value(); } - @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) protected @interface ClassMapper { Class value(); } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.METHOD}) + protected @interface LocalMapper { + String value(); + } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index ce98811..307b36e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -37,6 +37,10 @@ class SubCommand { private Function commandSenderFunction; public SubCommand(SWCommand swCommand, Method method, String[] subCommand) { + this(swCommand, method, subCommand, new HashMap<>()); + } + + public SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper) { this.swCommand = swCommand; this.method = method; @@ -54,7 +58,7 @@ class SubCommand { } SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); - if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { + if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) { Class> enumClass = (Class>) clazz; List tabCompletes = new ArrayList<>(); for (Enum enumConstant : enumClass.getEnumConstants()) { @@ -68,8 +72,11 @@ class SubCommand { if (mapper != null) { name = mapper.value(); } - - arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); + if (localTypeMapper.containsKey(name)) { + arguments[i - 1] = localTypeMapper.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); + } else { + arguments[i - 1] = SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION); + } } } @@ -93,7 +100,7 @@ class SubCommand { return true; } - List tabComplete(CommandSender commandSender, String[] args) { + List tabComplete(String[] args) { if (!varArgs && args.length < arguments.length - 1) { return Collections.emptyList(); }