SteamWar/SpigotCore
Archiviert
13
0

Simplify Argument for common use cases

Add Executor to SWCommand
Dieser Commit ist enthalten in:
jojo 2020-12-27 22:12:42 +01:00
Ursprung 47025962fd
Commit fbc13c75f0
2 geänderte Dateien mit 6 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -33,6 +33,7 @@ public class Argument<T> {
public static final Argument<Long> LONG = new Argument<>(ArgumentType.LONG, l -> true);
public static final Argument<Float> FLOAT = new Argument<>(ArgumentType.FLOAT, integer -> true);
public static final Argument<Double> DOUBLE = new Argument<>(ArgumentType.DOUBLE, integer -> true);
public static final Argument<String> STRING = new Argument<>(ArgumentType.STRING, string -> true);
public static final Argument<String> PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer);
private ArgumentType<T> argumentType;

Datei anzeigen

@ -32,6 +32,7 @@ public class SWCommand {
public SWCommand(Executor executor, Argument<?>... arguments) {
this.arguments = arguments;
this.executor = executor;
}
public boolean checkValidity(String[] args) {
@ -40,7 +41,7 @@ public class SWCommand {
}
for (int i = 0; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (optional.isEmpty()) return false;
if (!optional.isPresent()) return false;
}
return true;
}
@ -51,7 +52,9 @@ public class SWCommand {
}
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
objects[i] = arguments[i].valueSupplier(args[i]).orElseThrow();
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (!optional.isPresent()) throw new IllegalStateException();
objects[i] = optional.get();
}
return Optional.of(executor.execute(player, objects));
}