From 2b7edeec0c02c392e2b9a605357cfbac034a0a67 Mon Sep 17 00:00:00 2001 From: jojo Date: Sun, 27 Dec 2020 17:14:43 +0100 Subject: [PATCH] Add simple SWCommand Add SWCommandArgument Add SWCommandArgumentBuilder Add SWCommandExecutor --- .../src/de/steamwar/command/SWCommand.java | 111 ++++++++++++++++++ .../steamwar/command/SWCommandArgument.java | 45 +++++++ .../command/SWCommandArgumentBuilder.java | 80 +++++++++++++ .../steamwar/command/SWCommandExecutor.java | 31 +++++ 4 files changed, 267 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/command/SWCommand.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java create mode 100644 SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java 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..9893144 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -0,0 +1,111 @@ +/* + * + * 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.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Predicate; + +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() { + + } + + 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); + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return false; + } + Player player = (Player) sender; + + if (!permissionCheck.test(player)) { + permissionMessage.accept(player); + 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; + } + + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + return new ArrayList<>(); + } + Player player = (Player) sender; + + List tabComplete = new ArrayList<>(); + for (SWCommandArgument swCommandArgument : swCommandArgumentList) { + if (swCommandArgument.tabCompletePredicate.test(player, args)) { + tabComplete.addAll(Arrays.asList(swCommandArgument.tabCompleteArguments)); + } + } + 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; + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java new file mode 100644 index 0000000..2ae8122 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java @@ -0,0 +1,45 @@ +/* + * + * 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 SWCommandArgument { + + final SWCommandExecutor swCommandExecutor; + final BiPredicate commandPredicate; + final String[] commandArguments; + + 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; + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java new file mode 100644 index 0000000..6784db4 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandArgumentBuilder.java @@ -0,0 +1,80 @@ +/* + * + * 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; + } +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java new file mode 100644 index 0000000..22ac389 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java @@ -0,0 +1,31 @@ +/* + * + * 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.command.Command; +import org.bukkit.entity.Player; + +public interface SWCommandExecutor { + + boolean execute(Player sender, Command command, String label, String[] args); + +}