PlotPermissions/src/main/java/com/alttd/commands/CommandManager.java
2022-01-31 02:49:57 +01:00

117 lines
4.4 KiB
Java

package com.alttd.commands;
import com.alttd.PlotPermissions;
import com.alttd.commands.subcommands.Add;
import com.alttd.commands.subcommands.Help;
import com.alttd.commands.subcommands.Info;
import com.alttd.commands.subcommands.Remove;
import com.alttd.config.Config;
import com.alttd.util.Logger;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.minimessage.template.TemplateResolver;
import org.bukkit.command.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CommandManager implements CommandExecutor, TabExecutor {
private final List<SubCommand> subCommands;
private final MiniMessage miniMessage = MiniMessage.miniMessage();
public CommandManager() {
PlotPermissions plotPermissions = PlotPermissions.getInstance();
PluginCommand command = plotPermissions.getCommand("plotp");
if (command == null) {
subCommands = null;
Logger.severe("Unable to find PlotPermissions command.");
return;
}
command.setExecutor(this);
command.setTabCompleter(this);
subCommands = Arrays.asList(
new Help(this),
new Info(),
new Add(),
new Remove()
);
}
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
if (!commandSender.hasPermission("plotpermissions.use")) {
commandSender.sendMiniMessage(Config.NO_PERMISSION, null);
return true;
}
if (args.length == 0) {
getHelpMessage(commandSender);
return true;
}
Optional<SubCommand> first = subCommands.stream()
.filter(subCommand -> subCommand.getName().equalsIgnoreCase(args[0]))
.findFirst();
if (first.isEmpty()) {
commandSender.sendMessage(getHelpMessage(commandSender));
return true;
}
SubCommand subCommand = first.get();
if (commandSender.hasPermission(subCommand.getPermission()))
return subCommand.onCommand(commandSender, args);
else
commandSender.sendMiniMessage(Config.NO_PERMISSION, null);
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]))
.collect(Collectors.toList())
);
} else {
SubCommand subCommand = getSubCommand(args[0]);
if (subCommand != null && commandSender.hasPermission(subCommand.getPermission()))
res.addAll(subCommand.getTabComplete(commandSender, args));
}
return res;
}
public List<SubCommand> getSubCommands() {
return subCommands;
}
private SubCommand getSubCommand(String cmdName) {
return subCommands.stream()
.filter(subCommand -> subCommand.getName().equals(cmdName))
.findFirst()
.orElse(null);
}
public Component getHelpMessage(CommandSender commandSender) {
StringBuilder stringBuilder = new StringBuilder();
subCommands.stream()
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
.forEach(subCommand -> stringBuilder.append(subCommand.getHelpMessage()).append("\n"));
if (stringBuilder.length() != 0)
stringBuilder.replace(stringBuilder.length() - 1, stringBuilder.length(), "");
return miniMessage.deserialize(Config.HELP_MESSAGE_WRAPPER, TemplateResolver.resolving(
Template.template("commands", miniMessage.deserialize(stringBuilder.toString()))
));
}
}