Add SubCommand.varArgType
Add SWCommandUtils varArg capabilities
Dieser Commit ist enthalten in:
Ursprung
2bb7a435c3
Commit
a7bf487336
@ -28,6 +28,7 @@ import org.bukkit.command.SimpleCommandMap;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.*;
|
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];
|
Object[] arguments = new Object[parameters.length + 1];
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (index < subCommand.length) {
|
while (index < subCommand.length) {
|
||||||
@ -109,19 +110,26 @@ public class SWCommandUtils {
|
|||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < parameters.length - (varArgs ? 1 : 0); i++) {
|
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
|
||||||
arguments[i + 1] = parameters[i].map(args[index++]);
|
arguments[i + 1] = parameters[i].map(args[index]);
|
||||||
|
index++;
|
||||||
if (arguments[i + 1] == null) {
|
if (arguments[i + 1] == null) {
|
||||||
throw new CommandParseException();
|
throw new CommandParseException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (varArgs) {
|
if (varArgType != null) {
|
||||||
Object[] varArgument = new Object[args.length - parameters.length + 2];
|
int length = args.length - parameters.length + 1;
|
||||||
|
Object varArgument = Array.newInstance(varArgType, length);
|
||||||
arguments[arguments.length - 1] = varArgument;
|
arguments[arguments.length - 1] = varArgument;
|
||||||
|
|
||||||
for (int i = 0; i < varArgument.length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
varArgument[i] = parameters[parameters.length - 1].map(args[index++]);
|
Object value = parameters[parameters.length - 1].map(args[index]);
|
||||||
|
if (value == null) {
|
||||||
|
throw new CommandParseException();
|
||||||
|
}
|
||||||
|
Array.set(varArgument, i, value);
|
||||||
|
index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arguments;
|
return arguments;
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
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,7 +26,6 @@ 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.Function;
|
import java.util.function.Function;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
class SubCommand {
|
class SubCommand {
|
||||||
|
|
||||||
@ -35,8 +33,8 @@ class SubCommand {
|
|||||||
private Method method;
|
private Method method;
|
||||||
String[] subCommand;
|
String[] subCommand;
|
||||||
private TypeMapper<?>[] arguments;
|
private TypeMapper<?>[] arguments;
|
||||||
private boolean varArgs = false;
|
|
||||||
private Function<CommandSender, ?> commandSenderFunction;
|
private Function<CommandSender, ?> commandSenderFunction;
|
||||||
|
private Class<?> varArgType = null;
|
||||||
|
|
||||||
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
|
public SubCommand(SWCommand swCommand, Method method, String[] subCommand) {
|
||||||
this(swCommand, method, subCommand, new HashMap<>());
|
this(swCommand, method, subCommand, new HashMap<>());
|
||||||
@ -55,8 +53,8 @@ class SubCommand {
|
|||||||
Parameter parameter = parameters[i];
|
Parameter parameter = parameters[i];
|
||||||
Class<?> clazz = parameter.getType();
|
Class<?> clazz = parameter.getType();
|
||||||
if (parameter.isVarArgs()) {
|
if (parameter.isVarArgs()) {
|
||||||
varArgs = true;
|
|
||||||
clazz = clazz.getComponentType();
|
clazz = clazz.getComponentType();
|
||||||
|
varArgType = clazz;
|
||||||
}
|
}
|
||||||
|
|
||||||
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
|
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
|
||||||
@ -86,11 +84,11 @@ class SubCommand {
|
|||||||
if (args.length < arguments.length - 1) {
|
if (args.length < arguments.length - 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!varArgs && args.length > arguments.length) {
|
if (varArgType == null && args.length > arguments.length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgs, subCommand);
|
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, 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);
|
||||||
@ -103,7 +101,7 @@ class SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> tabComplete(CommandSender commandSender, String[] args) {
|
List<String> tabComplete(CommandSender commandSender, String[] args) {
|
||||||
if (!varArgs && args.length < arguments.length - 1) {
|
if (varArgType == null && args.length < arguments.length - 1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
List<String> argsList = new LinkedList<>(Arrays.asList(args));
|
List<String> argsList = new LinkedList<>(Arrays.asList(args));
|
||||||
@ -123,7 +121,7 @@ class SubCommand {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (varArgs && !argsList.isEmpty()) {
|
if (varArgType != null && !argsList.isEmpty()) {
|
||||||
while (!argsList.isEmpty()) {
|
while (!argsList.isEmpty()) {
|
||||||
String s = argsList.remove(0);
|
String s = argsList.remove(0);
|
||||||
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren