From a42f546e4d5c278a20cdafc0120243f69a7d942d Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Sun, 14 Jul 2024 14:21:06 +0200 Subject: [PATCH] add subcommand execution blocker to velocity --- .../velocity/CommandWhitelistVelocity.java | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) 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 acf6c5e..5dbb98f 100644 --- a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java +++ b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java @@ -4,9 +4,11 @@ import com.google.inject.Inject; import com.google.inject.Injector; import com.mojang.brigadier.Command; import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.command.PlayerAvailableCommandsEvent; +import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; @@ -22,10 +24,7 @@ import org.bstats.velocity.Metrics; import org.slf4j.Logger; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; +import java.util.*; public class CommandWhitelistVelocity { @@ -74,7 +73,7 @@ public class CommandWhitelistVelocity { metrics.addCustomChart(new SimplePie("proxy", () -> "Velocity")); } - @Subscribe + @Subscribe(order = PostOrder.LAST) @SuppressWarnings("UnstableApiUsage") public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) { Player player = event.getPlayer(); @@ -95,11 +94,45 @@ public class CommandWhitelistVelocity { // Workaround for velocity executing "/ command" as valid command String command = event.getCommand().trim(); + if (command.startsWith("/")) command = command.substring(1); HashSet allowedCommands = getCommands(player); String label = CommandUtil.getCommandLabel(command); - if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label)) + if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label)) { event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); + return; + } + + HashSet bannedSubCommands = getSuggestions(player); + + for (String bannedSubCommand : bannedSubCommands) { + if (command.startsWith(bannedSubCommand)) { + event.setResult(CommandExecuteEvent.CommandResult.denied()); + player.sendMessage(CWCommand.miniMessage.deserialize(configCache.prefix + configCache.subcommand_denied)); + return; + } + } + } + + /** + * THIS IS FOR CLIENTS ON 1.12 AND BELOW, NOT GUARANTEED TO WORK, IF IT DOESN'T, PR OF GTFO + */ + @Subscribe + public void onUserTabCompleteEvent(TabCompleteEvent event) { + Player player = event.getPlayer(); + if (player.hasPermission(CWPermission.BYPASS.permission())) return; + String buffer = event.getPartialMessage(); + + if (event.getSuggestions().isEmpty()) return; + + List newSuggestions = CommandUtil.filterSuggestions( + buffer, + event.getSuggestions(), + getSuggestions(player) + ); + + event.getSuggestions().clear(); + event.getSuggestions().addAll(newSuggestions); } public ConfigCache getConfigCache() {