SteamWar/SpigotCore
Archiviert
13
0

Add better TabComplete for only Command implementations

Dieser Commit ist enthalten in:
yoyosource 2021-03-13 16:17:31 +01:00
Ursprung 03bbdcfe29
Commit add406a8aa
3 geänderte Dateien mit 33 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -24,17 +24,22 @@ import org.bukkit.command.CommandSender;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.Collections;
import java.util.List;
class InternalCommand { class InternalCommand {
private SWCommand swCommand; private SWCommand swCommand;
private Method method; private Method method;
private int increment = 0;
private Parameter[] parameters; private Parameter[] parameters;
InternalCommand(SWCommand swCommand, Method method) { InternalCommand(SWCommand swCommand, Method method) {
this.swCommand = swCommand; this.swCommand = swCommand;
this.method = method; this.method = method;
parameters = method.getParameters(); parameters = method.getParameters();
increment = method.getAnnotation(SWCommand.Register.class).subCommand().length;
} }
boolean invoke(CommandSender commandSender, String[] args) { boolean invoke(CommandSender commandSender, String[] args) {
@ -51,4 +56,24 @@ class InternalCommand {
return true; return true;
} }
List<String> tabComplete(String[] args) {
if (args.length > parameters.length - increment) {
if (parameters[parameters.length - 1].isVarArgs()) {
Class<?> clazz = parameters[parameters.length - 1].getType().getComponentType();
try {
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
} catch (Exception e) {
// Ignored
}
}
return Collections.emptyList();
}
Class<?> clazz = parameters[args.length - increment + 1].getType();
try {
return SWCommandUtils.MAPPER_FUNCTIONS.getOrDefault(clazz, SWCommandUtils.ERROR_FUNCTION).tabCompletes(args[args.length - 1]);
} catch (Exception e) {
return Collections.emptyList();
}
}
} }

Datei anzeigen

@ -57,7 +57,6 @@ public abstract class SWCommand {
@Override @Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
// TODO: add better tab complete, command based
List<String> strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>())); List<String> strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>()));
for (InternalTabComplete internalTabComplete : tabCompleteSet) { for (InternalTabComplete internalTabComplete : tabCompleteSet) {
SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args); SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args);
@ -65,6 +64,12 @@ public abstract class SWCommand {
strings.addAll(tabComplete.tabCompletes); strings.addAll(tabComplete.tabCompletes);
} }
} }
for (InternalCommand internalCommand : commandSet) {
List<String> stringList = internalCommand.tabComplete(args);
if (stringList != null) {
strings.addAll(stringList);
}
}
strings = new ArrayList<>(strings); strings = new ArrayList<>(strings);
for (int i = strings.size() - 1; i >= 0; i--) { for (int i = strings.size() - 1; i >= 0; i--) {
if (!strings.get(i).startsWith(args[args.length - 1])) { if (!strings.get(i).startsWith(args[args.length - 1])) {

Datei anzeigen

@ -225,7 +225,8 @@ class SWCommandUtils {
} }
} }
public static <K> void addMapper(Class<K> clazz, TypeMapper<K> mapper) { public static void addMapper(Class<?> clazz, TypeMapper<?> mapper) {
if (clazz.isEnum()) return;
if (MAPPER_FUNCTIONS.containsKey(clazz)) return; if (MAPPER_FUNCTIONS.containsKey(clazz)) return;
MAPPER_FUNCTIONS.put(clazz, mapper); MAPPER_FUNCTIONS.put(clazz, mapper);
} }