From 58cc8eb2c6e0ea603895c1b02c17fde606e10362 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 3 Dec 2021 18:15:42 +0100 Subject: [PATCH 1/3] Add SWCommand.StaticValue --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 6 ++++++ .../src/de/steamwar/command/SWCommandUtils.java | 4 ++++ SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 258ab5c..177db88 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -325,4 +325,10 @@ public abstract class SWCommand { boolean local() default false; } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + protected @interface StaticValue { + String value(); + } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 52e362e..6efcc02 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -151,6 +151,10 @@ public class SWCommandUtils { GUARD_FUNCTIONS.putIfAbsent(name, guardChecker); } + public static TypeMapper createMapper(String value) { + return createMapper((s) -> value.equals(s) ? value : null, s -> Collections.singletonList(value)); + } + public static TypeMapper createMapper(Function mapper, Function> tabCompleter) { return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s)); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index e18b5f8..af2c36e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -82,6 +82,13 @@ class SubCommand { String name = clazz.getTypeName(); if (mapper != null) { name = mapper.value(); + } else { + SWCommand.StaticValue staticValue = parameter.getAnnotation(SWCommand.StaticValue.class); + if (parameter.getType() == String.class) { + arguments[i - 1] = SWCommandUtils.createMapper(staticValue.value()); + guards[i - 1] = getGuardChecker(parameter, localGuardChecker); + continue; + } } arguments[i - 1] = localTypeMapper.containsKey(name) ? localTypeMapper.get(name) From d6cf8477f948db09ecc76c0e07fa091c616f8e41 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 3 Dec 2021 18:22:19 +0100 Subject: [PATCH 2/3] Add SWCommand.Register.noTabComplete --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 7 +++++-- SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 258ab5c..60a2c6b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -78,6 +78,7 @@ public abstract class SWCommand { } String string = args[args.length - 1].toLowerCase(); return commandList.stream() + .filter(s -> !s.noTabComplete) .map(s -> s.tabComplete(sender, args)) .filter(Objects::nonNull) .flatMap(Collection::stream) @@ -118,7 +119,7 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument"); return; } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null)); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete())); }); } for (Method method : methods) { @@ -140,7 +141,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description())); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description(), anno.noTabComplete())); }); this.commandList.sort((o1, o2) -> { @@ -287,6 +288,8 @@ public abstract class SWCommand { String[] description() default {}; + boolean noTabComplete() default false; + @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @interface Registeres { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index e18b5f8..bd6c743 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -45,12 +45,14 @@ class SubCommand { GuardChecker guardChecker; Class varArgType = null; private boolean help; + boolean noTabComplete = false; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help, String[] description) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help, String[] description, boolean noTabComplete) { this.swCommand = swCommand; this.method = method; this.help = help; this.description = description; + this.noTabComplete = noTabComplete; Parameter[] parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); From 0e993d8f3e55d8c98862a41156470923f8c4bb8b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 8 Dec 2021 17:22:02 +0100 Subject: [PATCH 3/3] Hotfix SubCommand --- SpigotCore_Main/src/de/steamwar/command/SubCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 0c1adb0..0db26e8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -86,7 +86,7 @@ class SubCommand { name = mapper.value(); } else { SWCommand.StaticValue staticValue = parameter.getAnnotation(SWCommand.StaticValue.class); - if (parameter.getType() == String.class) { + if (staticValue != null && parameter.getType() == String.class) { arguments[i - 1] = SWCommandUtils.createMapper(staticValue.value()); guards[i - 1] = getGuardChecker(parameter, localGuardChecker); continue;