12
0

WIP: CommandFramework #84

Geschlossen
YoyoNow möchte 53 Commits von CommandFramework nach master mergen
4 geänderte Dateien mit 390 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,120 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
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;
import java.util.stream.Collectors;
public class Argument<T> {
private static final Set<Class<?>> numbers = new HashSet<>();
static {
numbers.add(Integer.class);
numbers.add(Long.class);
numbers.add(Double.class);
numbers.add(Float.class);
}
public static final Argument<Integer> INT = new Argument<>(Integer::parseInt, integer -> true);
public static final Argument<Double> DOUBLE = new Argument<>(Double::parseDouble, d -> true);
public static final Argument<String> STRING = new Argument<>(s -> s, string -> true);
public static final Argument<Player> PLAYER = new Argument<>(Bukkit::getPlayer, Objects::nonNull, () -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new));
public static final Argument<GameMode> GAMEMODE = new Argument<>(s -> {
switch (s.toLowerCase()) {
case "creative": case "c": case "1":
return GameMode.CREATIVE;
case "survival": case "s": case "0":
return GameMode.SURVIVAL;
case "spectator": case "sp": case "3":
return GameMode.SPECTATOR;
case "adventure": case "a": case "2":
return GameMode.ADVENTURE;
}
return null;
}, Objects::nonNull, GameMode.class);
public static final Argument<SteamwarUser> USER = new Argument<>(SteamwarUser::get, Objects::nonNull, PLAYER.tabCompletes);
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((s, player) -> mapper.apply(s), constraint, s -> tabCompletes);
}
public Argument(Function<String, T> mapper, Predicate<T> constraint, Class<? extends Enum<?>> tabCompletes) {
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((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, CommandSender sender) {
try {
T argumentMapped = mapper.apply(s, sender);
if (constraint.test(argumentMapped)) return Optional.ofNullable(argumentMapped);
} catch (Exception e) {
// Ignored
}
return Optional.empty();
}
public Optional<List<String>> tabCompleteSupplier(String s, CommandSender sender) {
try {
if (!s.isEmpty()) {
// Check if mappable
T argumentMapped = mapper.apply(s, sender);
// Check number constraints if needed
if (numbers.contains(argumentMapped.getClass()) && !constraint.test(argumentMapped)) return Optional.empty();
}
} catch (Exception e) {
// Ignored
}
try {
return Optional.of(Arrays.stream(tabCompletes.apply(s)).filter(t -> t.startsWith(s)).collect(Collectors.toList()));
} catch (Exception e) {
return Optional.empty();
}
}
}

Datei anzeigen

@ -0,0 +1,47 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package de.steamwar.command;
import java.util.Arrays;
import java.util.stream.Collectors;
public class ArgumentMap {
private Object[] objects;
public ArgumentMap(Object[] objects) {
this.objects = objects;
}
public int length() {
return objects.length;
}
public <T> T get(int index) {
return (T)objects[index];
}
@Override
public String toString() {
return "ArgumentMap{" + Arrays.stream(objects).map(o -> o.getClass().getSimpleName() + "=" + o.toString()).collect(Collectors.joining(",")) + "}";
}
}

Datei anzeigen

@ -0,0 +1,78 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class ArgumentUtils {
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;
return false;
}
private static String[] supply(String s, String[] empty, String[] notEmpty) {
if (s.isEmpty()) return empty;
return notEmpty;
}
private static <T> String[] tabCompletes(Stream<T> stream, Predicate<T> predicate) {
return stream.filter(predicate).map(value -> value + "").toArray(String[]::new);
}
public static Argument<String> of(String... arguments) {
return new Argument<>(s -> s, string -> contains(arguments, string, String::equals), arguments);
}
public static Argument<String> of(String[] commands, String[] tabCompletes) {
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
}
public static Argument<String> ofIgnoreCase(String... arguments) {
return new Argument<>(s -> s, string -> contains(arguments, string, String::equals), arguments);
}
public static Argument<String> ofIgnoreCase(String[] commands, String[] tabCompletes) {
return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
}
public static Argument<Integer> between(int minValue, int maxValue, int... tabValues) {
Predicate<Integer> predicate = i -> i >= minValue && i <= maxValue;
return new Argument<>(Integer::parseInt, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
}
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));
}
public static <T extends Enum<T>> Argument<T> ofEnum(Class<T> enumClass) {
String[] strings = Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).toArray(String[]::new);
return new Argument<>((s, sender) -> Enum.valueOf(enumClass, s), Objects::nonNull, s -> strings);
}
}

Datei anzeigen

@ -0,0 +1,145 @@
/*
*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
public class SWCommand<T extends CommandSender> {
private static final CommandMap commandMap;
static {
try {
final Field commandMapField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
commandMapField.setAccessible(true);
commandMap = (CommandMap) commandMapField.get(Bukkit.getServer());
} catch (NoSuchFieldException | IllegalAccessException exception) {
Bukkit.shutdown();
throw new SecurityException("Oh shit. Commands cannot not register.", exception);
}
}
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 < (lastArgRepeatable ? arguments.length - 1 : arguments.length); 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], sender);
if (!optional.isPresent()) return false;
lastArg[i] = optional.get();
}
objects[objects.length - 1] = lastArg;
}
executor.accept(sender, new ArgumentMap(objects));
return true;
}
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], sender).isPresent()) return new ArrayList<>();
}
if (lastArgRepeatable) {
for (int i = arguments.length; i < args.length; i++) {
if (!arguments[arguments.length - 1].valueSupplier(args[i], sender).isPresent())
return new ArrayList<>();
}
}
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) {
for (SWCommand<T> swCommand : swCommandList) {
if (swCommand.execute(sender, args)) return true;
}
return false;
}
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(sender, args)));
return strings;
}
public static <T extends CommandSender> void register(List<SWCommand<T>> commandList, String plugin, String name, Predicate<T> permissionPredicate, String... aliases) {
commandMap.register(plugin, new Command(name, "", "/" + name, Arrays.asList(aliases)) {
@Override
public boolean execute(CommandSender sender, String alias, String[] args) {
if (!isInstance(sender)) return false;
T t = (T) sender;
if (!permissionPredicate.test(t)) return false;
SWCommand.execute(commandList, t, args);
return true;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
if (!isInstance(sender)) new ArrayList<>();
return SWCommand.tabComplete(commandList, (T) sender, args);
}
private boolean isInstance(CommandSender sender) {
try {
if (Class.forName(getClass().getGenericInterfaces()[0].getTypeName()).isInstance(sender)) {
return true;
}
} catch (ClassNotFoundException e) {
// Ignored
}
return false;
}
});
}
}