diff --git a/SpigotCore_Main/src/de/steamwar/command/Argument.java b/SpigotCore_Main/src/de/steamwar/command/Argument.java index dadae09..0a6b106 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Argument.java +++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java @@ -43,18 +43,10 @@ public class Argument { public static final Argument PLAYER = new Argument<>(ArgumentType.STRING, string -> Bukkit.getPlayer(string) == null, Bukkit::getPlayer, () -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new)); public static final Argument GAMEMODE = new Argument<>(ArgumentType.STRING, - string -> string.equalsIgnoreCase("creative") || - string.equalsIgnoreCase("c") || - string.equalsIgnoreCase("1") || - string.equalsIgnoreCase("survival") || - string.equalsIgnoreCase("s") || - string.equalsIgnoreCase("0") || - string.equalsIgnoreCase("spectator") || - string.equalsIgnoreCase("sp") || - string.equalsIgnoreCase("3") || - string.equalsIgnoreCase("adventure") || - string.equalsIgnoreCase("a") || - string.equalsIgnoreCase("2"), + string -> string.equalsIgnoreCase("creative") || string.equalsIgnoreCase("c") || string.equalsIgnoreCase("1") || + string.equalsIgnoreCase("survival") || string.equalsIgnoreCase("s") || string.equalsIgnoreCase("0") || + string.equalsIgnoreCase("spectator") || string.equalsIgnoreCase("sp") || string.equalsIgnoreCase("3") || + string.equalsIgnoreCase("adventure") || string.equalsIgnoreCase("a") || string.equalsIgnoreCase("2"), s -> { if (s.equalsIgnoreCase("creative") || s.equalsIgnoreCase("c") || s.equalsIgnoreCase("1")) return GameMode.CREATIVE; if (s.equalsIgnoreCase("spectator") || s.equalsIgnoreCase("sp") || s.equalsIgnoreCase("3")) return GameMode.SPECTATOR; @@ -135,73 +127,6 @@ public class Argument { } } - public static class ArgumentBuilder { - - private List[]> options = new ArrayList<>(); - - public ArgumentBuilder(int min, int max, Argument... arguments) { - if (min < 0) min = 0; - if (max > arguments.length) max = arguments.length; - for (int i = min; i <= max; i++) { - generate(arguments, i, 0, new Argument[i]); - } - } - - public void generate(int n, Argument[] elements) { - if (n == 1 || n == 0) { - options.add(Arrays.copyOf(elements, elements.length)); - return; - } - for (int i = 0; i < n - 1; i++) { - generate(n - 1, elements); - if (n % 2 == 0) { - swap(elements, i, n - 1); - } else { - swap(elements, 0, n - 1); - } - } - generate(n - 1, elements); - } - - private void swap(Argument[] elements, int a, int b) { - Argument tmp = elements[a]; - elements[a] = elements[b]; - elements[b] = tmp; - } - - private void generate(Argument[] arguments, int length, int startPosition, Argument[] result) { - if (length == 0) { - Set> argumentSet = new HashSet<>(Arrays.asList(result)); - if (argumentSet.size() != result.length) return; - generate(result.length, Arrays.copyOf(result, result.length)); - return; - } - for (int i = startPosition; i <= arguments.length - length; i++) { - result[result.length - length] = arguments[i]; - generate(arguments, length - 1, startPosition + 1, result); - } - } - - public List[]> apply(Argument... arguments) { - List[]> args = new ArrayList<>(); - args.add(arguments); - return apply(args); - } - - public List[]> apply(List[]> arguments) { - List[]> results = new ArrayList<>(); - arguments.forEach(args -> { - for (Argument[] option : options) { - Argument[] result = Arrays.copyOf(args, args.length + option.length); - System.arraycopy(option, 0, result, args.length, option.length); - results.add(result); - } - }); - return results; - } - - } - @Override public String toString() { return "Argument{" + diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java index 5ced8bc..1d3e51f 100644 --- a/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java +++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java @@ -69,4 +69,5 @@ public class ArgumentMap { st.append("}"); return st.toString(); } + } diff --git a/SpigotCore_Main/src/de/steamwar/command/Executor.java b/SpigotCore_Main/src/de/steamwar/command/Executor.java index f752951..26d6bdc 100644 --- a/SpigotCore_Main/src/de/steamwar/command/Executor.java +++ b/SpigotCore_Main/src/de/steamwar/command/Executor.java @@ -25,5 +25,5 @@ import org.bukkit.entity.Player; @FunctionalInterface public interface Executor { - boolean execute(Player player, ArgumentMap argumentMap); + void execute(Player player, ArgumentMap argumentMap); } diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 4d73c31..95d0d27 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -36,17 +36,18 @@ public class SWCommand { this.executor = executor; } - public Optional execute(Player player, String[] args) { + public boolean execute(Player player, String[] args) { if (args.length != arguments.length) { - return Optional.empty(); + return false; } Object[] objects = new Object[args.length]; for (int i = 0; i < args.length; i++) { Optional optional = arguments[i].valueSupplier(args[i]); - if (!optional.isPresent()) return Optional.empty(); + if (!optional.isPresent()) return false; objects[i] = optional.get(); } - return Optional.of(executor.execute(player, new ArgumentMap(objects))); + executor.execute(player, new ArgumentMap(objects)); + return true; } public Optional> tabComplete(String[] args) { diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java index 24f24c7..d3e2590 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandBundle.java @@ -48,12 +48,11 @@ public class SWCommandBundle { return this; } - public Optional execute(Player player, String[] args) { + public boolean execute(Player player, String[] args) { for (SWCommand swCommand : swCommandList) { - Optional optionalBoolean = swCommand.execute(player, args); - if (optionalBoolean.isPresent()) return optionalBoolean; + if (swCommand.execute(player, args)) return true; } - return Optional.empty(); + return false; } public List tabComplete(String[] args) { diff --git a/SpigotCore_Main/src/de/steamwar/commandn/Argument.java b/SpigotCore_Main/src/de/steamwar/commandn/Argument.java new file mode 100644 index 0000000..9a574f3 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/commandn/Argument.java @@ -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 . + * / + */ + +package de.steamwar.commandn; + +import org.bukkit.command.CommandSender; + +import java.util.List; + +public interface Argument { + + T parse(CommandSender sender, String arg); + List tabComplete(CommandSender sender, String arg) throws InvalidArgumentException; + + abstract class IntArgument implements Argument { + @Override + public Integer parse(CommandSender sender, String arg) { + return Integer.parseInt(arg); + } + } + + abstract class DoubleArgument implements Argument { + @Override + public Double parse(CommandSender sender, String arg) { + return Double.parseDouble(arg); + } + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/commandn/InvalidArgumentException.java b/SpigotCore_Main/src/de/steamwar/commandn/InvalidArgumentException.java new file mode 100644 index 0000000..4c0fd78 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/commandn/InvalidArgumentException.java @@ -0,0 +1,26 @@ +/* + * + * 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.commandn; + +public class InvalidArgumentException extends Exception { + +}