SteamWar/SpigotCore
Archiviert
13
0

Add SubCommand.varArgType

Add SWCommandUtils varArg capabilities
Dieser Commit ist enthalten in:
yoyosource 2021-03-30 14:31:52 +02:00
Ursprung 2bb7a435c3
Commit a7bf487336
2 geänderte Dateien mit 21 neuen und 15 gelöschten Zeilen

Datei anzeigen

@ -28,6 +28,7 @@ import org.bukkit.command.SimpleCommandMap;
import org.bukkit.entity.Player;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
@ -99,7 +100,7 @@ public class SWCommandUtils {
}
}
static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, boolean varArgs, String[] subCommand) throws CommandParseException {
static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, Class<?> varArgType, String[] subCommand) throws CommandParseException {
Object[] arguments = new Object[parameters.length + 1];
int index = 0;
while (index < subCommand.length) {
@ -109,19 +110,26 @@ public class SWCommandUtils {
index++;
}
for (int i = 0; i < parameters.length - (varArgs ? 1 : 0); i++) {
arguments[i + 1] = parameters[i].map(args[index++]);
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
arguments[i + 1] = parameters[i].map(args[index]);
index++;
if (arguments[i + 1] == null) {
throw new CommandParseException();
}
}
if (varArgs) {
Object[] varArgument = new Object[args.length - parameters.length + 2];
if (varArgType != null) {
int length = args.length - parameters.length + 1;
Object varArgument = Array.newInstance(varArgType, length);
arguments[arguments.length - 1] = varArgument;
for (int i = 0; i < varArgument.length; i++) {
varArgument[i] = parameters[parameters.length - 1].map(args[index++]);
for (int i = 0; i < length; i++) {
Object value = parameters[parameters.length - 1].map(args[index]);
if (value == null) {
throw new CommandParseException();
}
Array.set(varArgument, i, value);
index++;
}
}
return arguments;

Datei anzeigen

@ -19,7 +19,6 @@
package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import java.lang.reflect.InvocationTargetException;
@ -27,7 +26,6 @@ import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.function.Function;
import java.util.logging.Level;
class SubCommand {
@ -35,8 +33,8 @@ class SubCommand {
private Method method;
String[] subCommand;
private TypeMapper<?>[] arguments;
private boolean varArgs = false;
private Function<CommandSender, ?> commandSenderFunction;
private Class<?> varArgType = null;
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
this(swCommand, method, subCommand, new HashMap<>());
@ -55,8 +53,8 @@ class SubCommand {
Parameter parameter = parameters[i];
Class<?> clazz = parameter.getType();
if (parameter.isVarArgs()) {
varArgs = true;
clazz = clazz.getComponentType();
varArgType = clazz;
}
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
@ -86,11 +84,11 @@ class SubCommand {
if (args.length < arguments.length - 1) {
return false;
}
if (!varArgs && args.length > arguments.length) {
if (varArgType == null && args.length > arguments.length) {
return false;
}
try {
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgs, subCommand);
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgType, subCommand);
objects[0] = commandSenderFunction.apply(commandSender);
method.setAccessible(true);
method.invoke(swCommand, objects);
@ -103,7 +101,7 @@ class SubCommand {
}
List<String> tabComplete(CommandSender commandSender, String[] args) {
if (!varArgs && args.length < arguments.length - 1) {
if (varArgType == null && args.length < arguments.length - 1) {
return null;
}
List<String> argsList = new LinkedList<>(Arrays.asList(args));
@ -123,7 +121,7 @@ class SubCommand {
return null;
}
}
if (varArgs && !argsList.isEmpty()) {
if (varArgType != null && !argsList.isEmpty()) {
while (!argsList.isEmpty()) {
String s = argsList.remove(0);
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);