From d48175234ad70e76b9c78ca130a5193f75af73de Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 5 May 2021 08:45:15 +0200 Subject: [PATCH] Simplify SubCommand and SWCommand --- .../src/de/steamwar/command/SWCommand.java | 2 +- .../src/de/steamwar/command/SubCommand.java | 24 +++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 44865c9..d20237b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -109,7 +109,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; } - commandHelpSet.add(new SubCommand(this, method, anno.value())); + commandHelpSet.add(new SubCommand(this, method, anno.value(), new HashMap<>())); }); } for (Method method : getClass().getDeclaredMethods()) { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 86cfdc7..ef75877 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -28,6 +28,8 @@ import java.util.*; import java.util.function.Function; import java.util.function.Predicate; +import static de.steamwar.command.SWCommandUtils.*; + class SubCommand { private SWCommand swCommand; @@ -38,10 +40,6 @@ class SubCommand { private Function commandSenderFunction; Class varArgType = null; - 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; @@ -61,13 +59,13 @@ class SubCommand { } SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class); - if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) { + if (clazz.isEnum() && mapper == null && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) { Class> enumClass = (Class>) clazz; List tabCompletes = new ArrayList<>(); for (Enum enumConstant : enumClass.getEnumConstants()) { tabCompletes.add(enumConstant.name().toLowerCase()); } - arguments[i - 1] = SWCommandUtils.createMapper(s -> SWCommandUtils.ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); + arguments[i - 1] = SWCommandUtils.createMapper(s -> ENUM_MAPPER.apply(enumClass, s), s -> tabCompletes); continue; } @@ -75,11 +73,9 @@ class SubCommand { if (mapper != null) { name = mapper.value(); } - 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); - } + arguments[i - 1] = localTypeMapper.containsKey(name) + ? localTypeMapper.get(name) + : MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION); } } @@ -120,7 +116,8 @@ class SubCommand { } for (TypeMapper argument : arguments) { String s = argsList.remove(0); - if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); + if (argsList.isEmpty()) + return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { return null; @@ -133,7 +130,8 @@ class SubCommand { if (varArgType != null && !argsList.isEmpty()) { while (!argsList.isEmpty()) { String s = argsList.remove(0); - if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); + if (argsList.isEmpty()) + return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) { return null;