Add simple SWCommand
Add SWCommandArgument Add SWCommandArgumentBuilder Add SWCommandExecutor
Dieser Commit ist enthalten in:
Ursprung
88e4b4b092
Commit
2b7edeec0c
111
SpigotCore_Main/src/de/steamwar/command/SWCommand.java
Normale Datei
111
SpigotCore_Main/src/de/steamwar/command/SWCommand.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
* /
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||||
|
if (!(sender instanceof Player)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
45
SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java
Normale Datei
45
SpigotCore_Main/src/de/steamwar/command/SWCommandArgument.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
* /
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.function.BiPredicate;
|
||||||
|
|
||||||
|
public class SWCommandArgument {
|
||||||
|
|
||||||
|
final SWCommandExecutor swCommandExecutor;
|
||||||
|
final BiPredicate<Player, String[]> commandPredicate;
|
||||||
|
final String[] commandArguments;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 <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;
|
||||||
|
}
|
||||||
|
}
|
31
SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java
Normale Datei
31
SpigotCore_Main/src/de/steamwar/command/SWCommandExecutor.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
* /
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
In neuem Issue referenzieren
Einen Benutzer sperren