2021-02-16 21:58:31 +01:00
|
|
|
package net.knarcraft.stargate.command;
|
|
|
|
|
2021-11-09 20:58:55 +01:00
|
|
|
import org.apache.commons.lang.ArrayUtils;
|
2021-02-16 21:58:31 +01:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.CommandSender;
|
|
|
|
import org.bukkit.command.TabCompleter;
|
2021-11-09 02:04:59 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2021-02-16 21:58:31 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-02-19 12:07:34 +01:00
|
|
|
/**
|
|
|
|
* This is the tab completer for the /stargate (/sg) command
|
|
|
|
*/
|
2021-02-16 21:58:31 +01:00
|
|
|
public class StarGateTabCompleter implements TabCompleter {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @Nullable List<String> onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command,
|
2021-09-20 13:56:30 +02:00
|
|
|
@NotNull String s, @NotNull String[] args) {
|
2021-11-09 20:58:55 +01:00
|
|
|
if (args.length == 1) {
|
|
|
|
List<String> commands = getAvailableCommands(commandSender);
|
|
|
|
List<String> matchingCommands = new ArrayList<>();
|
|
|
|
for (String availableCommand : commands) {
|
|
|
|
if (availableCommand.startsWith(args[0])) {
|
|
|
|
matchingCommands.add(availableCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matchingCommands;
|
|
|
|
} else if (args.length > 1 && args[0].equalsIgnoreCase("config")) {
|
|
|
|
String[] subArgs = (String[]) ArrayUtils.remove(args, 0);
|
|
|
|
return new ConfigTabCompleter().onTabComplete(commandSender, command, s, subArgs);
|
|
|
|
} else {
|
|
|
|
return new ArrayList<>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the available commands
|
|
|
|
*
|
|
|
|
* @param commandSender <p>The command sender to get available commands for</p>
|
|
|
|
* @return <p>The commands available to the command sender</p>
|
|
|
|
*/
|
|
|
|
private List<String> getAvailableCommands(CommandSender commandSender) {
|
2021-02-16 21:58:31 +01:00
|
|
|
List<String> commands = new ArrayList<>();
|
|
|
|
commands.add("about");
|
2021-11-09 02:04:59 +01:00
|
|
|
if (!(commandSender instanceof Player player) || player.hasPermission("stargate.admin.reload")) {
|
|
|
|
commands.add("reload");
|
|
|
|
}
|
2021-11-09 21:16:53 +01:00
|
|
|
if (!(commandSender instanceof Player player) || player.hasPermission("stargate.admin.config")) {
|
2021-11-09 02:04:59 +01:00
|
|
|
commands.add("config");
|
|
|
|
}
|
2021-11-09 20:58:55 +01:00
|
|
|
return commands;
|
2021-02-16 21:58:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|