Generify SWCommand to any thing implementing CommandSender
Dieser Commit ist enthalten in:
Ursprung
cfa3ff0d8d
Commit
d0672072d8
@ -20,6 +20,7 @@
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -27,17 +28,17 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public class SWCommand {
|
||||
public class SWCommand<T extends CommandSender> {
|
||||
|
||||
private Argument<?>[] arguments;
|
||||
private BiConsumer<Player, ArgumentMap> executor;
|
||||
private BiConsumer<T, ArgumentMap> executor;
|
||||
|
||||
public SWCommand(BiConsumer<Player, ArgumentMap> executor, Argument<?>... arguments) {
|
||||
this.arguments = arguments;
|
||||
public SWCommand(BiConsumer<T, ArgumentMap> executor, Argument<?>... arguments) {
|
||||
this.executor = executor;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public boolean execute(Player player, String[] args) {
|
||||
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++) {
|
||||
@ -45,29 +46,29 @@ public class SWCommand {
|
||||
if (!optional.isPresent()) return false;
|
||||
objects[i] = optional.get();
|
||||
}
|
||||
executor.accept(player, new ArgumentMap(objects));
|
||||
executor.accept(sender, new ArgumentMap(objects));
|
||||
return true;
|
||||
}
|
||||
|
||||
public Optional<List<String>> tabComplete(String[] args) {
|
||||
if (args.length > arguments.length) return Optional.empty();
|
||||
public List<String> tabComplete(String[] args) {
|
||||
if (args.length > arguments.length) return new ArrayList<>();
|
||||
for (int i = 0; i < args.length - 1; i++) {
|
||||
if (!arguments[i].valueSupplier(args[i]).isPresent()) return Optional.empty();
|
||||
if (!arguments[i].valueSupplier(args[i]).isPresent()) return new ArrayList<>();
|
||||
}
|
||||
int index = args.length - 1;
|
||||
return arguments[index].tabCompleteSupplier(args[index]);
|
||||
return arguments[index].tabCompleteSupplier(args[index]).orElseGet(ArrayList::new);
|
||||
}
|
||||
|
||||
public static boolean execute(List<SWCommand> swCommandList, Player player, String[] args) {
|
||||
public static boolean execute(List<SWCommand<? extends CommandSender>> swCommandList, Player player, String[] args) {
|
||||
for (SWCommand swCommand : swCommandList) {
|
||||
if (swCommand.execute(player, args)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<String> tabComplete(List<SWCommand> swCommandList, String[] args) {
|
||||
public static List<String> tabComplete(List<SWCommand<? extends CommandSender>> swCommandList, String[] args) {
|
||||
List<String> strings = new ArrayList<>();
|
||||
swCommandList.forEach(swCommand -> swCommand.tabComplete(args).ifPresent(strings::addAll));
|
||||
swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(args)));
|
||||
return strings;
|
||||
}
|
||||
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren