| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package me.lethunderhawk.bazaarflux.util.command;
- import org.bukkit.command.CommandSender;
- import java.util.*;
- import java.util.function.BiConsumer;
- import java.util.function.BiFunction;
- public class CommandNode {
- private final String name;
- private final String description;
- private BiConsumer<CommandSender, String[]> executor;
- private BiFunction<CommandSender, String[], List<String>> tabCompleter;
- private Map<String, CommandNode> subCommands = new HashMap<>();
- private CommandNode parent;
- public CommandNode(String name, String description, BiConsumer<CommandSender, String[]> executor) {
- this.name = name;
- this.description = description;
- this.executor = executor;
- }
- public CommandNode registerSubCommand(String name, String description, BiConsumer<CommandSender, String[]> executor) {
- CommandNode subCommand = new CommandNode(name, description, executor);
- subCommand.parent = this;
- subCommands.put(name.toLowerCase(), subCommand);
- return subCommand;
- }
- public void addSubCommand(CommandNode subCommand) {
- subCommand.parent = this;
- subCommands.put(subCommand.getName().toLowerCase(), subCommand);
- }
- public void addSubCommands(CommandNode... subCommandNodes) {
- for(CommandNode subCommand : subCommandNodes) {
- subCommand.parent = this;
- subCommands.put(subCommand.getName().toLowerCase(), subCommand);
- }
- }
- public CommandNode getSubCommand(String name) {
- return subCommands.get(name.toLowerCase());
- }
- public boolean hasSubCommand(String name) {
- return subCommands.containsKey(name.toLowerCase());
- }
- public Collection<CommandNode> getSubCommands() {
- return subCommands.values();
- }
- public List<String> getSubCommandNames() {
- return new ArrayList<>(subCommands.keySet());
- }
- public void setTabCompleter(BiFunction<CommandSender, String[], List<String>> tabCompleter) {
- this.tabCompleter = tabCompleter;
- }
- // Getters
- public String getName() { return name; }
- public String getDescription() { return description; }
- public BiConsumer<CommandSender, String[]> getExecutor() { return executor; }
- public BiFunction<CommandSender, String[], List<String>> getTabCompleter() { return tabCompleter; }
- public CommandNode getParent() { return parent; }
- public void setExecutor(BiConsumer<CommandSender, String[]> executor) {
- this.executor = executor;
- }
- }
|