SteamWar/SpigotCore
Archiviert
13
0

Add TypeMapper.map with CommandSender #98

Manuell gemergt
YoyoNow hat 4 Commits von CommandUpdate nach master 2021-04-10 18:36:47 +02:00 zusammengeführt
4 geänderte Dateien mit 18 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -186,7 +186,7 @@ public abstract class SWCommand {
});
}
protected void unregister() {
public void unregister() {
SWCommandUtils.knownCommandMap.remove(command.getName());
for (String alias : command.getAliases()) {
SWCommandUtils.knownCommandMap.remove(alias);
@ -194,7 +194,7 @@ public abstract class SWCommand {
command.unregister(SWCommandUtils.commandMap);
}
protected void register() {
public void register() {
SWCommandUtils.commandMap.register("steamwar", this.command);
}

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];
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);
}

Datei anzeigen

@ -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) {

Datei anzeigen

@ -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);
}