From 7186c4de6c1975b34230791794339dc5685ae862 Mon Sep 17 00:00:00 2001 From: Adrian Date: Fri, 28 Apr 2023 17:53:37 -0500 Subject: [PATCH 1/2] Improved Velocity Module --- .../common/commands/CWCommand.java | 21 +- CommandWhitelistVelocity/pom.xml | 10 + .../velocity/CommandWhitelistVelocity.java | 65 ++--- .../velocity/command/VelocityMainCommand.java | 231 +++++++++++------- 4 files changed, 201 insertions(+), 126 deletions(-) diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/commands/CWCommand.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/commands/CWCommand.java index 02a5ab2..94416bc 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/commands/CWCommand.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/commands/CWCommand.java @@ -3,6 +3,7 @@ package eu.endermite.commandwhitelist.common.commands; import eu.endermite.commandwhitelist.common.CWGroup; import eu.endermite.commandwhitelist.common.ConfigCache; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.minimessage.MiniMessage; import java.util.ArrayList; @@ -31,20 +32,20 @@ public class CWCommand { } public static Component helpComponent(String baseCommand, boolean showReloadCommand, boolean showAdminCommands) { - Component component = miniMessage.deserialize("CommandWhitelist by YouHaveTrouble") - .append(Component.newline()); - component = component.append(Component.text(baseCommand + " help").color(NamedTextColor.AQUA).append(Component.text(" - Displays this message").color(NamedTextColor.BLUE))); + final TextComponent.Builder builder = Component.text(); + builder.append(miniMessage.deserialize("CommandWhitelist by YouHaveTrouble"), Component.newline()); + builder.append(Component.text(baseCommand + " help", NamedTextColor.AQUA), Component.text(" - Displays this message", NamedTextColor.BLUE)); if (showReloadCommand) { - component = component.append(Component.newline()); - component = component.append(Component.text(baseCommand + " reload").color(NamedTextColor.AQUA).append(Component.text(" - Reloads plugin configuration").color(NamedTextColor.BLUE))); + builder.append(Component.newline()); + builder.append(Component.text(baseCommand + " reload", NamedTextColor.AQUA), Component.text(" - Reloads plugin configuration", NamedTextColor.BLUE)); } if (showAdminCommands) { - component = component.append(Component.newline()); - component = component.append(Component.text(baseCommand + " add ").color(NamedTextColor.AQUA).append(Component.text(" - Add a command to selected permission group").color(NamedTextColor.BLUE))); - component = component.append(Component.newline()); - component = component.append(Component.text(baseCommand + " remove ").color(NamedTextColor.AQUA).append(Component.text(" - Removes a command from selected permission group").color(NamedTextColor.BLUE))); + builder.append(Component.newline()); + builder.append(Component.text(baseCommand + " add ", NamedTextColor.AQUA), Component.text(" - Add a command to selected permission group", NamedTextColor.BLUE)); + builder.append(Component.newline()); + builder.append(Component.text(baseCommand + " remove ", NamedTextColor.AQUA), Component.text(" - Removes a command from selected permission group", NamedTextColor.BLUE)); } - return component; + return builder.build(); } public enum CommandType { diff --git a/CommandWhitelistVelocity/pom.xml b/CommandWhitelistVelocity/pom.xml index 955f888..9a5f1a0 100644 --- a/CommandWhitelistVelocity/pom.xml +++ b/CommandWhitelistVelocity/pom.xml @@ -50,6 +50,16 @@ eu.endermite.bstats + + + net.kyori:adventure-api + net.kyori:adventure-key + net.kyori:examination-api + net.kyori:examination-string + org.jetbrains:annotations + org.intellij.lang:annotations + + diff --git a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java index 9e35f86..cd6e04d 100644 --- a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java +++ b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java @@ -1,6 +1,7 @@ package eu.endermite.commandwhitelist.velocity; -import com.velocitypowered.api.command.CommandMeta; +import com.google.inject.Injector; +import com.mojang.brigadier.Command; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; @@ -15,13 +16,11 @@ import eu.endermite.commandwhitelist.common.CommandUtil; import eu.endermite.commandwhitelist.common.ConfigCache; import eu.endermite.commandwhitelist.common.commands.CWCommand; import eu.endermite.commandwhitelist.velocity.command.VelocityMainCommand; -import net.kyori.adventure.identity.Identity; import org.bstats.charts.SimplePie; import org.bstats.velocity.Metrics; import org.slf4j.Logger; import javax.inject.Inject; -import java.io.File; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -30,51 +29,57 @@ import java.util.Map; public class CommandWhitelistVelocity { - private static CommandWhitelistVelocity plugin; - private static ProxyServer server; - private static ConfigCache configCache; - private static Path folder; - private static Logger logger; + private final ProxyServer server; + private ConfigCache configCache; + private final Path folder; + private final Logger logger; private final Metrics.Factory metricsFactory; + private final Injector injector; @Inject - public CommandWhitelistVelocity(ProxyServer server, Logger logger, @DataDirectory final Path folder, Metrics.Factory metricsFactory) { - CommandWhitelistVelocity.server = server; - CommandWhitelistVelocity.folder = folder; - CommandWhitelistVelocity.plugin = this; - CommandWhitelistVelocity.logger = logger; + public CommandWhitelistVelocity( + ProxyServer server, + Logger logger, + @DataDirectory final Path folder, + Metrics.Factory metricsFactory, + Injector injector + ) { + this.server = server; + this.folder = folder; + this.logger = logger; this.metricsFactory = metricsFactory; - + this.injector = injector; } - private static void reloadConfig() { + private void reloadConfig() { if (configCache == null) - configCache = new ConfigCache(new File(String.valueOf(folder), "config.yml"), false, logger); + configCache = new ConfigCache(folder.resolve("config.yml").toFile(), false, logger); else configCache.reloadConfig(); } - public static void reloadConfig(CommandSource source) { - server.getScheduler().buildTask(plugin, () -> { + public int reloadConfig(CommandSource source) { + server.getScheduler().buildTask(this, () -> { reloadConfig(); - source.sendMessage(Identity.nil(), CWCommand.miniMessage.deserialize(getConfigCache().prefix + getConfigCache().config_reloaded)); + source.sendMessage(CWCommand.miniMessage.deserialize(getConfigCache().prefix + getConfigCache().config_reloaded)); }).schedule(); + return Command.SINGLE_SUCCESS; } @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { reloadConfig(); - CommandMeta commandMeta = server.getCommandManager().metaBuilder("vcw").build(); - server.getCommandManager().register(commandMeta, new VelocityMainCommand()); + injector.getInstance(VelocityMainCommand.class).register(); Metrics metrics = metricsFactory.make(this, 8704); metrics.addCustomChart(new SimplePie("proxy", () -> "Velocity")); } @Subscribe + @SuppressWarnings("UnstableApiUsage") public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) { Player player = event.getPlayer(); if (player.hasPermission(CWPermission.BYPASS.permission())) return; - HashSet allowedCommands = CommandWhitelistVelocity.getCommands(player); + HashSet allowedCommands = getCommands(player); event.getRootNode().getChildren().removeIf((commandNode) -> server.getCommandManager().hasCommand(commandNode.getName()) && !allowedCommands.contains(commandNode.getName()) @@ -82,7 +87,7 @@ public class CommandWhitelistVelocity { } @Subscribe - public void onUserCommandExecuteEvent(com.velocitypowered.api.event.command.CommandExecuteEvent event) { + public void onUserCommandExecuteEvent(CommandExecuteEvent event) { if (!(event.getCommandSource() instanceof Player)) return; Player player = (Player) event.getCommandSource(); @@ -91,25 +96,21 @@ public class CommandWhitelistVelocity { // Workaround for velocity executing "/ command" as valid command String command = event.getCommand().trim(); - HashSet allowedCommands = CommandWhitelistVelocity.getCommands(player); + HashSet allowedCommands = getCommands(player); String label = CommandUtil.getCommandLabel(command); if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label)) event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); } - public static ConfigCache getConfigCache() { + public ConfigCache getConfigCache() { return configCache; } - public static Path getConfigPath() { - return folder; - } - /** * @param player Velocity Player * @return commands available to the player */ - public static HashSet getCommands(Player player) { + public HashSet getCommands(Player player) { HashMap groups = configCache.getGroupList(); HashSet commandList = new HashSet<>(); for (Map.Entry s : groups.entrySet()) { @@ -126,7 +127,7 @@ public class CommandWhitelistVelocity { * @param player Velocity Player * @return subcommands unavailable for the player */ - public static HashSet getSuggestions(Player player, HashMap groups) { + public HashSet getSuggestions(Player player, HashMap groups) { HashSet suggestionList = new HashSet<>(); for (Map.Entry s : groups.entrySet()) { if (s.getKey().equalsIgnoreCase("default")) @@ -137,7 +138,7 @@ public class CommandWhitelistVelocity { return suggestionList; } - public static ArrayList getServerCommands() { + public ArrayList getServerCommands() { return new ArrayList<>(server.getCommandManager().getAliases()); } diff --git a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java index 7e035f5..2be1d35 100644 --- a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java +++ b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java @@ -1,104 +1,167 @@ package eu.endermite.commandwhitelist.velocity.command; +import com.google.inject.Inject; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.builder.RequiredArgumentBuilder; +import com.mojang.brigadier.context.ParsedArgument; +import com.mojang.brigadier.tree.LiteralCommandNode; +import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandManager; import com.velocitypowered.api.command.CommandSource; -import com.velocitypowered.api.command.SimpleCommand; +import com.velocitypowered.api.permission.Tristate; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import eu.endermite.commandwhitelist.common.CWGroup; import eu.endermite.commandwhitelist.common.CWPermission; import eu.endermite.commandwhitelist.common.CommandUtil; +import eu.endermite.commandwhitelist.common.ConfigCache; import eu.endermite.commandwhitelist.common.commands.CWCommand; import eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity; import net.kyori.adventure.text.Component; -import java.io.File; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.CompletableFuture; -public class VelocityMainCommand implements SimpleCommand { +public final class VelocityMainCommand { + @Inject + private CommandManager commandManager; + @Inject + private CommandWhitelistVelocity plugin; + @Inject + @DataDirectory + private Path dataDirectory; - @Override - public void execute(final Invocation invocation) { - CommandSource sender = invocation.source(); - String[] args = invocation.arguments(); - String label = invocation.alias(); + public void register() { + LiteralCommandNode node = LiteralArgumentBuilder + .literal("vcw") + .requires(src -> src.getPermissionValue("commandwhitelist.command") != Tristate.FALSE) + .executes(ctx -> { + CommandSource source = ctx.getSource(); + source.sendMessage(CWCommand.helpComponent("vcw", source.hasPermission(CWPermission.RELOAD.permission()), source.hasPermission(CWPermission.ADMIN.permission()))); + return Command.SINGLE_SUCCESS; + }) + .then(LiteralArgumentBuilder.literal("reload") + .requires(src -> src.hasPermission(CWPermission.RELOAD.permission())) + .executes(ctx -> plugin.reloadConfig(ctx.getSource())) + ) + .then(LiteralArgumentBuilder.literal("add") + .requires(src -> src.hasPermission(CWPermission.ADMIN.permission())) + .then(RequiredArgumentBuilder.argument("group", StringArgumentType.word()) + .suggests((ctx, builder) -> { + groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group")) + .forEach(builder::suggest); + return builder.buildFuture(); + }) + .then(RequiredArgumentBuilder.argument("command", StringArgumentType.word()) + .suggests((ctx, builder) -> { + CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class)); + if (group == null) return builder.buildFuture(); - if (args.length == 0) { - sender.sendMessage(CWCommand.helpComponent(label, sender.hasPermission(CWPermission.RELOAD.permission()), sender.hasPermission(CWPermission.ADMIN.permission()))); - return; - } + String stringArgument = stringArgument(ctx.getArguments().get("command")); + for (String cmd : plugin.getServerCommands()) { + if (cmd.charAt(0) == '/') + cmd = cmd.substring(1); + if (cmd.indexOf(':') != -1) { + String[] cmdSplit = cmd.split(":"); + if (cmdSplit.length < 2) continue; + cmd = cmdSplit[1]; + } + if (group.getCommands().contains(cmd)) continue; + if (stringArgument == null || cmd.startsWith(stringArgument)) + builder.suggest(cmd); + } + return builder.buildFuture(); + }) + .executes(ctx -> { + CommandSource source = ctx.getSource(); + ConfigCache configCache = plugin.getConfigCache(); + String arg1 = ctx.getArgument("group", String.class); + String arg2 = ctx.getArgument("command", String.class); - try { - CWCommand.CommandType commandType = CWCommand.CommandType.valueOf(args[0].toUpperCase()); - switch (commandType) { - case RELOAD: - if (!sender.hasPermission(CWPermission.RELOAD.permission())) { - sender.sendMessage(CWCommand.miniMessage.deserialize(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().no_permission)); - return; - } - CommandWhitelistVelocity.reloadConfig(sender); - return; - case ADD: - if (!sender.hasPermission(CWPermission.ADMIN.permission())) { - sender.sendMessage(CWCommand.miniMessage.deserialize(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().no_permission)); - return; - } - if (args.length == 3) { - if (CWCommand.addToWhitelist(CommandWhitelistVelocity.getConfigCache(), args[2], args[1])) - sender.sendMessage(CWCommand.miniMessage.deserialize(String.format(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().added_to_whitelist, args[2], args[1]))); - else - sender.sendMessage(CWCommand.miniMessage.deserialize(String.format(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().group_doesnt_exist, args[1]))); - } else - sender.sendMessage(Component.text("/" + label + " add ")); - return; - case REMOVE: - if (!sender.hasPermission(CWPermission.ADMIN.permission())) { - sender.sendMessage(CWCommand.miniMessage.deserialize(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().no_permission)); - return; - } - if (args.length == 3) { - if (CWCommand.removeFromWhitelist(CommandWhitelistVelocity.getConfigCache(), args[2], args[1])) - sender.sendMessage(CWCommand.miniMessage.deserialize(String.format(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().removed_from_whitelist, args[2], args[1]))); - else - sender.sendMessage(CWCommand.miniMessage.deserialize(String.format(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().group_doesnt_exist, args[1]))); - } else - sender.sendMessage(Component.text("/" + label + " remove ")); - return; - case DUMP: - if (!sender.hasPermission(CWPermission.ADMIN.permission())) { - sender.sendMessage(CWCommand.miniMessage.deserialize(CommandWhitelistVelocity.getConfigCache().prefix + CommandWhitelistVelocity.getConfigCache().no_permission)); - return; - } - sender.sendMessage(Component.text("Dumping all available commands to a file...")); - if (CommandUtil.dumpAllBukkitCommands(CommandWhitelistVelocity.getServerCommands(), new File(String.valueOf(CommandWhitelistVelocity.getConfigPath()), "command_dump.yml"))) { - sender.sendMessage(Component.text("Commands dumped to command_dump.yml")); - } else { - sender.sendMessage(Component.text("Failed to save the file.")); - } - return; - case HELP: - default: - sender.sendMessage(CWCommand.helpComponent(label, sender.hasPermission(CWPermission.RELOAD.permission()), sender.hasPermission(CWPermission.ADMIN.permission()))); - } + if (CWCommand.addToWhitelist(configCache, arg2, arg1)) + source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.added_to_whitelist, arg2, arg1))); + else + source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.group_doesnt_exist, arg1))); + return Command.SINGLE_SUCCESS; + }) + ) + ) + ) + .then(LiteralArgumentBuilder.literal("remove") + .requires(src -> src.hasPermission(CWPermission.ADMIN.permission())) + .then(RequiredArgumentBuilder.argument("group", StringArgumentType.word()) + .suggests((ctx, builder) -> { + groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group")) + .forEach(builder::suggest); + return builder.buildFuture(); + }) + .then(RequiredArgumentBuilder.argument("command", StringArgumentType.word()) + .suggests((ctx, builder) -> { + CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class)); + if (group == null) return builder.buildFuture(); - } catch (IllegalArgumentException e) { - sender.sendMessage(CWCommand.helpComponent(label, sender.hasPermission(CWPermission.RELOAD.permission()), sender.hasPermission(CWPermission.ADMIN.permission()))); - } - return; + String stringArgument = stringArgument(ctx.getArguments().get("command")); + for (String s : group.getCommands()) { + if (stringArgument == null || s.startsWith(stringArgument)) + builder.suggest(s); + } + return builder.buildFuture(); + }) + .executes(ctx -> { + CommandSource source = ctx.getSource(); + ConfigCache configCache = plugin.getConfigCache(); + String arg1 = ctx.getArgument("group", String.class); + String arg2 = ctx.getArgument("command", String.class); + + if (CWCommand.removeFromWhitelist(configCache, arg2, arg1)) + source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.removed_from_whitelist, arg2, arg1))); + else + source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.group_doesnt_exist, arg1))); + return Command.SINGLE_SUCCESS; + }) + ) + ) + ) + .then(LiteralArgumentBuilder.literal("dump") + .requires(src -> src.hasPermission(CWPermission.ADMIN.permission())) + .executes(ctx -> { + CommandSource source = ctx.getSource(); + source.sendMessage(Component.text("Dumping all available commands to a file...")); + if (CommandUtil.dumpAllBukkitCommands(plugin.getServerCommands(), dataDirectory.resolve("command_dump.yml").toFile())) { + source.sendMessage(Component.text("Commands dumped to command_dump.yml")); + } else { + source.sendMessage(Component.text("Failed to save the file.")); + } + return Command.SINGLE_SUCCESS; + }) + ) + .then(LiteralArgumentBuilder.literal("help") + .executes(ctx -> { + CommandSource source = ctx.getSource(); + source.sendMessage(CWCommand.helpComponent("cw", source.hasPermission(CWPermission.RELOAD.permission()), source.hasPermission(CWPermission.ADMIN.permission()))); + return Command.SINGLE_SUCCESS; + }) + ) + .build(); + + final BrigadierCommand command = new BrigadierCommand(node); + commandManager.register(commandManager.metaBuilder(command).plugin(plugin).build(), command); } - @Override - public CompletableFuture> suggestAsync(Invocation invocation) { - CommandSource source = invocation.source(); - String[] args = invocation.arguments(); - return CompletableFuture.supplyAsync(() -> { - List serverCommands = CommandWhitelistVelocity.getServerCommands(); - return CWCommand.commandSuggestions( - CommandWhitelistVelocity.getConfigCache(), - serverCommands, - args, - source.hasPermission(CWPermission.RELOAD.permission()), - source.hasPermission(CWPermission.ADMIN.permission()), - CWCommand.ImplementationType.VELOCITY - ); - }); + private List groupSuggestions(ConfigCache configCache, ParsedArgument argument) { + final String stringArgument = stringArgument(argument); + List groups = new ArrayList<>(); + for (String s : configCache.getGroupList().keySet()) { + if (stringArgument == null || s.startsWith(stringArgument)) + groups.add(s); + } + return groups; + } + + private String stringArgument(ParsedArgument argument) { + return argument == null + ? null : (String) argument.getResult(); } } From c36ac056d5717b5a32f1c54fe522a108a315a982 Mon Sep 17 00:00:00 2001 From: Adrian Date: Fri, 28 Apr 2023 18:25:19 -0500 Subject: [PATCH 2/2] Simplified suggestions --- .../velocity/command/VelocityMainCommand.java | 32 +++---------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java index 2be1d35..c00b953 100644 --- a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java +++ b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java @@ -5,7 +5,6 @@ import com.mojang.brigadier.Command; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder; -import com.mojang.brigadier.context.ParsedArgument; import com.mojang.brigadier.tree.LiteralCommandNode; import com.velocitypowered.api.command.BrigadierCommand; import com.velocitypowered.api.command.CommandManager; @@ -21,8 +20,6 @@ import eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity; import net.kyori.adventure.text.Component; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; public final class VelocityMainCommand { @Inject @@ -50,8 +47,7 @@ public final class VelocityMainCommand { .requires(src -> src.hasPermission(CWPermission.ADMIN.permission())) .then(RequiredArgumentBuilder.argument("group", StringArgumentType.word()) .suggests((ctx, builder) -> { - groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group")) - .forEach(builder::suggest); + plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest); return builder.buildFuture(); }) .then(RequiredArgumentBuilder.argument("command", StringArgumentType.word()) @@ -59,7 +55,6 @@ public final class VelocityMainCommand { CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class)); if (group == null) return builder.buildFuture(); - String stringArgument = stringArgument(ctx.getArguments().get("command")); for (String cmd : plugin.getServerCommands()) { if (cmd.charAt(0) == '/') cmd = cmd.substring(1); @@ -69,8 +64,7 @@ public final class VelocityMainCommand { cmd = cmdSplit[1]; } if (group.getCommands().contains(cmd)) continue; - if (stringArgument == null || cmd.startsWith(stringArgument)) - builder.suggest(cmd); + builder.suggest(cmd); } return builder.buildFuture(); }) @@ -93,8 +87,7 @@ public final class VelocityMainCommand { .requires(src -> src.hasPermission(CWPermission.ADMIN.permission())) .then(RequiredArgumentBuilder.argument("group", StringArgumentType.word()) .suggests((ctx, builder) -> { - groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group")) - .forEach(builder::suggest); + plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest); return builder.buildFuture(); }) .then(RequiredArgumentBuilder.argument("command", StringArgumentType.word()) @@ -102,10 +95,8 @@ public final class VelocityMainCommand { CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class)); if (group == null) return builder.buildFuture(); - String stringArgument = stringArgument(ctx.getArguments().get("command")); for (String s : group.getCommands()) { - if (stringArgument == null || s.startsWith(stringArgument)) - builder.suggest(s); + builder.suggest(s); } return builder.buildFuture(); }) @@ -149,19 +140,4 @@ public final class VelocityMainCommand { final BrigadierCommand command = new BrigadierCommand(node); commandManager.register(commandManager.metaBuilder(command).plugin(plugin).build(), command); } - - private List groupSuggestions(ConfigCache configCache, ParsedArgument argument) { - final String stringArgument = stringArgument(argument); - List groups = new ArrayList<>(); - for (String s : configCache.getGroupList().keySet()) { - if (stringArgument == null || s.startsWith(stringArgument)) - groups.add(s); - } - return groups; - } - - private String stringArgument(ParsedArgument argument) { - return argument == null - ? null : (String) argument.getResult(); - } }