From d9bb62d5f3e3173007dface942c425d16fbaeb6c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 16 Nov 2021 20:41:49 +0100 Subject: [PATCH] Add CommandNoHelpException Add GuardCheckType Add GuardResult --- .../command/CommandNoHelpException.java | 26 ++++++++++++++++ .../de/steamwar/command/GuardCheckType.java | 25 ++++++++++++++++ .../src/de/steamwar/command/GuardChecker.java | 2 +- .../src/de/steamwar/command/GuardResult.java | 26 ++++++++++++++++ .../src/de/steamwar/command/SWCommand.java | 8 +++-- .../de/steamwar/command/SWCommandUtils.java | 2 -- .../src/de/steamwar/command/SubCommand.java | 30 ++++++++++++++----- 7 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/GuardResult.java diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java b/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java new file mode 100644 index 0000000..41b487b --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java @@ -0,0 +1,26 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +class CommandNoHelpException extends RuntimeException { + + CommandNoHelpException() { + } +} diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java new file mode 100644 index 0000000..f2b2214 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java @@ -0,0 +1,25 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +public enum GuardCheckType { + COMMAND, + TAB_COMPLETE +} diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java index ac3fa3c..e8f3a1f 100644 --- a/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java +++ b/SpigotCore_Main/src/de/steamwar/command/GuardChecker.java @@ -26,5 +26,5 @@ public interface GuardChecker { /** * While guarding the first parameter of the command the parameter s of this method is {@code null} */ - boolean guard(CommandSender commandSender, String[] previousArguments, String s); + GuardResult guard(CommandSender commandSender, GuardCheckType guardCheckType, String[] previousArguments, String s); } diff --git a/SpigotCore_Main/src/de/steamwar/command/GuardResult.java b/SpigotCore_Main/src/de/steamwar/command/GuardResult.java new file mode 100644 index 0000000..9ffbf77 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/GuardResult.java @@ -0,0 +1,26 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +public enum GuardResult { + ALLOWED, + DENIED_WITH_HELP, + DENIED +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index f649a1a..fe6387e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -52,8 +52,12 @@ public abstract class SWCommand { if (!initialized) { createMapping(); } - if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { - commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + try { + if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { + commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + } + } catch (CommandNoHelpException e) { + // Ignored } return false; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 39e56d5..52e362e 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -117,7 +117,6 @@ public class SWCommandUtils { } for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { - if (guards[i] != null && !guards[i].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]); index++; if (arguments[i + 1] == null) throw new CommandParseException(); @@ -127,7 +126,6 @@ public class SWCommandUtils { Object varArgument = arguments[arguments.length - 1]; for (int i = 0; i < length; i++) { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), args[index])) throw new CommandParseException(); Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]); if (value == null) throw new CommandParseException(); Array.set(varArgument, i, value); diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 9496d8c..426abc6 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -112,11 +112,25 @@ class SubCommand { if (!commandSenderPredicate.test(commandSender)) { return false; } - if (!guardChecker.guard(commandSender, new String[0], null)) { - return false; - } Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, guards, args, varArgType, subCommand); objects[0] = commandSenderFunction.apply(commandSender); + for (int i = 0; i < objects.length; i++) { + GuardChecker current; + if (i == 0 && guardChecker != null) { + current = guardChecker; + } else { + current = guards[i - 1]; + } + if (current != null) { + GuardResult guardResult = current.guard(commandSender, GuardCheckType.COMMAND, new String[0], null); + if (guardResult != GuardResult.ALLOWED) { + if (guardResult == GuardResult.DENIED) { + throw new CommandNoHelpException(); + } + return false; + } + } + } method.setAccessible(true); method.invoke(swCommand, objects); } catch (IllegalAccessException | RuntimeException | InvocationTargetException e) { @@ -131,7 +145,7 @@ class SubCommand { if (varArgType == null && args.length > arguments.length + subCommand.length) { return null; } - if (guardChecker != null && !guardChecker.guard(commandSender, new String[0], null)) { + if (guardChecker != null && guardChecker.guard(commandSender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) { return null; } int index = 0; @@ -145,13 +159,13 @@ class SubCommand { for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + if (guards[index] != null && guards[index].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, Arrays.copyOf(args, index), s)) { + if (guards[index] != null && guards[index].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) { @@ -166,13 +180,13 @@ class SubCommand { while (!argsList.isEmpty()) { String s = argsList.remove(0); if (argsList.isEmpty()) { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) { + if (guards[guards.length - 1] != null && guards[guards.length - 1].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, args.length - 1), s) != GuardResult.ALLOWED) { return null; } return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); } try { - if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), s)) { + if (guards[guards.length - 1] != null && guards[guards.length - 1].guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index), s) != GuardResult.ALLOWED) { return null; } if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) {