SteamWar/SpigotCore
Archiviert
13
0

CommandGuards #130

Zusammengeführt
YoyoNow hat 5 Commits von CommandGuards nach master 2021-11-16 21:36:40 +01:00 zusammengeführt
3 geänderte Dateien mit 14 neuen und 7 gelöschten Zeilen
Nur Änderungen aus Commit cc5d435199 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -21,5 +21,6 @@ package de.steamwar.command;
public enum GuardCheckType {
COMMAND,
HELP_COMMAND,
TAB_COMPLETE
}

Datei anzeigen

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

Datei anzeigen

@ -43,10 +43,12 @@ class SubCommand {
private Function<CommandSender, ?> commandSenderFunction;
private GuardChecker guardChecker;
Class<?> varArgType = null;
private boolean help = false;
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker) {
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> 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()) {