110 lines
4.3 KiB
Java
110 lines
4.3 KiB
Java
package com.alttd.hunger_games.commands;
|
|
|
|
import com.alttd.hunger_games.Main;
|
|
import com.alttd.hunger_games.commands.subcommands.Register;
|
|
import com.alttd.hunger_games.commands.subcommands.Reload;
|
|
import com.alttd.hunger_games.commands.subcommands.RoundState;
|
|
import com.alttd.hunger_games.commands.subcommands.StartRound;
|
|
import com.alttd.hunger_games.config.Messages;
|
|
import com.alttd.hunger_games.services.PlayerService;
|
|
import com.alttd.hunger_games.services.Round;
|
|
import com.alttd.hunger_games.services.RoundService;
|
|
import lombok.Getter;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import org.bukkit.command.*;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Slf4j
|
|
@Getter
|
|
public class BaseCommand implements CommandExecutor, TabExecutor {
|
|
private final List<SubCommand> subCommands;
|
|
|
|
public BaseCommand(Main main, RoundService roundService, PlayerService playerService, Round round) {
|
|
PluginCommand command = main.getCommand("hungergames");
|
|
if (command == null) {
|
|
subCommands = null;
|
|
log.error("Unable to find hungergames command.");
|
|
return;
|
|
}
|
|
command.setExecutor(this);
|
|
command.setTabCompleter(this);
|
|
command.setAliases(List.of("hg"));
|
|
|
|
subCommands = new ArrayList<>(List.of(
|
|
new Reload(main),
|
|
new RoundState(roundService),
|
|
new Register(playerService),
|
|
new StartRound(round, roundService)
|
|
));
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
|
if (args.length == 0) {
|
|
commandSender.sendRichMessage(Messages.HELP.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", subCommands.stream()
|
|
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
|
.map(SubCommand::getHelpMessage)
|
|
.collect(Collectors.joining("\n"))));
|
|
return true;
|
|
}
|
|
|
|
SubCommand subCommand = getSubCommand(args[0]);
|
|
if (subCommand == null) {
|
|
return false;
|
|
}
|
|
|
|
if (!commandSender.hasPermission(subCommand.getPermission())) {
|
|
commandSender.sendRichMessage(Messages.GENERIC.NO_PERMISSION, Placeholder.parsed("permission", subCommand.getPermission()));
|
|
return true;
|
|
}
|
|
|
|
boolean executedCorrectly = subCommand.onCommand(commandSender, args);
|
|
if (!executedCorrectly) {
|
|
commandSender.sendRichMessage(subCommand.getHelpMessage());
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
|
List<String> res = new ArrayList<>();
|
|
|
|
if (args.length <= 1) {
|
|
res.addAll(subCommands.stream()
|
|
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
|
.map(SubCommand::getName)
|
|
.filter(name -> args.length == 0 || name.startsWith(args[0]))
|
|
.toList()
|
|
);
|
|
} else {
|
|
SubCommand subCommand = getSubCommand(args[0]);
|
|
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission())) {
|
|
res.addAll(subCommand.getTabComplete(commandSender, args).stream()
|
|
.filter(str -> str.toLowerCase().startsWith(args[args.length - 1].toLowerCase()))
|
|
.toList());
|
|
}
|
|
}
|
|
return res;
|
|
}
|
|
|
|
private SubCommand getSubCommand(String cmdName) {
|
|
return subCommands.stream()
|
|
.filter(subCommand -> subCommand.getName().equals(cmdName))
|
|
.findFirst()
|
|
.orElse(null);
|
|
}
|
|
|
|
public void addSubCommand(SubCommand subCommand) {
|
|
if (subCommands.stream().anyMatch(entry -> entry.getName().equalsIgnoreCase(subCommand.getName()))) {
|
|
return;
|
|
}
|
|
subCommands.add(subCommand);
|
|
}
|
|
}
|