SteamWar/SpigotCore
Archiviert
13
0

Add more capabilities to TypeMapper.tabCompletes

Dieser Commit ist enthalten in:
yoyosource 2021-03-29 22:51:27 +02:00
Ursprung 529b3daf6c
Commit 8be46cd7bc
4 geänderte Dateien mit 24 neuen und 9 gelöschten Zeilen

Datei anzeigen

@ -29,8 +29,6 @@ import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import static de.steamwar.command.SWCommandUtils.getAnnotation;
public abstract class SWCommand { public abstract class SWCommand {
private boolean enabled = true; private boolean enabled = true;
@ -65,7 +63,7 @@ public abstract class SWCommand {
if (!enabled) return Collections.emptyList(); if (!enabled) return Collections.emptyList();
List<String> strings = new ArrayList<>(); List<String> strings = new ArrayList<>();
for (SubCommand subCommand : commandSet) { for (SubCommand subCommand : commandSet) {
List<String> tabCompletes = subCommand.tabComplete(args); List<String> tabCompletes = subCommand.tabComplete(sender, args);
if (tabCompletes != null) { if (tabCompletes != null) {
strings.addAll(tabCompletes); strings.addAll(tabCompletes);
} }
@ -124,7 +122,7 @@ public abstract class SWCommand {
} }
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) { private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
T anno = getAnnotation(method, annotation); T anno = SWCommandUtils.getAnnotation(method, annotation);
if (anno == null) { if (anno == null) {
return; return;
} }

Datei anzeigen

@ -22,6 +22,7 @@ package de.steamwar.command;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -133,12 +134,26 @@ public class SWCommandUtils {
} }
@Override @Override
public List<String> tabCompletes(String s) { public List<String> tabCompletes(CommandSender commandSender, String s) {
return tabCompleter.apply(s); return tabCompleter.apply(s);
} }
}; };
} }
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 s) {
return mapper.apply(s);
}
@Override
public List<String> tabCompletes(CommandSender commandSender, String s) {
return tabCompleter.apply(commandSender, s);
}
};
}
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) { private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) {
return s -> { return s -> {
try { try {

Datei anzeigen

@ -100,7 +100,7 @@ class SubCommand {
return true; return true;
} }
List<String> tabComplete(String[] args) { List<String> tabComplete(CommandSender commandSender, String[] args) {
if (!varArgs && args.length < arguments.length - 1) { if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList(); return Collections.emptyList();
} }
@ -112,7 +112,7 @@ class SubCommand {
} }
for (TypeMapper<?> argument : arguments) { for (TypeMapper<?> argument : arguments) {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(s); if (argsList.isEmpty()) return argument.tabCompletes(commandSender, s);
try { try {
if (argument.map(s) == null) { if (argument.map(s) == null) {
return Collections.emptyList(); return Collections.emptyList();
@ -124,7 +124,7 @@ class SubCommand {
if (varArgs && !argsList.isEmpty()) { if (varArgs && !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(s); if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, s);
try { try {
arguments[arguments.length - 1].map(s); arguments[arguments.length - 1].map(s);
} catch (Exception e) { } catch (Exception e) {

Datei anzeigen

@ -19,10 +19,12 @@
package de.steamwar.command; package de.steamwar.command;
import org.bukkit.command.CommandSender;
import java.util.List; import java.util.List;
public interface TypeMapper<T> { public interface TypeMapper<T> {
T map(String s); T map(String s);
List<String> tabCompletes(String s); List<String> tabCompletes(CommandSender commandSender, String s);
} }