CommandFramework3 #94
@ -79,9 +79,7 @@ public abstract class SWCommand {
|
||||
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false;
|
||||
for (int i = 1; i < parameters.length; i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
StringConstraint stringConstraint = parameter.getAnnotation(StringConstraint.class);
|
||||
Class<?> clazz = parameter.getType();
|
||||
if (stringConstraint != null && clazz != String.class) return false;
|
||||
if (parameter.isVarArgs() && i == parameters.length - 1) {
|
||||
clazz = parameter.getType().getComponentType();
|
||||
}
|
||||
@ -103,12 +101,7 @@ public abstract class SWCommand {
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.METHOD})
|
||||
protected @interface Register {
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.PARAMETER})
|
||||
@interface StringConstraint {
|
||||
String constraint();
|
||||
String[] subCommand() default {};
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
@ -78,7 +79,7 @@ class SWCommandUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static Object[] generateArgumentArray(CommandSender commandSender, Parameter[] parameters, String[] args) {
|
||||
public static Object[] generateArgumentArray(CommandSender commandSender, Method method, Parameter[] parameters, String[] args) {
|
||||
Object[] arguments = new Object[args.length + 1];
|
||||
boolean varArgs = false;
|
||||
if (parameters[parameters.length - 1].isVarArgs()) {
|
||||
@ -87,14 +88,15 @@ class SWCommandUtils {
|
||||
}
|
||||
arguments[0] = parameters[0].getType().cast(commandSender);
|
||||
|
||||
for (int i = 1; i < parameters.length - (varArgs ? 1 : 0); i++) {
|
||||
Parameter parameter = parameters[i];
|
||||
SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
|
||||
int subCommandIndex = 1;
|
||||
for (String s : register.subCommand()) {
|
||||
if (!args[subCommandIndex++].equals(s)) throw new SecurityException();
|
||||
}
|
||||
|
||||
for (int i = subCommandIndex; i < parameters.length - (varArgs ? 1 : 0); i++) {
|
||||
Class<?> clazz = parameters[i].getType();
|
||||
arguments[i] = mapper(clazz).apply(args[i - 1]);
|
||||
SWCommand.StringConstraint stringConstraint = parameter.getAnnotation(SWCommand.StringConstraint.class);
|
||||
if (stringConstraint != null && !Objects.equals(arguments[i], stringConstraint.constraint())) {
|
||||
throw new SecurityException();
|
||||
}
|
||||
}
|
||||
|
||||
if (varArgs) {
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren