SteamWar/SpigotCore
Archiviert
13
0

Add reactive dynamic Argument TabCompletion

Dieser Commit ist enthalten in:
jojo 2020-12-29 13:46:31 +01:00
Ursprung 52af8a71f5
Commit 72863f30f0
2 geänderte Dateien mit 41 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -21,7 +21,7 @@
package de.steamwar.command;
import com.sk89q.worldedit.extent.world.SurvivalModeExtent;
import com.mojang.datafixers.types.Func;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
@ -69,7 +69,7 @@ public class Argument<T> {
private ArgumentType<T> argumentType;
private Predicate<T> constraint;
private Supplier<String[]> tabCompletes;
private Function<String, String[]> tabCompletes;
private Function<T, ?> valueMapper;
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, String... tabCompletes) {
@ -80,7 +80,15 @@ public class Argument<T> {
this(argumentType, constraint, o -> o, tabCompletes);
}
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<String, String[]> tabCompletes) {
this(argumentType, constraint, o -> o, tabCompletes);
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper, Supplier<String[]> tabCompletes) {
this(argumentType, constraint, valueMapper, s -> tabCompletes.get());
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper, Function<String, String[]> tabCompletes) {
this.argumentType = argumentType;
this.constraint = constraint;
this.valueMapper = valueMapper;
@ -109,7 +117,7 @@ public class Argument<T> {
// Check number constraints if needed
if (argumentType.number && !constraint.test(argumentMapped)) return Optional.empty();
}
List<String> strings = Arrays.stream(tabCompletes.get()).filter(t -> t.startsWith(s)).collect(Collectors.toList());
List<String> strings = Arrays.stream(tabCompletes.apply(s)).filter(t -> t.startsWith(s)).collect(Collectors.toList());
if (strings.isEmpty()) strings.add(s);
return Optional.of(strings);
} catch (NumberFormatException e) {
@ -189,7 +197,7 @@ public class Argument<T> {
@Override
public String toString() {
return "Argument{" +
"tabCompletes=" + Arrays.toString(tabCompletes.get()) +
"tabCompletes=" + Arrays.toString(tabCompletes.apply("")) +
'}';
}

Datei anzeigen

@ -21,6 +21,7 @@
package de.steamwar.command;
import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;
import java.util.Arrays;
import java.util.function.Predicate;
@ -43,6 +44,20 @@ public class ArgumentUtils {
}, arguments);
}
public static Argument<String> of(String[] commands, String[] tabCompletes) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : commands){
if (string.equals(arg)) return true;
}
return false;
}, s -> {
if (s.isEmpty()) {
return tabCompletes;
}
return commands;
});
}
public static Argument<String> ofIgnoreCase(String argument) {
return new Argument<>(ArgumentType.STRING, string -> string.equalsIgnoreCase(argument), argument);
}
@ -56,6 +71,20 @@ public class ArgumentUtils {
}, arguments);
}
public static Argument<String> ofIgnoreCase(String[] commands, String[] tabCompletes) {
return new Argument<>(ArgumentType.STRING, string -> {
for (String arg : commands){
if (string.equalsIgnoreCase(arg)) return true;
}
return false;
}, s -> {
if (s.isEmpty()) {
return tabCompletes;
}
return commands;
});
}
public static Argument<Integer> above(int minValue, int... tabValues) {
return between(minValue, Integer.MAX_VALUE, tabValues);
}