diff --git a/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java b/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java index 945c78a..6481fa3 100644 --- a/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/InternalCommand.java @@ -43,7 +43,9 @@ class InternalCommand { } boolean invoke(CommandSender commandSender, String[] args) { - if (args.length < parameters.length - 1) return false; + if (args.length < parameters.length - 1) { + return false; + } try { Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args); method.setAccessible(true); @@ -59,18 +61,26 @@ class InternalCommand { List tabComplete(String[] args) { if (args.length > parameters.length - increment) { if (parameters[parameters.length - 1].isVarArgs()) { - Class clazz = parameters[parameters.length - 1].getType().getComponentType(); + String name = parameters[parameters.length - 1].getType().getComponentType().getTypeName(); + SWCommand.Mapper mapper = parameters[parameters.length - 1].getAnnotation(SWCommand.Mapper.class); + if (mapper != null) { + name = mapper.mapper(); + } try { - return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); + return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); } catch (Exception e) { // Ignored } } return Collections.emptyList(); } - Class clazz = parameters[args.length - increment + 1].getType(); + String name = parameters[args.length - increment + 1].getType().getTypeName(); + SWCommand.Mapper mapper = parameters[args.length - increment + 1].getAnnotation(SWCommand.Mapper.class); + if (mapper != null) { + name = mapper.mapper(); + } try { - return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); + return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(name, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]); } catch (Exception e) { return Collections.emptyList(); } diff --git a/SpigotCore_Main/src/de/steamwar/command/InternalTabComplete.java b/SpigotCore_Main/src/de/steamwar/command/InternalTabComplete.java index 44effb6..cb7c655 100644 --- a/SpigotCore_Main/src/de/steamwar/command/InternalTabComplete.java +++ b/SpigotCore_Main/src/de/steamwar/command/InternalTabComplete.java @@ -38,7 +38,9 @@ class InternalTabComplete { } SWCommandUtils.TabComplete invoke(CommandSender commandSender, String[] args) { - if (args.length < parameters.length - 1) return null; + if (args.length < parameters.length - 1) { + return null; + } try { Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, method, parameters, args); method.setAccessible(true); diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 7f83dc4..81e7fed 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,7 +36,8 @@ public abstract class SWCommand { private final Set commandSet = new HashSet<>(); private final Set tabCompleteSet = new HashSet<>(); private final Map> subCommandTabCompletes = new HashMap<>(); - private Consumer helpMessage = sender -> {}; + private Consumer helpMessage = sender -> { + }; protected SWCommand(String command) { this(command, new String[0]); @@ -82,12 +83,16 @@ public abstract class SWCommand { for (Method method : getClass().getDeclaredMethods()) { Register register = method.getDeclaredAnnotation(Register.class); - if (register == null) continue; + if (register == null) { + continue; + } for (int i = 0; i < register.subCommand().length; i++) { subCommandTabCompletes.computeIfAbsent(i, integer -> new HashSet<>()).add(register.subCommand()[i]); } - if (!validMethod(method)) continue; + if (!validMethod(method)) { + continue; + } if (method.getReturnType() == Void.TYPE) { commandSet.add(new InternalCommand(this, method)); } @@ -99,16 +104,27 @@ public abstract class SWCommand { private boolean validMethod(Method method) { Parameter[] parameters = method.getParameters(); - if (parameters.length == 0) return false; - if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false; + if (parameters.length == 0) { + return false; + } + if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) { + return false; + } for (int i = 1; i < parameters.length; i++) { Parameter parameter = parameters[i]; Class clazz = parameter.getType(); if (parameter.isVarArgs() && i == parameters.length - 1) { clazz = parameter.getType().getComponentType(); } - if (clazz.isEnum()) continue; - if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz)) return false; + if (clazz.isEnum()) { + continue; + } + String name = clazz.getTypeName(); + Mapper mapper = parameter.getAnnotation(Mapper.class); + if (mapper != null) { + name = mapper.mapper(); + } + if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return false; } return true; } @@ -128,4 +144,10 @@ public abstract class SWCommand { String[] subCommand() default {}; } + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + protected @interface Mapper { + String mapper(); + } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 636f08b..4df770d 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -38,7 +38,7 @@ class SWCommandUtils { throw new IllegalStateException("Utility Class"); } - static final Map, TypeMapper> MAPPER_FUNCTIONS = new HashMap<>(); + static final Map> MAPPER_FUNCTIONS = new HashMap<>(); static final TypeMapper ERROR_FUNCTION = new TypeMapper<>() { @Override @@ -120,7 +120,7 @@ class SWCommandUtils { } } }); - MAPPER_FUNCTIONS.put(String.class, new TypeMapper() { + MAPPER_FUNCTIONS.put(String.class.getTypeName(), new TypeMapper() { @Override public String map(String s) { return s; @@ -131,7 +131,7 @@ class SWCommandUtils { return Collections.singletonList(s); } }); - MAPPER_FUNCTIONS.put(StringBuilder.class, new TypeMapper() { + MAPPER_FUNCTIONS.put(StringBuilder.class.getTypeName(), new TypeMapper() { @Override public StringBuilder map(String s) { return new StringBuilder(s); @@ -142,7 +142,7 @@ class SWCommandUtils { return Collections.singletonList(s); } }); - MAPPER_FUNCTIONS.put(Player.class, new TypeMapper() { + MAPPER_FUNCTIONS.put(Player.class.getTypeName(), new TypeMapper() { @Override public Player map(String s) { return Bukkit.getPlayer(s); @@ -156,8 +156,8 @@ class SWCommandUtils { } private static void addMapper(Class clazz, Class alternativeClazz, TypeMapper mapper) { - MAPPER_FUNCTIONS.put(clazz, mapper); - MAPPER_FUNCTIONS.put(alternativeClazz, mapper); + MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper); + MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper); } static final CommandMap commandMap; @@ -189,14 +189,13 @@ class SWCommandUtils { } for (int i = subCommandIndex; i < parameters.length - (varArgs ? 1 : 0); i++) { - Class clazz = parameters[i].getType(); - arguments[i] = mapper(clazz).apply(args[i - 1]); + arguments[i] = mapper(parameters[i]).apply(args[i - 1]); } if (varArgs) { Object[] varArgument = new Object[args.length - parameters.length + 2]; arguments[arguments.length - 1] = varArgument; - Function mapper = mapper(parameters[parameters.length - 1].getType().getComponentType()); + Function mapper = mapper(parameters[parameters.length - 1]); int index = 0; for (int i = parameters.length - 2; i < args.length; i++) { @@ -208,13 +207,25 @@ class SWCommandUtils { } @SuppressWarnings("unchecked") - private static Function mapper(Class clazz) { + private static Function mapper(Parameter parameter) { + Class clazz = parameter.getType(); + if (parameter.isVarArgs()) { + clazz = clazz.getComponentType(); + } + if (clazz.isEnum()) { Class> enumClass = (Class>) clazz; return s -> ENUM_MAPPER.apply(enumClass, s); - } else { - return s -> MAPPER_FUNCTIONS.getOrDefault(clazz, ERROR_FUNCTION).map(s); } + + String name = clazz.getTypeName(); + SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); + if (mapper != null) { + name = mapper.mapper(); + } + + TypeMapper typeMapper = MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION); + return typeMapper::map; } public static class TabComplete { @@ -225,10 +236,9 @@ class SWCommandUtils { } } - public static void addMapper(Class clazz, TypeMapper mapper) { - if (clazz.isEnum()) return; - if (MAPPER_FUNCTIONS.containsKey(clazz)) return; - MAPPER_FUNCTIONS.put(clazz, mapper); + public static void addMapper(String name, TypeMapper mapper) { + if (MAPPER_FUNCTIONS.containsKey(name)) return; + MAPPER_FUNCTIONS.put(name, mapper); } } diff --git a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java index 119c217..c9f34e4 100644 --- a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java +++ b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java @@ -22,8 +22,7 @@ package de.steamwar.command; import java.util.List; public interface TypeMapper { - T map(String s); - List tabCompletes(String s); + List tabCompletes(String s); }