Simplify Argument for common use cases
Add Executor to SWCommand
Dieser Commit ist enthalten in:
Ursprung
47025962fd
Commit
fbc13c75f0
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren