SteamWar/SpigotCore
Archiviert
13
0
Add SWCommand
Dieser Commit ist enthalten in:
jojo 2020-12-30 20:24:48 +01:00
Ursprung 8ea8fb6e7b
Commit 0b389f2f99
2 geänderte Dateien mit 26 neuen und 19 gelöschten Zeilen

Datei anzeigen

@ -20,14 +20,15 @@
package de.steamwar.command;
import de.steamwar.core.Core;
import de.steamwar.sql.Schematic;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
@ -66,34 +67,40 @@ public class Argument<T> {
return null;
}, Objects::nonNull, GameMode.class);
public static final Argument<SteamwarUser> USER = new Argument<>(SteamwarUser::get, Objects::nonNull, PLAYER.tabCompletes);
// MAKE mapper a BiFunction<String, Player, T>
// public static final Argument<Schematic> SCHEMATIC = new Argument<>(s -> Schematic.getSchemFromDB(s, null), Objects::nonNull, "");
public static final Argument<Schematic> SCHEMATIC = new Argument<>((s, sender) -> {
if (sender instanceof Player) return Schematic.getSchemFromDB(s, ((Player) sender).getUniqueId());
return null;
}, Objects::nonNull, s -> new String[]{""});
private Function<String, T> mapper;
private BiFunction<String, CommandSender, T> mapper;
private Predicate<T> constraint;
private Function<String, String[]> tabCompletes;
public Argument(Function<String, T> mapper, Predicate<T> constraint, String... tabCompletes) {
this(mapper, constraint, s -> tabCompletes);
this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes);
}
public Argument(Function<String, T> mapper, Predicate<T> constraint, Class<? extends Enum<?>> tabCompletes) {
this(mapper, constraint, s -> Arrays.stream(tabCompletes.getEnumConstants()).map(e -> e.name().toLowerCase()).toArray(String[]::new));
this((s, player) -> mapper.apply(s), constraint, s -> Arrays.stream(tabCompletes.getEnumConstants()).map(e -> e.name().toLowerCase()).toArray(String[]::new));
}
public Argument(Function<String, T> mapper, Predicate<T> constraint, Supplier<String[]> tabCompletes) {
this(mapper, constraint, s -> tabCompletes.get());
this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes.get());
}
public Argument(Function<String, T> mapper, Predicate<T> constraint, Function<String, String[]> tabCompletes) {
this((s, player) -> mapper.apply(s), constraint, tabCompletes);
}
public Argument(BiFunction<String, CommandSender, T> mapper, Predicate<T> constraint, Function<String, String[]> tabCompletes) {
this.mapper = mapper;
this.constraint = constraint;
this.tabCompletes = tabCompletes;
}
public Optional<T> valueSupplier(String s) {
public Optional<T> valueSupplier(String s, CommandSender sender) {
try {
T argumentMapped = mapper.apply(s);
T argumentMapped = mapper.apply(s, sender);
if (constraint.test(argumentMapped)) return Optional.ofNullable(argumentMapped);
} catch (Exception e) {
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
@ -101,11 +108,11 @@ public class Argument<T> {
return Optional.empty();
}
public Optional<List<String>> tabCompleteSupplier(String s) {
public Optional<List<String>> tabCompleteSupplier(String s, CommandSender sender) {
try {
if (!s.isEmpty()) {
// Check if mappable
T argumentMapped = mapper.apply(s);
T argumentMapped = mapper.apply(s, sender);
// Check number constraints if needed
if (numbers.contains(argumentMapped.getClass()) && !constraint.test(argumentMapped)) return Optional.empty();
}

Datei anzeigen

@ -47,14 +47,14 @@ public class SWCommand<T extends CommandSender> {
if (args.length != arguments.length) return false;
Object[] objects = new Object[args.length];
for (int i = 0; i < (lastArgRepeatable ? arguments.length - 1 : arguments.length); i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
Optional<?> optional = arguments[i].valueSupplier(args[i], sender);
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]);
Optional<?> optional = arguments[i].valueSupplier(args[i], sender);
if (!optional.isPresent()) return false;
lastArg[i] = optional.get();
}
@ -64,19 +64,19 @@ public class SWCommand<T extends CommandSender> {
return true;
}
public List<String> tabComplete(String[] args) {
public List<String> tabComplete(T sender, String[] args) {
if (args.length > arguments.length && !lastArgRepeatable) {
return new ArrayList<>();
}
for (int i = 0; i < Math.min(args.length - 1, arguments.length - 1); i++) {
if (!arguments[i].valueSupplier(args[i]).isPresent()) return new ArrayList<>();
if (!arguments[i].valueSupplier(args[i], sender).isPresent()) return new ArrayList<>();
}
if (lastArgRepeatable) {
for (int i = arguments.length; i < args.length; i++) {
if (!arguments[arguments.length - 1].valueSupplier(args[i]).isPresent()) return new ArrayList<>();
if (!arguments[arguments.length - 1].valueSupplier(args[i], sender).isPresent()) return new ArrayList<>();
}
}
return arguments[arguments.length - 1].tabCompleteSupplier(args[args.length - 1]).orElseGet(ArrayList::new);
return arguments[arguments.length - 1].tabCompleteSupplier(args[args.length - 1], sender).orElseGet(ArrayList::new);
}
public static <T extends CommandSender> boolean execute(List<SWCommand<T>> swCommandList, T sender, String[] args) {
@ -86,9 +86,9 @@ public class SWCommand<T extends CommandSender> {
return false;
}
public static <T extends CommandSender> List<String> tabComplete(List<SWCommand<T>> swCommandList, String[] args) {
public static <T extends CommandSender> List<String> tabComplete(List<SWCommand<T>> swCommandList, T sender, String[] args) {
List<String> strings = new ArrayList<>();
swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(args)));
swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(sender, args)));
return strings;
}