Dieser Commit ist enthalten in:
Ursprung
59540fe432
Commit
373612f24b
30
SpigotCore_Main/src/de/steamwar/command/GuardChecker.java
Normale Datei
30
SpigotCore_Main/src/de/steamwar/command/GuardChecker.java
Normale Datei
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
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);
|
||||||
|
}
|
@ -39,6 +39,7 @@ public abstract class SWCommand {
|
|||||||
private final List<SubCommand> commandList = new ArrayList<>();
|
private final List<SubCommand> commandList = new ArrayList<>();
|
||||||
private final List<SubCommand> commandHelpList = new ArrayList<>();
|
private final List<SubCommand> commandHelpList = new ArrayList<>();
|
||||||
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
||||||
|
private final Map<String, GuardChecker> localGuardChecker = new HashMap<>();
|
||||||
|
|
||||||
protected SWCommand(String command) {
|
protected SWCommand(String command) {
|
||||||
this(command, new String[0]);
|
this(command, new String[0]);
|
||||||
@ -85,6 +86,13 @@ public abstract class SWCommand {
|
|||||||
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
||||||
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
|
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
|
||||||
});
|
});
|
||||||
|
addGuard(Guard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> {
|
||||||
|
(anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value(), guardChecker);
|
||||||
|
});
|
||||||
|
addGuard(ClassGuard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> {
|
||||||
|
(anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), guardChecker);
|
||||||
|
});
|
||||||
|
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
||||||
if (!anno.help()) return;
|
if (!anno.help()) return;
|
||||||
if (parameters.length != 2) {
|
if (parameters.length != 2) {
|
||||||
@ -97,7 +105,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");
|
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>()));
|
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
@ -119,7 +127,7 @@ public abstract class SWCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper));
|
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.commandList.sort((o1, o2) -> {
|
this.commandList.sort((o1, o2) -> {
|
||||||
@ -167,6 +175,17 @@ public abstract class SWCommand {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T extends Annotation> void addGuard(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, GuardChecker> consumer) {
|
||||||
|
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
||||||
|
try {
|
||||||
|
method.setAccessible(true);
|
||||||
|
consumer.accept(anno, (GuardChecker) method.invoke(this));
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new SecurityException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void unregister() {
|
public void unregister() {
|
||||||
SWCommandUtils.knownCommandMap.remove(command.getName());
|
SWCommandUtils.knownCommandMap.remove(command.getName());
|
||||||
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove);
|
||||||
@ -207,4 +226,20 @@ public abstract class SWCommand {
|
|||||||
|
|
||||||
boolean local() default false;
|
boolean local() default false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
protected @interface Guard {
|
||||||
|
String value() default "";
|
||||||
|
|
||||||
|
boolean local() default false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
protected @interface ClassGuard {
|
||||||
|
Class<?> value();
|
||||||
|
|
||||||
|
boolean local() default false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,6 +44,7 @@ public class SWCommandUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||||
|
static final Map<String, GuardChecker> GUARD_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
static final TypeMapper<?> ERROR_FUNCTION = createMapper(s -> {
|
static final TypeMapper<?> ERROR_FUNCTION = createMapper(s -> {
|
||||||
throw new SecurityException();
|
throw new SecurityException();
|
||||||
@ -100,7 +101,7 @@ public class SWCommandUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper<?>[] parameters, String[] args, Class<?> varArgType, String[] subCommand) throws CommandParseException {
|
static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper<?>[] parameters, GuardChecker[] guards, String[] args, Class<?> varArgType, String[] subCommand) throws CommandParseException {
|
||||||
Object[] arguments = new Object[parameters.length + 1];
|
Object[] arguments = new Object[parameters.length + 1];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (index < subCommand.length) {
|
while (index < subCommand.length) {
|
||||||
@ -116,6 +117,7 @@ public class SWCommandUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
|
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]);
|
arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]);
|
||||||
index++;
|
index++;
|
||||||
if (arguments[i + 1] == null) throw new CommandParseException();
|
if (arguments[i + 1] == null) throw new CommandParseException();
|
||||||
@ -125,6 +127,7 @@ public class SWCommandUtils {
|
|||||||
Object varArgument = arguments[arguments.length - 1];
|
Object varArgument = arguments[arguments.length - 1];
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
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]);
|
Object value = parameters[parameters.length - 1].map(commandSender, Arrays.copyOf(args, index), args[index]);
|
||||||
if (value == null) throw new CommandParseException();
|
if (value == null) throw new CommandParseException();
|
||||||
Array.set(varArgument, i, value);
|
Array.set(varArgument, i, value);
|
||||||
@ -142,6 +145,14 @@ public class SWCommandUtils {
|
|||||||
MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
|
MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void addGuard(Class<?> clazz, GuardChecker guardChecker) {
|
||||||
|
addGuard(clazz.getTypeName(), guardChecker);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addGuard(String name, GuardChecker guardChecker) {
|
||||||
|
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {
|
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {
|
||||||
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s));
|
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s));
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package de.steamwar.command;
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@ -27,6 +28,7 @@ import java.lang.reflect.Parameter;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import static de.steamwar.command.SWCommandUtils.*;
|
import static de.steamwar.command.SWCommandUtils.*;
|
||||||
|
|
||||||
@ -36,11 +38,13 @@ class SubCommand {
|
|||||||
private Method method;
|
private Method method;
|
||||||
String[] subCommand;
|
String[] subCommand;
|
||||||
TypeMapper<?>[] arguments;
|
TypeMapper<?>[] arguments;
|
||||||
|
GuardChecker[] guards;
|
||||||
private Predicate<CommandSender> commandSenderPredicate;
|
private Predicate<CommandSender> commandSenderPredicate;
|
||||||
private Function<CommandSender, ?> commandSenderFunction;
|
private Function<CommandSender, ?> commandSenderFunction;
|
||||||
|
private GuardChecker guardChecker;
|
||||||
Class<?> varArgType = null;
|
Class<?> varArgType = null;
|
||||||
|
|
||||||
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper) {
|
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker) {
|
||||||
this.swCommand = swCommand;
|
this.swCommand = swCommand;
|
||||||
this.method = method;
|
this.method = method;
|
||||||
|
|
||||||
@ -48,8 +52,10 @@ class SubCommand {
|
|||||||
commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass());
|
commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass());
|
||||||
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
||||||
this.subCommand = subCommand;
|
this.subCommand = subCommand;
|
||||||
|
guardChecker = getGuardChecker(parameters[0], localGuardChecker);
|
||||||
|
|
||||||
arguments = new TypeMapper[parameters.length - 1];
|
arguments = new TypeMapper[parameters.length - 1];
|
||||||
|
guards = new GuardChecker[parameters.length - 1];
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
for (int i = 1; i < parameters.length; i++) {
|
||||||
Parameter parameter = parameters[i];
|
Parameter parameter = parameters[i];
|
||||||
Class<?> clazz = parameter.getType();
|
Class<?> clazz = parameter.getType();
|
||||||
@ -76,9 +82,22 @@ class SubCommand {
|
|||||||
arguments[i - 1] = localTypeMapper.containsKey(name)
|
arguments[i - 1] = localTypeMapper.containsKey(name)
|
||||||
? localTypeMapper.get(name)
|
? localTypeMapper.get(name)
|
||||||
: MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION);
|
: MAPPER_FUNCTIONS.getOrDefault(name, ERROR_FUNCTION);
|
||||||
|
guards[i - 1] = getGuardChecker(parameter, localGuardChecker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private GuardChecker getGuardChecker(Parameter parameter, Map<String, GuardChecker> localGuardChecker) {
|
||||||
|
SWCommand.Guard guard = parameter.getAnnotation(SWCommand.Guard.class);
|
||||||
|
if (guard != null) {
|
||||||
|
GuardChecker current = localGuardChecker.getOrDefault(guard.value(), GUARD_FUNCTIONS.getOrDefault(guard.value(), null));
|
||||||
|
if (guardChecker == null) {
|
||||||
|
Bukkit.getLogger().log(Level.WARNING, () -> "The guard checker with name '" + guard.value() + "' is neither a local guard checker nor a global one");
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
boolean invoke(CommandSender commandSender, String[] args) {
|
boolean invoke(CommandSender commandSender, String[] args) {
|
||||||
if (args.length < arguments.length + subCommand.length - (varArgType != null ? 1 : 0)) {
|
if (args.length < arguments.length + subCommand.length - (varArgType != null ? 1 : 0)) {
|
||||||
return false;
|
return false;
|
||||||
@ -90,7 +109,10 @@ class SubCommand {
|
|||||||
if (!commandSenderPredicate.test(commandSender)) {
|
if (!commandSenderPredicate.test(commandSender)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, args, varArgType, subCommand);
|
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);
|
objects[0] = commandSenderFunction.apply(commandSender);
|
||||||
method.setAccessible(true);
|
method.setAccessible(true);
|
||||||
method.invoke(swCommand, objects);
|
method.invoke(swCommand, objects);
|
||||||
@ -106,6 +128,9 @@ class SubCommand {
|
|||||||
if (varArgType == null && args.length > arguments.length + subCommand.length) {
|
if (varArgType == null && args.length > arguments.length + subCommand.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (guardChecker != null && !guardChecker.guard(commandSender, new String[0], null)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
int index = 0;
|
int index = 0;
|
||||||
List<String> argsList = new LinkedList<>(Arrays.asList(args));
|
List<String> argsList = new LinkedList<>(Arrays.asList(args));
|
||||||
for (String value : subCommand) {
|
for (String value : subCommand) {
|
||||||
@ -117,9 +142,15 @@ class SubCommand {
|
|||||||
for (TypeMapper<?> argument : arguments) {
|
for (TypeMapper<?> argument : arguments) {
|
||||||
String s = argsList.remove(0);
|
String s = argsList.remove(0);
|
||||||
if (argsList.isEmpty()) {
|
if (argsList.isEmpty()) {
|
||||||
|
if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
if (guards[index] != null && !guards[index].guard(commandSender, Arrays.copyOf(args, index), s)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -132,9 +163,15 @@ class SubCommand {
|
|||||||
while (!argsList.isEmpty()) {
|
while (!argsList.isEmpty()) {
|
||||||
String s = argsList.remove(0);
|
String s = argsList.remove(0);
|
||||||
if (argsList.isEmpty()) {
|
if (argsList.isEmpty()) {
|
||||||
|
if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, args.length - 1), s)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
if (guards[guards.length - 1] != null && !guards[guards.length - 1].guard(commandSender, Arrays.copyOf(args, index), s)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren