From a42188f7a03dc423892eae78b8c85df1cb773c77 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 5 May 2021 09:21:05 +0200 Subject: [PATCH] Streamify SWCommand --- .../src/de/steamwar/command/SWCommand.java | 80 +++++++------------ 1 file changed, 28 insertions(+), 52 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index e29b8e4..9c66a2c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -30,12 +30,13 @@ import java.util.*; import java.util.function.BiConsumer; import java.util.function.IntPredicate; import java.util.logging.Level; +import java.util.stream.Collectors; public abstract class SWCommand { private final Command command; - private final List commandSet = new ArrayList<>(); - private final List commandHelpSet = new ArrayList<>(); + private final List commandList = new ArrayList<>(); + private final List commandHelpList = new ArrayList<>(); private final Map> localTypeMapper = new HashMap<>(); protected SWCommand(String command) { @@ -46,15 +47,13 @@ public abstract class SWCommand { this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) { @Override public boolean execute(CommandSender sender, String alias, String[] args) { - for (SubCommand subCommand : commandSet) { - if (subCommand.invoke(sender, args)) { - return false; - } + // if (commandList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false; + // if (commandHelpList.stream().filter(s -> s.invoke(sender, args)).findFirst().isEmpty()) return false; + for (SubCommand subCommand : commandList) { + if (subCommand.invoke(sender, args)) return false; } - for (SubCommand subCommand : commandHelpSet) { - if (subCommand.invoke(sender, args)) { - return false; - } + for (SubCommand subCommand : commandHelpList) { + if (subCommand.invoke(sender, args)) return false; } return false; } @@ -62,19 +61,15 @@ public abstract class SWCommand { @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { List strings = new ArrayList<>(); - for (SubCommand subCommand : commandSet) { + for (SubCommand subCommand : commandList) { List tabCompletes = subCommand.tabComplete(sender, args); - if (tabCompletes != null) { - strings.addAll(tabCompletes); - } + if (tabCompletes != null) strings.addAll(tabCompletes); } - strings = new ArrayList<>(strings); - for (int i = strings.size() - 1; i >= 0; i--) { - if (!strings.get(i).toLowerCase().startsWith(args[args.length - 1].toLowerCase())) { - strings.remove(i); - } - } - return strings; + return strings.stream() + .filter(s -> !s.isEmpty()) + .filter(s -> !s.isBlank()) + .filter(s -> s.toLowerCase().startsWith(args[args.length - 1].toLowerCase())) + .collect(Collectors.toList()); } }; unregister(); @@ -83,23 +78,13 @@ public abstract class SWCommand { Method[] methods = getClass().getDeclaredMethods(); for (Method method : methods) { addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { - if (anno.local()) { - localTypeMapper.put(anno.value(), typeMapper); - } else { - SWCommandUtils.addMapper(anno.value(), typeMapper); - } + (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper); }); addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> { - if (anno.local()) { - localTypeMapper.put(anno.value().getTypeName(), typeMapper); - } else { - SWCommandUtils.addMapper(anno.value().getTypeName(), typeMapper); - } + (anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper); }); add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { - if (!anno.help()) { - return; - } + if (!anno.help()) return; if (parameters.length != 2) { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many"); } @@ -110,14 +95,12 @@ 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(), new HashMap<>())); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>())); }); } for (Method method : methods) { add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> { - if (anno.help()) { - return; - } + if (anno.help()) return; for (int i = 1; i < parameters.length; i++) { Parameter parameter = parameters[i]; Class clazz = parameter.getType(); @@ -137,10 +120,10 @@ public abstract class SWCommand { return; } } - commandSet.add(new SubCommand(this, method, anno.value(), localTypeMapper)); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper)); }); - this.commandSet.sort((o1, o2) -> { + this.commandList.sort((o1, o2) -> { int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length); if (compare != 0) { return compare; @@ -150,15 +133,13 @@ public abstract class SWCommand { return Integer.compare(i1, i2); } }); - commandHelpSet.sort(Comparator.comparingInt(o -> -o.subCommand.length)); + commandHelpList.sort(Comparator.comparingInt(o -> -o.subCommand.length)); } } private void add(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer consumer) { T[] anno = SWCommandUtils.getAnnotation(method, annotation); - if (anno == null || anno.length == 0) { - return; - } + if (anno == null || anno.length == 0) return; Parameter[] parameters = method.getParameters(); if (!parameterTester.test(parameters.length)) { @@ -173,17 +154,14 @@ public abstract class SWCommand { Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'"); return; } - for (T a : anno) { - consumer.accept(a, parameters); - } + Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters)); } private void addMapper(Class annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class returnType, BiConsumer> consumer) { add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> { try { method.setAccessible(true); - Object object = method.invoke(this); - consumer.accept(anno, (TypeMapper) object); + consumer.accept(anno, (TypeMapper) method.invoke(this)); } catch (Exception e) { throw new SecurityException(e.getMessage(), e); } @@ -192,9 +170,7 @@ public abstract class SWCommand { public void unregister() { SWCommandUtils.knownCommandMap.remove(command.getName()); - for (String alias : command.getAliases()) { - SWCommandUtils.knownCommandMap.remove(alias); - } + command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); command.unregister(SWCommandUtils.commandMap); }