SteamWar/SpigotCore
Archiviert
13
0

Add TypeMapper.map with CommandSender

Dieser Commit ist enthalten in:
yoyosource 2021-04-10 14:52:58 +02:00
Ursprung 0b21b743e4
Commit 45c2a77186
3 geänderte Dateien mit 16 neuen und 8 gelöschten Zeilen

Datei anzeigen

@ -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]; Object[] arguments = new Object[parameters.length + 1];
int index = 0; int index = 0;
while (index < subCommand.length) { while (index < subCommand.length) {
@ -115,7 +115,7 @@ public class SWCommandUtils {
arguments[arguments.length - 1] = varArgument; arguments[arguments.length - 1] = varArgument;
} else { } else {
for (int i = 0; i < parameters.length - (varArgType != null ? 1 : 0); i++) { 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++; index++;
if (arguments[i + 1] == null) { if (arguments[i + 1] == null) {
throw new CommandParseException(); throw new CommandParseException();
@ -128,7 +128,7 @@ public class SWCommandUtils {
arguments[arguments.length - 1] = varArgument; arguments[arguments.length - 1] = varArgument;
for (int i = 0; i < length; i++) { 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) { if (value == null) {
throw new CommandParseException(); 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) { public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, BiFunction<CommandSender, String, List<String>> tabCompleter) {
return new TypeMapper<T>() { return new TypeMapper<T>() {
@Override @Override
public T map(String[] previous, String s) { public T map(CommandSender commandSender, String[] previousArguments, String s) {
return mapper.apply(s); return mapper.apply(s);
} }

Datei anzeigen

@ -88,7 +88,7 @@ class SubCommand {
return false; return false;
} }
try { try {
Object[] objects = SWCommandUtils.generateArgumentArray(arguments, args, varArgType, subCommand); Object[] objects = SWCommandUtils.generateArgumentArray(commandSender, 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);
@ -116,7 +116,7 @@ class SubCommand {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try { try {
if (argument.map(Arrays.copyOf(args, index), s) == null) { if (argument.map(commandSender, Arrays.copyOf(args, index), s) == null) {
return null; return null;
} }
} catch (Exception e) { } catch (Exception e) {
@ -129,7 +129,7 @@ class SubCommand {
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);
try { 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; return null;
} }
} catch (Exception e) { } catch (Exception e) {

Datei anzeigen

@ -24,7 +24,15 @@ import org.bukkit.command.CommandSender;
import java.util.List; import java.util.List;
public interface TypeMapper<T> { 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); List<String> tabCompletes(CommandSender commandSender, String[] previousArguments, String s);
} }