86 lines
3.1 KiB
Java
86 lines
3.1 KiB
Java
package com.alttd.fishingevent.commands;
|
|
|
|
import com.alttd.fishingevent.FishingEvent;
|
|
import com.alttd.fishingevent.config.Messages;
|
|
import com.alttd.fishingevent.util.Logger;
|
|
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.stream.Collectors;
|
|
|
|
public class FishCommand implements CommandExecutor, TabExecutor {
|
|
private final List<SubCommand> subCommands;
|
|
|
|
public FishCommand(FishingEvent fishingEvent, Logger logger) {
|
|
|
|
PluginCommand command = fishingEvent.getCommand("fish");
|
|
if (command == null) {
|
|
subCommands = null;
|
|
logger.severe("Unable to find transfer command.");
|
|
return;
|
|
}
|
|
command.setExecutor(this);
|
|
command.setTabCompleter(this);
|
|
|
|
subCommands = Arrays.asList(
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String cmd, @NotNull String[] args) {
|
|
if (args.length == 0) {
|
|
commandSender.sendMiniMessage(Messages.HELP.HELP_MESSAGE_WRAPPER.replaceAll("<commands>", subCommands.stream()
|
|
.filter(subCommand -> commandSender.hasPermission(subCommand.getPermission()))
|
|
.map(SubCommand::getHelpMessage)
|
|
.collect(Collectors.joining("\n"))), null);
|
|
return true;
|
|
}
|
|
|
|
SubCommand subCommand = getSubCommand(args[0]);
|
|
if (subCommand == null)
|
|
return false;
|
|
|
|
if (!commandSender.hasPermission(subCommand.getPermission())) {
|
|
commandSender.sendMiniMessage(Messages.GENERIC.NO_PERMISSION, null);
|
|
return true;
|
|
}
|
|
|
|
return subCommand.onCommand(commandSender, args);
|
|
}
|
|
|
|
@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;
|
|
}
|
|
|
|
public List<SubCommand> getSubCommands() {
|
|
return subCommands;
|
|
}
|
|
|
|
private SubCommand getSubCommand(String cmdName) {
|
|
return subCommands.stream()
|
|
.filter(subCommand -> subCommand.getName().equals(cmdName))
|
|
.findFirst()
|
|
.orElse(null);
|
|
}
|
|
} |