work on command completions

This commit is contained in:
YouHaveTrouble
2021-07-06 21:31:51 +02:00
parent 050e3e7af4
commit 017011fad0
4 changed files with 30 additions and 22 deletions
@@ -21,6 +21,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
public class CommandWhitelistBukkit extends JavaPlugin { public class CommandWhitelistBukkit extends JavaPlugin {
@@ -36,16 +36,16 @@ public class CWCommand {
Component component = MiniMessage.markdown().parse("<rainbow><bold>CommandWhitelist by YouHaveTrouble") Component component = MiniMessage.markdown().parse("<rainbow><bold>CommandWhitelist by YouHaveTrouble")
.append(Component.newline()); .append(Component.newline());
component = component.append(Component.text("Hover over the command to see what it does!").color(NamedTextColor.AQUA)).decoration(TextDecoration.BOLD, false).append(Component.newline()); component = component.append(Component.text("Hover over the command to see what it does!").color(NamedTextColor.AQUA)).decoration(TextDecoration.BOLD, false).append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" help").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Displays this message")))); component = component.append(Component.text("/" + baseCommand + " help").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Displays this message"))));
if (showReloadCommand) { if (showReloadCommand) {
component = component.append(Component.newline()); component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" reload").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Reloads plugin configuration")))); component = component.append(Component.text("/" + baseCommand + " reload").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Reloads plugin configuration"))));
} }
if (showAdminCommands) { if (showAdminCommands) {
component = component.append(Component.newline()); component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" add <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Add a command to selected permission group")))); component = component.append(Component.text("/" + baseCommand + " add <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Add a command to selected permission group"))));
component = component.append(Component.newline()); component = component.append(Component.newline());
component = component.append(Component.text("/"+baseCommand+" remove <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Removes a command from selected permission group")))); component = component.append(Component.text("/" + baseCommand + " remove <group> <command>").color(NamedTextColor.AQUA).hoverEvent(HoverEvent.showText(Component.text("Removes a command from selected permission group"))));
} }
return component; return component;
} }
@@ -77,27 +77,27 @@ public class CWCommand {
case 3: case 3:
if (args[0].equalsIgnoreCase("remove")) { if (args[0].equalsIgnoreCase("remove")) {
if (!adminPerm) return list; if (!adminPerm) return list;
try { CWGroup group = config.getGroupList().get(args[1]);
for (String s : config.getGroupList().get(args[1]).getCommands()) { if (group == null) return list;
if (s.startsWith(args[2])) for (String s : group.getCommands()) {
list.add(s); if (s.startsWith(args[2]))
} list.add(s);
} catch (NullPointerException ignored) {
} }
return list; return list;
} }
if (args[0].equalsIgnoreCase("add")) { if (args[0].equalsIgnoreCase("add")) {
if (!adminPerm) return list; if (!adminPerm) return list;
CWGroup group = config.getGroupList().get(args[1]);
if (group == null) return list;
for (String cmd : serverCommands) { for (String cmd : serverCommands) {
if (!cmd.startsWith("/")) continue; if (cmd.startsWith("/"))
cmd = cmd.substring(1);
if (cmd.contains(":")) { if (cmd.contains(":")) {
String[] cmdSplit = cmd.split(":"); String[] cmdSplit = cmd.split(":");
if (cmdSplit.length < 2) continue; if (cmdSplit.length < 2) continue;
cmd = cmd.split(":")[1]; cmd = cmdSplit[1];
} }
cmd = cmd.replace("/", ""); if (group.getCommands().contains(cmd)) continue;
if (config.getGroupList().get(args[1]) == null) continue;
if (config.getGroupList().get(args[1]).getCommands().contains(cmd)) continue;
if (cmd.startsWith(args[2])) if (cmd.startsWith(args[2]))
list.add(cmd); list.add(cmd);
} }
@@ -77,12 +77,11 @@ public class VelocityMainCommand implements SimpleCommand {
CommandSource source = invocation.source(); CommandSource source = invocation.source();
String[] args = invocation.arguments(); String[] args = invocation.arguments();
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
List<String> suggestions = new ArrayList<>(); List<String> serverCommands = new ArrayList<>();
if (args.length == 1) { //TODO find out how to get all registered commands
if (source.hasPermission(CWPermission.RELOAD.permission()) && "reload".startsWith(args[0]))
suggestions.add("reload"); // This is probably very broken now
} return CWCommand.commandSuggestions(CommandWhitelistVelocity.getConfigCache(), serverCommands, args, source.hasPermission(CWPermission.RELOAD.permission()), source.hasPermission(CWPermission.ADMIN.permission()));
return suggestions;
}); });
} }
} }
@@ -11,6 +11,10 @@ import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Command;
import net.md_5.bungee.api.plugin.TabExecutor; import net.md_5.bungee.api.plugin.TabExecutor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class BungeeMainCommand extends Command implements TabExecutor { public class BungeeMainCommand extends Command implements TabExecutor {
public BungeeMainCommand(String name) { public BungeeMainCommand(String name) {
@@ -77,6 +81,10 @@ public class BungeeMainCommand extends Command implements TabExecutor {
@Override @Override
public Iterable<String> onTabComplete(CommandSender sender, String[] args) { public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
return null; List<String> serverCommands = new ArrayList<>();
for (Map.Entry<String, Command> command : CommandWhitelistWaterfall.getPlugin().getProxy().getPluginManager().getCommands()) {
serverCommands.add(command.getValue().getName());
}
return CWCommand.commandSuggestions(CommandWhitelistWaterfall.getConfigCache(), serverCommands, args, sender.hasPermission(CWPermission.RELOAD.permission()),sender.hasPermission(CWPermission.ADMIN.permission()));
} }
} }