Skip to content

Commit a42f546

Browse files
add subcommand execution blocker to velocity
1 parent 7222b91 commit a42f546

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import com.google.inject.Injector;
55
import com.mojang.brigadier.Command;
66
import com.velocitypowered.api.command.CommandSource;
7+
import com.velocitypowered.api.event.PostOrder;
78
import com.velocitypowered.api.event.Subscribe;
89
import com.velocitypowered.api.event.command.CommandExecuteEvent;
910
import com.velocitypowered.api.event.command.PlayerAvailableCommandsEvent;
11+
import com.velocitypowered.api.event.player.TabCompleteEvent;
1012
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
1113
import com.velocitypowered.api.plugin.annotation.DataDirectory;
1214
import com.velocitypowered.api.proxy.Player;
@@ -22,10 +24,7 @@
2224
import org.slf4j.Logger;
2325

2426
import java.nio.file.Path;
25-
import java.util.ArrayList;
26-
import java.util.HashMap;
27-
import java.util.HashSet;
28-
import java.util.Map;
27+
import java.util.*;
2928

3029
public class CommandWhitelistVelocity {
3130

@@ -74,7 +73,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) {
7473
metrics.addCustomChart(new SimplePie("proxy", () -> "Velocity"));
7574
}
7675

77-
@Subscribe
76+
@Subscribe(order = PostOrder.LAST)
7877
@SuppressWarnings("UnstableApiUsage")
7978
public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) {
8079
Player player = event.getPlayer();
@@ -95,11 +94,45 @@ public void onUserCommandExecuteEvent(CommandExecuteEvent event) {
9594

9695
// Workaround for velocity executing "/ command" as valid command
9796
String command = event.getCommand().trim();
97+
if (command.startsWith("/")) command = command.substring(1);
9898

9999
HashSet<String> allowedCommands = getCommands(player);
100100
String label = CommandUtil.getCommandLabel(command);
101-
if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label))
101+
if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label)) {
102102
event.setResult(CommandExecuteEvent.CommandResult.forwardToServer());
103+
return;
104+
}
105+
106+
HashSet<String> bannedSubCommands = getSuggestions(player);
107+
108+
for (String bannedSubCommand : bannedSubCommands) {
109+
if (command.startsWith(bannedSubCommand)) {
110+
event.setResult(CommandExecuteEvent.CommandResult.denied());
111+
player.sendMessage(CWCommand.miniMessage.deserialize(configCache.prefix + configCache.subcommand_denied));
112+
return;
113+
}
114+
}
115+
}
116+
117+
/**
118+
* THIS IS FOR CLIENTS ON 1.12 AND BELOW, NOT GUARANTEED TO WORK, IF IT DOESN'T, PR OF GTFO
119+
*/
120+
@Subscribe
121+
public void onUserTabCompleteEvent(TabCompleteEvent event) {
122+
Player player = event.getPlayer();
123+
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
124+
String buffer = event.getPartialMessage();
125+
126+
if (event.getSuggestions().isEmpty()) return;
127+
128+
List<String> newSuggestions = CommandUtil.filterSuggestions(
129+
buffer,
130+
event.getSuggestions(),
131+
getSuggestions(player)
132+
);
133+
134+
event.getSuggestions().clear();
135+
event.getSuggestions().addAll(newSuggestions);
103136
}
104137

105138
public ConfigCache getConfigCache() {

0 commit comments

Comments
 (0)