CommandGuards #130
26
SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java
Normale Datei
26
SpigotCore_Main/src/de/steamwar/command/CommandNoHelpException.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
class CommandNoHelpException extends RuntimeException {
|
||||
|
||||
CommandNoHelpException() {
|
||||
}
|
||||
}
|
25
SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java
Normale Datei
25
SpigotCore_Main/src/de/steamwar/command/GuardCheckType.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
public enum GuardCheckType {
|
||||
COMMAND,
|
||||
TAB_COMPLETE
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
26
SpigotCore_Main/src/de/steamwar/command/GuardResult.java
Normale Datei
26
SpigotCore_Main/src/de/steamwar/command/GuardResult.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
public enum GuardResult {
|
||||
ALLOWED,
|
||||
DENIED_WITH_HELP,
|
||||
DENIED
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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) {
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren