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..635b868
--- /dev/null
+++ b/SpigotCore_Main/src/de/steamwar/command/Argument.java
@@ -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 .
+ */
+
+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 {
+
+ private static final Set> numbers = new HashSet<>();
+ static {
+ numbers.add(Integer.class);
+ numbers.add(Long.class);
+ numbers.add(Double.class);
+ numbers.add(Float.class);
+ }
+
+ public static final Argument INT = new Argument<>(Integer::parseInt, integer -> true);
+ public static final Argument DOUBLE = new Argument<>(Double::parseDouble, d -> true);
+ public static final Argument STRING = new Argument<>(s -> s, string -> true);
+
+ public static final Argument PLAYER = new Argument<>(Bukkit::getPlayer, Objects::nonNull, () -> Bukkit.getOnlinePlayers().stream().map(Player::getName).toArray(String[]::new));
+ public static final Argument 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 USER = new Argument<>(SteamwarUser::get, Objects::nonNull, PLAYER.tabCompletes);
+
+ private BiFunction mapper;
+ private Predicate constraint;
+ private Function tabCompletes;
+
+ public Argument(Function mapper, Predicate constraint, String... tabCompletes) {
+ this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes);
+ }
+
+ public Argument(Function mapper, Predicate 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 mapper, Predicate constraint, Supplier tabCompletes) {
+ this((s, player) -> mapper.apply(s), constraint, s -> tabCompletes.get());
+ }
+
+ public Argument(Function mapper, Predicate constraint, Function tabCompletes) {
+ this((s, player) -> mapper.apply(s), constraint, tabCompletes);
+ }
+
+ public Argument(BiFunction mapper, Predicate constraint, Function tabCompletes) {
+ this.mapper = mapper;
+ this.constraint = constraint;
+ this.tabCompletes = tabCompletes;
+ }
+
+ public Optional 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> 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();
+ }
+ }
+
+}
diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.java
new file mode 100644
index 0000000..5db8a44
--- /dev/null
+++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentMap.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.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 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(",")) + "}";
+ }
+
+}
diff --git a/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java
new file mode 100644
index 0000000..afd763d
--- /dev/null
+++ b/SpigotCore_Main/src/de/steamwar/command/ArgumentUtils.java
@@ -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 .
+ */
+
+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 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 String[] tabCompletes(Stream stream, Predicate predicate) {
+ return stream.filter(predicate).map(value -> value + "").toArray(String[]::new);
+ }
+
+ public static Argument of(String... arguments) {
+ return new Argument<>(s -> s, string -> contains(arguments, string, String::equals), arguments);
+ }
+
+ public static Argument of(String[] commands, String[] tabCompletes) {
+ return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
+ }
+
+ public static Argument ofIgnoreCase(String... arguments) {
+ return new Argument<>(s -> s, string -> contains(arguments, string, String::equals), arguments);
+ }
+
+ public static Argument ofIgnoreCase(String[] commands, String[] tabCompletes) {
+ return new Argument<>(s -> s, string -> contains(commands, string, String::equals), s -> supply(s, tabCompletes, commands));
+ }
+
+ public static Argument between(int minValue, int maxValue, int... tabValues) {
+ Predicate predicate = i -> i >= minValue && i <= maxValue;
+ return new Argument<>(Integer::parseInt, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
+ }
+
+ public static Argument between(double minValue, double maxValue, double... tabValues) {
+ Predicate predicate = d -> d >= minValue && d <= maxValue;
+ return new Argument<>(Double::parseDouble, predicate, tabCompletes(Arrays.stream(tabValues).boxed(), predicate));
+ }
+
+ public static > Argument ofEnum(Class 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);
+ }
+
+}
diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java
new file mode 100644
index 0000000..0faa1db
--- /dev/null
+++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java
@@ -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 .
+ */
+
+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 {
+
+ 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 executor;
+ private boolean lastArgRepeatable = false;
+
+ public SWCommand(BiConsumer executor, Argument>... arguments) {
+ this.executor = executor;
+ this.arguments = arguments;
+ }
+
+ public SWCommand(BiConsumer 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 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 boolean execute(List> swCommandList, T sender, String[] args) {
+ for (SWCommand swCommand : swCommandList) {
+ if (swCommand.execute(sender, args)) return true;
+ }
+ return false;
+ }
+
+ public static List tabComplete(List> swCommandList, T sender, String[] args) {
+ List strings = new ArrayList<>();
+ swCommandList.forEach(swCommand -> strings.addAll(swCommand.tabComplete(sender, args)));
+ return strings;
+ }
+
+ public static void register(List> commandList, String plugin, String name, Predicate 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 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;
+ }
+ });
+ }
+
+}