SteamWar/SpigotCore
Archiviert
13
0

Optimize ArgumentUtils

Optimize SWCommand
Optimize SWCommandBundle
Dieser Commit ist enthalten in:
jojo 2020-12-30 13:53:47 +01:00
Ursprung 8d86ee4f16
Commit 35a4b39758
3 geänderte Dateien mit 13 neuen und 65 gelöschten Zeilen

Datei anzeigen

@ -35,8 +35,9 @@ public class ArgumentUtils {
return false;
}
public static Argument<String> of(String argument) {
return new Argument<>(s -> s, string -> string.equals(argument), argument);
private static String[] supply(String s, String[] empty, String[] notEmpty) {
if (s.isEmpty()) return empty;
return notEmpty;
}
public static Argument<String> of(String... arguments) {
@ -44,14 +45,7 @@ public class ArgumentUtils {
}
public static Argument<String> of(String[] commands, String[] tabCompletes) {
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> {
if (s.isEmpty()) return tabCompletes;
return commands;
});
}
public static Argument<String> ofIgnoreCase(String argument) {
return new Argument<>(s -> s, string -> string.equalsIgnoreCase(argument), argument);
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
}
public static Argument<String> ofIgnoreCase(String... arguments) {
@ -59,70 +53,31 @@ public class ArgumentUtils {
}
public static Argument<String> ofIgnoreCase(String[] commands, String[] tabCompletes) {
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> {
if (s.isEmpty()) return tabCompletes;
return commands;
});
}
public static Argument<Integer> above(int minValue, int... tabValues) {
return between(minValue, Integer.MAX_VALUE, tabValues);
}
public static Argument<Integer> below(int maxValue, int... tabValues) {
return between(Integer.MIN_VALUE, maxValue, tabValues);
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
}
public static Argument<Integer> between(int minValue, int maxValue, int... tabValues) {
Predicate<Integer> predicate = i -> i >= minValue && i <= maxValue;
for (int tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException();
String[] array = Arrays.stream(tabValues).mapToObj(i -> i + "").toArray(String[]::new);
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(i -> i + "").toArray(String[]::new);
return new Argument<>(Integer::parseInt, predicate, array);
}
public static Argument<Long> above(long minValue, long... tabValues) {
return between(minValue, Long.MAX_VALUE, tabValues);
}
public static Argument<Long> below(long maxValue, long... tabValues) {
return between(Long.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Long> between(long minValue, long maxValue, long... tabValues) {
Predicate<Long> predicate = l -> l >= minValue && l <= maxValue;
for (long tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException();
String[] array = Arrays.stream(tabValues).mapToObj(l -> l + "").toArray(String[]::new);
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(l -> l + "").toArray(String[]::new);
return new Argument<>(Long::parseLong, predicate, array);
}
public static Argument<Float> above(float minValue, float... tabValues) {
return between(minValue, Float.MAX_VALUE, tabValues);
}
public static Argument<Float> below(float maxValue, float... tabValues) {
return between(Float.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Float> between(float minValue, float maxValue, float... tabValues) {
Predicate<Float> predicate = f -> f >= minValue && f <= maxValue;
for (float tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException();
String[] array = new String[tabValues.length];
for (int i = 0; i < tabValues.length; i++) array[i] = tabValues[i] + "";
for (int i = 0; i < tabValues.length; i++) if (predicate.test(tabValues[i])) array[i] = tabValues[i] + "";
return new Argument<>(Float::parseFloat, predicate, array);
}
public static Argument<Double> above(double minValue, double... tabValues) {
return between(minValue, Double.MAX_VALUE, tabValues);
}
public static Argument<Double> below(double maxValue, double... tabValues) {
return between(Double.MIN_VALUE, maxValue, tabValues);
}
public static Argument<Double> between(double minValue, double maxValue, double... tabValues) {
Predicate<Double> predicate = d -> d >= minValue && d <= maxValue;
for (double tabValue : tabValues) if (!predicate.test(tabValue)) throw new IllegalArgumentException();
String[] array = Arrays.stream(tabValues).mapToObj(d -> d + "").toArray(String[]::new);
String[] array = Arrays.stream(tabValues).boxed().filter(predicate).map(d -> d + "").toArray(String[]::new);
return new Argument<>(Double::parseDouble, predicate, array);
}

Datei anzeigen

@ -36,9 +36,7 @@ public class SWCommand {
}
public boolean execute(Player player, String[] args) {
if (args.length != arguments.length) {
return false;
}
if (args.length != arguments.length) return false;
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
@ -50,12 +48,9 @@ public class SWCommand {
}
public Optional<List<String>> tabComplete(String[] args) {
if (args.length > arguments.length) {
return Optional.empty();
}
if (args.length > arguments.length) return Optional.empty();
for (int i = 0; i < args.length - 1; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) return Optional.empty();
if (!arguments[i].valueSupplier(args[i]).isPresent()) return Optional.empty();
}
int index = args.length - 1;
return arguments[index].tabCompleteSupplier(args[index]);

Datei anzeigen

@ -55,9 +55,7 @@ public class SWCommandBundle {
public List<String> tabComplete(String[] args) {
List<String> strings = new ArrayList<>();
for (SWCommand swCommand : swCommandList) {
swCommand.tabComplete(args).ifPresent(strings::addAll);
}
swCommandList.forEach(swCommand -> swCommand.tabComplete(args).ifPresent(strings::addAll));
return strings;
}