From 58cc8eb2c6e0ea603895c1b02c17fde606e10362 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 3 Dec 2021 18:15:42 +0100 Subject: [PATCH] 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)