SteamWar/SpigotCore
Archiviert
13
0

Simplify ArgumentUtils

Remove unused Argument Types
Add SWCommand.lastArgRepeatable
Dieser Commit ist enthalten in:
jojo 2020-12-30 16:40:39 +01:00
Ursprung d0672072d8
Commit 3f9850ce26
3 geänderte Dateien mit 21 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -62,11 +62,6 @@ public class Argument<T> {
}
return null;
}, Objects::nonNull, GameMode.class);
public static final Argument<Material> MATERIAL = new Argument<>(Material::valueOf, Objects::nonNull, Material.class);
public static final Argument<Particle> PARTICLE = new Argument<>(Particle::valueOf, Objects::nonNull, Particle.class);
public static final Argument<EntityType> ENTITY = new Argument<>(EntityType::valueOf, Objects::nonNull, EntityType.class);
public static final Argument<Sound> SOUND = new Argument<>(Sound::valueOf, Objects::nonNull, Sound.class);
public static final Argument<SoundCategory> SOUND_CATEGORY = new Argument<>(SoundCategory::valueOf, Objects::nonNull, SoundCategory.class);
private Function<String, T> mapper;
private Predicate<T> constraint;

Datei anzeigen

@ -21,16 +21,13 @@
package de.steamwar.command;
import java.util.Arrays;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class ArgumentUtils {
private ArgumentUtils() {
throw new IllegalStateException("Utility class");
}
private ArgumentUtils() {}
private static boolean contains(String[] arguments, String string, BiPredicate<String, String> predicate) {
for (String arg : arguments) if (predicate.test(string, arg)) return true;
@ -67,18 +64,6 @@ public class ArgumentUtils {
return new Argument<>(Integer::parseInt, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
public static Argument<Long> between(long minValue, long maxValue, long... tabValues) {
Predicate<Long> predicate = l -> l >= minValue && l <= maxValue;
return new Argument<>(Long::parseLong, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
public static Argument<Float> between(float minValue, float maxValue, float... tabValues) {
Predicate<Float> predicate = f -> f >= minValue && f <= maxValue;
String[] array = new String[tabValues.length];
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> between(double minValue, double maxValue, double... tabValues) {
Predicate<Double> predicate = d -> d >= minValue && d <= maxValue;
return new Argument<>(Double::parseDouble, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));

Datei anzeigen

@ -21,7 +21,6 @@
package de.steamwar.command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
@ -32,20 +31,35 @@ public class SWCommand<T extends CommandSender> {
private Argument<?>[] arguments;
private BiConsumer<T, ArgumentMap> executor;
private boolean lastArgRepeatable = false;
public SWCommand(BiConsumer<T, ArgumentMap> executor, Argument<?>... arguments) {
this.executor = executor;
this.arguments = arguments;
}
public SWCommand(BiConsumer<T, ArgumentMap> executor, boolean lastArgRepeatable, Argument<?>... arguments) {
this(executor, arguments);
this.lastArgRepeatable = lastArgRepeatable;
}
public boolean execute(T sender, String[] args) {
if (args.length != arguments.length) return false;
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
for (int i = 0; i < (lastArgRepeatable ? arguments.length - 1 : arguments.length); i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) return false;
objects[i] = optional.get();
}
if (lastArgRepeatable) {
Object[] lastArg = new Object[args.length - arguments.length + 1];
for (int i = arguments.length - 1; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) return false;
lastArg[i] = optional.get();
}
objects[objects.length - 1] = lastArg;
}
executor.accept(sender, new ArgumentMap(objects));
return true;
}
@ -59,14 +73,14 @@ public class SWCommand<T extends CommandSender> {
return arguments[index].tabCompleteSupplier(args[index]).orElseGet(ArrayList::new);
}
public static boolean execute(List<SWCommand<? extends CommandSender>> swCommandList, Player player, String[] args) {
for (SWCommand swCommand : swCommandList) {
if (swCommand.execute(player, args)) return true;
public static <T extends CommandSender> boolean execute(List<SWCommand<T>> swCommandList, T sender, String[] args) {
for (SWCommand<T> swCommand : swCommandList) {
if (swCommand.execute(sender, args)) return true;
}
return false;
}
public static List<String> tabComplete(List<SWCommand<? extends CommandSender>> swCommandList, String[] args) {
public static <T extends CommandSender> List<String> tabComplete(List<SWCommand<T>> swCommandList, String[] args) {
List<String> strings = new ArrayList<>();
swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(args)));
return strings;