From cc5d43519997e9516da80850be773c917e3c4c90 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 21:18:42 +0100 Subject: [PATCH] Add GuardCheckType.HELP_COMMAND --- .../src/de/steamwar/command/GuardCheckType.java | 1 + .../src/de/steamwar/command/SWCommand.java | 4 ++-- .../src/de/steamwar/command/SubCommand.java | 16 +++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java index f2b2214..0f023b8 100644 --- a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java +++ b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java @@ -21,5 +21,6 @@ package de.steamwar.command; public enum GuardCheckType { COMMAND, + HELP_COMMAND, TAB_COMPLETE } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index fe6387e..99864c2 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; } - commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker)); + commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true)); }); } for (Method method : methods) { @@ -131,7 +131,7 @@ public abstract class SWCommand { return; } } - commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker)); + commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false)); }); this.commandList.sort((o1, o2) -> { diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 426abc6..87b9485 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -43,10 +43,12 @@ class SubCommand { private Function commandSenderFunction; private GuardChecker guardChecker; Class varArgType = null; + private boolean help = false; - SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker) { + SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map> localTypeMapper, Map localGuardChecker, boolean help) { this.swCommand = swCommand; this.method = method; + this.help = help; Parameter[] parameters = method.getParameters(); commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass()); @@ -116,13 +118,13 @@ class SubCommand { objects[0] = commandSenderFunction.apply(commandSender); for (int i = 0; i < objects.length; i++) { GuardChecker current; - if (i == 0 && guardChecker != null) { + if (i == 0) { current = guardChecker; } else { current = guards[i - 1]; } if (current != null) { - GuardResult guardResult = current.guard(commandSender, GuardCheckType.COMMAND, new String[0], null); + GuardResult guardResult = current.guard(commandSender, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND, new String[0], null); if (guardResult != GuardResult.ALLOWED) { if (guardResult == GuardResult.DENIED) { throw new CommandNoHelpException(); @@ -133,6 +135,8 @@ class SubCommand { } method.setAccessible(true); method.invoke(swCommand, objects); + } catch (CommandNoHelpException e) { + throw e; } catch (IllegalAccessException | RuntimeException | InvocationTargetException e) { throw new SecurityException(e.getMessage(), e); } catch (CommandParseException e) { @@ -156,16 +160,17 @@ class SubCommand { if (!value.equalsIgnoreCase(s)) return null; index++; } + int guardIndex = 0; for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { + if (guards[guardIndex] != null && guards[guardIndex].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { return null; } return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { - if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { + if (guards[guardIndex] != null && guards[guardIndex].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { @@ -175,6 +180,7 @@ class SubCommand { return null; } index++; + guardIndex++; } if (varArgType != null && !argsList.isEmpty()) { while (!argsList.isEmpty()) {