From 67dc554f14d6ca8a9f6fa99ca300e207b4253fb2 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 27 Dec 2020 21:30:50 +0100 Subject: [PATCH] Add Argument Add Executor --- .../src/de/steamwar/command/Argument.java | 62 +++++++++++ ...CommandArgument.java => ArgumentType.java} | 25 ++--- .../{SWCommandExecutor.java => Executor.java} | 8 +- .../src/de/steamwar/command/SWCommand.java | 100 ++++-------------- .../command/SWCommandArgumentBuilder.java | 80 -------------- 5 files changed, 94 insertions(+), 181 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/Argument.java rename SpigotCore_Main/src/de/steamwar/command/{SWCommandArgument.java => ArgumentType.java} (50%) rename SpigotCore_Main/src/de/steamwar/command/{SWCommandExecutor.java => Executor.java} (85%) delete mode 100644 SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java new file mode 100644 index 0000000..3f26bc7 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -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 . + * / + */ + +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 { + + public static final Argument PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer); + + private ArgumentType argumentType; + private Predicate constraint; + private Function valueMapper; + + public Argument(ArgumentType argumentType, Predicate constraint) { + this(argumentType, constraint, o -> o); + } + + public Argument(ArgumentType argumentType, Predicate constraint, Function 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(); + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java similarity index 50% rename from SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java rename to SpigotCore_Main/src/de/steamwar/command/ArgumentType.java index 2ae8122..f24cc1c 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentType.java @@ -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 { -public class SWCommandArgument { + public static final ArgumentType INT = new ArgumentType<>(Integer::parseInt); + public static final ArgumentType LONG = new ArgumentType<>(Long::parseLong); + public static final ArgumentType FLOAT = new ArgumentType<>(Float::parseFloat); + public static final ArgumentType DOUBLE = new ArgumentType<>(Double::parseDouble); + public static final ArgumentType STRING = new ArgumentType<>((s) -> s); - final SWCommandExecutor swCommandExecutor; - final BiPredicate commandPredicate; - final String[] commandArguments; + Function mapper; - final BiPredicate tabCompletePredicate; - final String[] tabCompleteArguments; - - public SWCommandArgument(SWCommandExecutor swCommandExecutor, BiPredicate commandPredicate, String[] commandArguments, BiPredicate tabCompletePredicate, String[] tabCompleteArguments) { - this.swCommandExecutor = swCommandExecutor; - this.commandPredicate = commandPredicate; - this.commandArguments = commandArguments; - this.tabCompletePredicate = tabCompletePredicate; - this.tabCompleteArguments = tabCompleteArguments; + private ArgumentType(Function mapper) { + this.mapper = mapper; } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java b/SpigotCore_Main/src/de/steamwar/command/Executor.java similarity index 85% rename from SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java rename to SpigotCore_Main/src/de/steamwar/command/Executor.java index 22ac389..0b42f79 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java +++ b/SpigotCore_Main/src/de/steamwar/command/Executor.java @@ -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); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 32449be..d17f4b3 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -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 permissionCheck = (player) -> true; - private Consumer permissionMessage = (player) -> {}; - - private List 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 permissionCheck, Consumer 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 onTabComplete(CommandSender sender, Command command, String label, String[] args) { - if (!(sender instanceof Player)) { - return new ArrayList<>(); + public Optional execute(Player player, String[] args) { + if (!checkValidity(args)) { + return Optional.empty(); } - Player player = (Player) sender; - - List 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 manageList(List 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)); } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java deleted file mode 100644 index 6784db4..0000000 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java +++ /dev/null @@ -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 . - * / - */ - -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 commandPredicate; - private String[] commandArguments; - - private BiPredicate 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 commandPredicate) { - this.commandPredicate = commandPredicate; - return this; - } - - public SWCommandArgumentBuilder setCommandArguments(String[] commandArguments) { - this.commandArguments = commandArguments; - return this; - } - - public SWCommandArgumentBuilder setTabCompletePredicate(BiPredicate tabCompletePredicate) { - this.tabCompletePredicate = tabCompletePredicate; - return this; - } - - public SWCommandArgumentBuilder setTabCompleteArguments(String[] tabCompleteArguments) { - this.tabCompleteArguments = tabCompleteArguments; - return this; - } -}