SteamWar/SpigotCore
Archiviert
13
0

Remove SWCommand.StringConstraints

Add SWCommand.Register.subCommand
Dieser Commit ist enthalten in:
yoyosource 2021-03-12 17:06:37 +01:00
Ursprung ce7d261174
Commit 03d35042e3
2 geänderte Dateien mit 10 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -79,9 +79,7 @@ public abstract class SWCommand {
if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false; if (!CommandSender.class.isAssignableFrom(parameters[0].getType())) return false;
for (int i = 1; i < parameters.length; i++) { for (int i = 1; i < parameters.length; i++) {
Parameter parameter = parameters[i]; Parameter parameter = parameters[i];
StringConstraint stringConstraint = parameter.getAnnotation(StringConstraint.class);
Class<?> clazz = parameter.getType(); Class<?> clazz = parameter.getType();
if (stringConstraint != null && clazz != String.class) return false;
if (parameter.isVarArgs() && i == parameters.length - 1) { if (parameter.isVarArgs() && i == parameters.length - 1) {
clazz = parameter.getType().getComponentType(); clazz = parameter.getType().getComponentType();
} }
@ -103,12 +101,7 @@ public abstract class SWCommand {
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD}) @Target({ElementType.METHOD})
protected @interface Register { protected @interface Register {
} String[] subCommand() default {};
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
@interface StringConstraint {
String constraint();
} }
} }

Datei anzeigen

@ -25,6 +25,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; 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]; Object[] arguments = new Object[args.length + 1];
boolean varArgs = false; boolean varArgs = false;
if (parameters[parameters.length - 1].isVarArgs()) { if (parameters[parameters.length - 1].isVarArgs()) {
@ -87,14 +88,15 @@ class SWCommandUtils {
} }
arguments[0] = parameters[0].getType().cast(commandSender); arguments[0] = parameters[0].getType().cast(commandSender);
for (int i = 1; i < parameters.length - (varArgs ? 1 : 0); i++) { SWCommand.Register register = method.getAnnotation(SWCommand.Register.class);
Parameter parameter = parameters[i]; 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(); Class<?> clazz = parameters[i].getType();
arguments[i] = mapper(clazz).apply(args[i - 1]); 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) { if (varArgs) {