SteamWar/SpigotCore
Archiviert
13
0

Add CommandNoHelpException
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Add GuardCheckType
Add GuardResult
Dieser Commit ist enthalten in:
yoyosource 2021-11-16 20:41:49 +01:00
Ursprung b1f7f205ad
Commit d9bb62d5f3
7 geänderte Dateien mit 106 neuen und 13 gelöschten Zeilen

Datei anzeigen

@ -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() {
}
}

Datei anzeigen

@ -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
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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
}

Datei anzeigen

@ -52,9 +52,13 @@ public abstract class SWCommand {
if (!initialized) {
createMapping();
}
try {
if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) {
commandHelpList.stream().anyMatch(s -> s.invoke(sender, args));
}
} catch (CommandNoHelpException e) {
// Ignored
}
return false;
}

Datei anzeigen

@ -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);

Datei anzeigen

@ -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) {