diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 432dd78..725f451 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -22,11 +22,16 @@ package de.steamwar.command; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import java.lang.annotation.*; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.*; +import static de.steamwar.command.SWCommandUtils.getAnnotation; + public abstract class SWCommand { private final Set commandSet = new HashSet<>(); @@ -101,7 +106,7 @@ public abstract class SWCommand { clazz = parameter.getType().getComponentType(); } Mapper mapper = parameter.getAnnotation(Mapper.class); - if (clazz.isEnum() && mapper == null) { + if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { continue; } String name = clazz.getTypeName(); @@ -151,11 +156,6 @@ public abstract class SWCommand { } } - private T getAnnotation(Method method, Class annotation) { - if (method.getAnnotations().length != 1) return null; - return method.getAnnotation(annotation); - } - @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) protected @interface Register { diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 9ff0169..f48bf08 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -20,10 +20,13 @@ package de.steamwar.command; import org.bukkit.Bukkit; +import org.bukkit.GameMode; import org.bukkit.command.CommandMap; import org.bukkit.entity.Player; +import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.*; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -149,6 +152,35 @@ class SWCommandUtils { return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); } }); + MAPPER_FUNCTIONS.put(GameMode.class.getTypeName(), new TypeMapper() { + @Override + public GameMode map(String s) { + switch (s.toLowerCase()) { + case "s": + case "survival": + case "0": + return GameMode.SURVIVAL; + case "c": + case "creative": + case "1": + return GameMode.CREATIVE; + case "sp": + case "spectator": + case "3": + return GameMode.SPECTATOR; + case "a": + case "adventure": + case "2": + return GameMode.ADVENTURE; + } + throw new SecurityException(); + } + + @Override + public List tabCompletes(String s) { + return Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "specator", "3", "a", "adventure", "2"); + } + }); } private static void addMapper(Class clazz, Class alternativeClazz, TypeMapper mapper) { @@ -202,4 +234,9 @@ class SWCommandUtils { MAPPER_FUNCTIONS.put(name, mapper); } + static T getAnnotation(Method method, Class annotation) { + if (method.getAnnotations().length != 1) return null; + return method.getAnnotation(annotation); + } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 8d50f73..053aae3 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -45,8 +45,15 @@ class SubCommand { Parameter[] parameters = method.getParameters(); commandSenderFunction = sender -> parameters[0].getType().cast(sender); - SWCommand.Register register = method.getAnnotation(SWCommand.Register.class); - subCommand = register.value(); + SWCommand.Register register = SWCommandUtils.getAnnotation(method, SWCommand.Register.class); + SWCommand.RegisterHelp registerHelp = SWCommandUtils.getAnnotation(method, SWCommand.RegisterHelp.class); + if (register != null) { + subCommand = register.value(); + } else if (registerHelp != null) { + subCommand = registerHelp.value(); + } else { + throw new SecurityException(); + } arguments = new TypeMapper[parameters.length - 1]; for (int i = 1; i < parameters.length; i++) { @@ -58,7 +65,7 @@ class SubCommand { } SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); - if (clazz.isEnum() && mapper == null) { + if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) { Class> enumClass = (Class>) clazz; List tabCompletes = new ArrayList<>(); for (Enum enumConstant : enumClass.getEnumConstants()) {