Add TypeMapper.map with CommandSender #98
@ -100,7 +100,7 @@ public class SWCommandUtils {
|
||||
}
|
||||
}
|
||||
|
||||
static Object[] generateArgumentArray(TypeMapper<?>[] parameters, String[] args, Class<?> varArgType, String[] subCommand) throws CommandParseException {
|
||||
static Object[] generateArgumentArray(CommandSender commandSender, TypeMapper<?>[] parameters, String[] args, Class<?> varArgType, String[] subCommand) throws CommandParseException {
|
||||
Object[] arguments = new Object[parameters.length + 1];
|
||||
int index = 0;
|
||||
while (index < subCommand.length) {
|
||||
@ -115,7 +115,7 @@ public class SWCommandUtils {
|
||||
arguments[arguments.length - 1] = varArgument;
|
||||
} else {
|
||||
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) {
|
||||
arguments[i + 1] = parameters[i].map(Arrays.copyOf(args, index), args[index]);
|
||||
arguments[i + 1] = parameters[i].map(commandSender, Arrays.copyOf(args, index), args[index]);
|
||||
index++;
|
||||
if (arguments[i + 1] == null) {
|
||||
throw new CommandParseException();
|
||||
@ -128,7 +128,7 @@ public class SWCommandUtils {
|
||||
arguments[arguments.length - 1] = varArgument;
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
Object value = parameters[parameters.length - 1].map(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();
|
||||
}
|
||||
@ -156,7 +156,7 @@ public class SWCommandUtils {
|
||||
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, BiFunction<CommandSender, String, List<String>> tabCompleter) {
|
||||
return new TypeMapper<T>() {
|
||||
@Override
|
||||
public T map(String[] previous, String s) {
|
||||
public T map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||
return mapper.apply(s);
|
||||
}
|
||||
|
||||
|
@ -88,7 +88,7 @@ class SubCommand {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgType, subCommand);
|
||||
Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, arguments, args, varArgType, subCommand);
|
||||
objects[0] = commandSenderFunction.apply(commandSender);
|
||||
method.setAccessible(true);
|
||||
method.invoke(swCommand, objects);
|
||||
@ -116,7 +116,7 @@ class SubCommand {
|
||||
String s = argsList.remove(0);
|
||||
if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
||||
try {
|
||||
if (argument.map(Arrays.copyOf(args, index), s) == null) {
|
||||
if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -129,7 +129,7 @@ class SubCommand {
|
||||
String s = argsList.remove(0);
|
||||
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
|
||||
try {
|
||||
if (arguments[arguments.length - 1].map(Arrays.copyOf(args, index), s) == null) {
|
||||
if (arguments[arguments.length - 1].map(commandSender, Arrays.copyOf(args, index), s) == null) {
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
@ -24,7 +24,15 @@ import org.bukkit.command.CommandSender;
|
||||
import java.util.List;
|
||||
|
||||
public interface TypeMapper<T> {
|
||||
T map(String[] previousArguments, String s);
|
||||
default T map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||
return map(previousArguments, s);
|
||||
}
|
||||
|
||||
// For backwards compatibility, can be removed later on
|
||||
@Deprecated(since = "Use the other map Function without calling super!")
|
||||
default T map(String[] previousArguments, String s) {
|
||||
throw new SecurityException();
|
||||
}
|
||||
|
||||
List<String> tabCompletes(CommandSender commandSender, String[] previousArguments, String s);
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren