Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.6 kB
5
Indexable
Never
package net.hollowrealms.util;

import net.hollowrealms.commands.SubCommand;
import net.hollowrealms.commands.Testing.subcommands.TestSubCommand;
import net.hollowrealms.duels.subcommands.DuelInvite;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class CommandManager implements TabExecutor {

    private ArrayList<SubCommand> subCommands = new ArrayList<>();

    public CommandManager() {
        subCommands.add(new TestSubCommand());
        subCommands.add(new DuelInvite());
    }
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {

        if(sender instanceof Player) {
            Player p = (Player) sender;
            if (args.length > 0 ) {
                for(int i  = 0; i < getSubCommands().size(); i++) {
                   if(args[0].equalsIgnoreCase(getSubCommands().get(i).getName())){
                       getSubCommands().get(i).execute(p, args);
                   }
                }
            } else if(args.length == 0) {

                p.sendMessage("----------------------------------");
                for(int i = 0; i < subCommands.size(); i++){
                    p.sendMessage(getSubCommands().get(i).getSyntax() + " - " + getSubCommands().get(i).getDescription());
                }
                p.sendMessage("----------------------------------");
            }

        }

        return true;
    }

    public ArrayList<SubCommand> getSubCommands() {
        return subCommands;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {

        if(args.length == 1) {
            ArrayList<String> subCommandArgs = new ArrayList<>();

            for(int i = 0; i < getSubCommands().size(); i++) {
                subCommandArgs.add(getSubCommands().get(i).getName());
            }
            return subCommandArgs;
        } else if(args.length == 2) {
            for (int i = 0; i < getSubCommands().size(); i++) {
                if (args[0].equalsIgnoreCase(getSubCommands().get(i).getName())) {
                    return getSubCommands().get(i).getSubCommandArgs((Player) sender, args);
                }
            }
        }

        return new ArrayList<>();
    }
}