SteamWar/SpigotCore
Archiviert
13
0
Add Executor
Dieser Commit ist enthalten in:
jojo 2020-12-27 21:30:50 +01:00
Ursprung 10934058cc
Commit 67dc554f14
5 geänderte Dateien mit 94 neuen und 181 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,62 @@
/*
*
* 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 java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
public class Argument<T> {
public static final Argument<String> PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer);
private ArgumentType<T> argumentType;
private Predicate<T> constraint;
private Function<T, ?> valueMapper;
public Argument(ArgumentType<T> argumentType, Predicate<T> constraint) {
this(argumentType, constraint, o -> o);
}
public <M> Argument(ArgumentType<T> argumentType, Predicate<T> constraint, Function<T, M> valueMapper) {
this.argumentType = argumentType;
this.constraint = constraint;
this.valueMapper = valueMapper;
}
public Optional<?> valueSupplier(String s) {
try {
T argumentMapped = argumentType.mapper.apply(s);
if (constraint.test(argumentMapped)) {
return Optional.ofNullable(valueMapper.apply(argumentMapped));
}
} catch (NumberFormatException e) {
return Optional.empty();
} catch (Exception e) {
throw new SecurityException(e);
}
return Optional.empty();
}
}

Datei anzeigen

@ -21,25 +21,20 @@
package de.steamwar.command;
import org.bukkit.entity.Player;
import java.util.function.Function;
import java.util.function.BiPredicate;
public class ArgumentType<T> {
public class SWCommandArgument {
public static final ArgumentType<Integer> INT = new ArgumentType<>(Integer::parseInt);
public static final ArgumentType<Long> LONG = new ArgumentType<>(Long::parseLong);
public static final ArgumentType<Float> FLOAT = new ArgumentType<>(Float::parseFloat);
public static final ArgumentType<Double> DOUBLE = new ArgumentType<>(Double::parseDouble);
public static final ArgumentType<String> STRING = new ArgumentType<>((s) -> s);
final SWCommandExecutor swCommandExecutor;
final BiPredicate<Player, String[]> commandPredicate;
final String[] commandArguments;
Function<String, T> mapper;
final BiPredicate<Player, String[]> tabCompletePredicate;
final String[] tabCompleteArguments;
public SWCommandArgument(SWCommandExecutor swCommandExecutor, BiPredicate<Player, String[]> commandPredicate, String[] commandArguments, BiPredicate<Player, String[]> tabCompletePredicate, String[] tabCompleteArguments) {
this.swCommandExecutor = swCommandExecutor;
this.commandPredicate = commandPredicate;
this.commandArguments = commandArguments;
this.tabCompletePredicate = tabCompletePredicate;
this.tabCompleteArguments = tabCompleteArguments;
private ArgumentType(Function<String, T> mapper) {
this.mapper = mapper;
}
}

Datei anzeigen

@ -21,11 +21,9 @@
package de.steamwar.command;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
public interface SWCommandExecutor {
boolean execute(Player sender, Command command, String label, String[] args);
@FunctionalInterface
public interface Executor {
boolean execute(Player player, Object[] args);
}

Datei anzeigen

@ -21,101 +21,39 @@
package de.steamwar.command;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.Optional;
public class SWCommand implements CommandExecutor, TabCompleter {
public class SWCommand {
private Predicate<Player> permissionCheck = (player) -> true;
private Consumer<Player> permissionMessage = (player) -> {};
private List<SWCommandArgument> swCommandArgumentList = new ArrayList<>();
private SWCommandExecutor helpExecutor = (sender, command, label, args) -> false;
public SWCommand() {
private Argument<?>[] arguments;
private Executor executor;
public SWCommand(Executor executor, Argument<?>... arguments) {
this.arguments = arguments;
}
public SWCommand(SWCommandArgument... swCommandArguments) {
swCommandArgumentList = Arrays.asList(swCommandArguments);
}
public SWCommand add(SWCommandArgument swCommandArgument) {
swCommandArgumentList.add(swCommandArgument);
return this;
}
public SWCommand setPermissionCheck(Predicate<Player> permissionCheck, Consumer<Player> permissionMessage) {
if (permissionCheck == null || permissionMessage == null) {
throw new IllegalStateException();
}
this.permissionCheck = permissionCheck;
this.permissionMessage = permissionMessage;
return this;
}
public boolean help(Player sender, Command command, String label, String[] args) {
return helpExecutor.execute(sender, command, label, args);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
public boolean checkValidity(String[] args) {
if (args.length != arguments.length) {
return false;
}
Player player = (Player) sender;
if (!permissionCheck.test(player)) {
permissionMessage.accept(player);
return false;
for (int i = 0; i < args.length; i++) {
Optional<?> optional = arguments[i].valueSupplier(args[i]);
if (optional.isEmpty()) return false;
}
for (SWCommandArgument swCommandArgument : swCommandArgumentList) {
if (swCommandArgument.commandPredicate.test(player, args)) {
return swCommandArgument.swCommandExecutor.execute(player, command, label, args);
}
}
help(player, command, label, args);
return false;
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
return new ArrayList<>();
public Optional<Boolean> execute(Player player, String[] args) {
if (!checkValidity(args)) {
return Optional.empty();
}
Player player = (Player) sender;
List<String> tabComplete = new ArrayList<>();
for (SWCommandArgument swCommandArgument : swCommandArgumentList) {
if (swCommandArgument.tabCompletePredicate.test(player, args)) {
tabComplete.addAll(Arrays.asList(swCommandArgument.tabCompleteArguments));
}
Object[] objects = new Object[args.length];
for (int i = 0; i < args.length; i++) {
objects[i] = arguments[i].valueSupplier(args[i]).orElseThrow();
}
return manageList(tabComplete, args);
}
private List<String> manageList(List<String> strings, String[] args) {
if (strings == null) return new ArrayList<>();
for (int i = strings.size() - 1; i >= 0; i--) {
if (!strings.get(i).startsWith(args[args.length - 1])) strings.remove(i);
}
return strings;
}
public void registerCommand(JavaPlugin plugin, String command) {
plugin.getCommand(command).setExecutor(this);
plugin.getCommand(command).setTabCompleter(this);
return Optional.of(executor.execute(player, objects));
}
}

Datei anzeigen

@ -1,80 +0,0 @@
/*
*
* 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.entity.Player;
import java.util.function.BiPredicate;
public class SWCommandArgumentBuilder {
private SWCommandExecutor swCommandExecutor = (sender, command, label, args) -> false;
private BiPredicate<Player, String[]> commandPredicate;
private String[] commandArguments;
private BiPredicate<Player, String[]> tabCompletePredicate = (player, args) -> true;
private String[] tabCompleteArguments;
public SWCommandArgument build() {
if (swCommandExecutor == null) {
throw new IllegalStateException();
}
if (commandPredicate == null) {
throw new IllegalStateException();
}
if (commandArguments == null) {
throw new IllegalStateException();
}
if (tabCompletePredicate == null) {
throw new IllegalStateException();
}
if (tabCompleteArguments == null) {
throw new IllegalStateException();
}
return new SWCommandArgument(swCommandExecutor, commandPredicate, commandArguments, tabCompletePredicate, tabCompleteArguments);
}
public SWCommandArgumentBuilder setSwCommandExecutor(SWCommandExecutor swCommandExecutor) {
this.swCommandExecutor = swCommandExecutor;
return this;
}
public SWCommandArgumentBuilder setCommandPredicate(BiPredicate<Player, String[]> commandPredicate) {
this.commandPredicate = commandPredicate;
return this;
}
public SWCommandArgumentBuilder setCommandArguments(String[] commandArguments) {
this.commandArguments = commandArguments;
return this;
}
public SWCommandArgumentBuilder setTabCompletePredicate(BiPredicate<Player, String[]> tabCompletePredicate) {
this.tabCompletePredicate = tabCompletePredicate;
return this;
}
public SWCommandArgumentBuilder setTabCompleteArguments(String[] tabCompleteArguments) {
this.tabCompleteArguments = tabCompleteArguments;
return this;
}
}