Skip to content

Commit e823aa9

Browse files
added optional usage of protocollib for command filter
1 parent eb0f53f commit e823aa9

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>eu.endermite</groupId>
88
<artifactId>CommandWhitelist</artifactId>
9-
<version>1.6.0</version>
9+
<version>1.7.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>CommandWhitelist</name>

src/main/java/eu/endermite/commandwhitelist/spigot/CommandWhitelist.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import eu.endermite.commandwhitelist.spigot.command.MainCommand;
44
import eu.endermite.commandwhitelist.spigot.config.ConfigCache;
5-
import eu.endermite.commandwhitelist.spigot.listeners.LegacyPlayerTabChatCompleteListener;
6-
import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandPreProcessListener;
7-
import eu.endermite.commandwhitelist.spigot.listeners.PlayerCommandSendListener;
8-
import eu.endermite.commandwhitelist.spigot.listeners.TabCompleteBlockerListener;
5+
import eu.endermite.commandwhitelist.spigot.listeners.*;
96
import eu.endermite.commandwhitelist.spigot.metrics.BukkitMetrics;
107
import org.bukkit.Bukkit;
118
import org.bukkit.ChatColor;
129
import org.bukkit.command.CommandSender;
1310
import org.bukkit.entity.Player;
11+
import org.bukkit.plugin.Plugin;
1412
import org.bukkit.plugin.java.JavaPlugin;
1513

1614
public class CommandWhitelist extends JavaPlugin {
@@ -28,12 +26,20 @@ public void onEnable() {
2826

2927
reloadPluginConfig();
3028

31-
getServer().getPluginManager().registerEvents(new PlayerCommandPreProcessListener(), this);
29+
Plugin protocollib = getServer().getPluginManager().getPlugin("ProtocolLib");
30+
31+
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
3232
if (!isLegacy) {
33-
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
33+
if (!getConfigCache().isUseProtocolLib() || protocollib == null || !protocollib.isEnabled()) {
34+
getServer().getPluginManager().registerEvents(new PlayerCommandPreProcessListener(), this);
35+
} else {
36+
PacketCommandSendListener.protocol(this);
37+
getLogger().info(ChatColor.AQUA+"Using ProtocolLib for command filter!");
38+
}
39+
3440
} else {
3541
getLogger().info(ChatColor.AQUA+"Running in legacy mode...");
36-
if (getServer().getPluginManager().getPlugin("ProtocolLib") != null) {
42+
if (protocollib != null) {
3743
LegacyPlayerTabChatCompleteListener.protocol(this);
3844
} else {
3945
getLogger().info(ChatColor.YELLOW+"ProtocolLib is required for tab completion blocking!");

src/main/java/eu/endermite/commandwhitelist/spigot/config/ConfigCache.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ConfigCache {
1717
private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded, whitelistedCommand,
1818
removedWhitelistedCommand, noSuchGroup, subCommandDenied;
1919
private final List<String> commandDeniedList;
20+
private boolean useProtocolLib;
2021

2122
public ConfigCache(FileConfiguration config) {
2223

@@ -33,6 +34,8 @@ public ConfigCache(FileConfiguration config) {
3334
removedWhitelistedCommand = config.getString("messages.removed-from-whitelist", "&eRemoved command &6%s &efrom permission &6%s");
3435
noSuchGroup = config.getString("messages.group-doesnt-exist", "&cGroup %s doesn't exist");
3536

37+
useProtocolLib = config.getBoolean("use-protocollib-to-detect-commands", false);
38+
3639
Set<String> perms = config.getConfigurationSection("commands").getKeys(false);
3740
for (String s : perms) {
3841
this.permList.put(s, config.getStringList("commands."+s));
@@ -99,4 +102,7 @@ public String getNoSuchGroup() {
99102
public String getSubCommandDenied() {
100103
return subCommandDenied;
101104
}
105+
public boolean isUseProtocolLib() {
106+
return useProtocolLib;
107+
}
102108
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package eu.endermite.commandwhitelist.spigot.listeners;
2+
3+
import com.comphenix.protocol.PacketType;
4+
import com.comphenix.protocol.ProtocolLibrary;
5+
import com.comphenix.protocol.ProtocolManager;
6+
import com.comphenix.protocol.events.ListenerPriority;
7+
import com.comphenix.protocol.events.PacketAdapter;
8+
import com.comphenix.protocol.events.PacketContainer;
9+
import com.comphenix.protocol.events.PacketEvent;
10+
import eu.endermite.commandwhitelist.api.CommandsList;
11+
import eu.endermite.commandwhitelist.api.RandomStuff;
12+
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
13+
import eu.endermite.commandwhitelist.spigot.config.ConfigCache;
14+
import org.bukkit.ChatColor;
15+
import org.bukkit.entity.Player;
16+
import org.bukkit.plugin.Plugin;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
public class PacketCommandSendListener {
21+
22+
public static void protocol(CommandWhitelist plugin) {
23+
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
24+
commandExecListener(protocolManager, plugin);
25+
}
26+
27+
public static void commandExecListener(ProtocolManager protocolManager, Plugin plugin) {
28+
protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Client.CHAT) {
29+
@Override
30+
public void onPacketReceiving(PacketEvent event) {
31+
PacketContainer packet = event.getPacket();
32+
String string = packet.getStrings().read(0);
33+
if (!string.startsWith("/"))
34+
return;
35+
Player player = event.getPlayer();
36+
if (player.hasPermission("commandwhitelist.bypass"))
37+
return;
38+
String cmd = string.replace("/", "");
39+
String[] split = cmd.split("\\s+");
40+
String command = split[0].toLowerCase();
41+
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
42+
if (!player.hasPermission("commandwhitelist.commands." + s.getKey()))
43+
continue;
44+
for (String comm : s.getValue()) {
45+
comm = comm.toLowerCase();
46+
if (command.equalsIgnoreCase(comm) || command.startsWith(comm + " ")) {
47+
List<String> bannedSubCommands = CommandsList.getSuggestions(player);
48+
for (String bannedSubCommand : bannedSubCommands) {
49+
if (cmd.startsWith(bannedSubCommand)) {
50+
event.setCancelled(true);
51+
ConfigCache config = CommandWhitelist.getConfigCache();
52+
player.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getSubCommandDenied())));
53+
return;
54+
}
55+
}
56+
return;
57+
}
58+
}
59+
}
60+
61+
event.setCancelled(true);
62+
ConfigCache config = CommandWhitelist.getConfigCache();
63+
player.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getCommandDenied())));
64+
65+
}
66+
});
67+
}
68+
}

src/main/resources/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ messages:
99
removed-from-whitelist: "&eRemoved command &6%s &efrom permission &6%s"
1010
group-doesnt-exist: "&cGroup doesn't exist or error occured"
1111

12+
# To cover the 1% of plugins that don't register their commands and/or aliases properly.
13+
# Do not enable if you don't have issues with aliased commands.
14+
# This requires server restart to take effect.
15+
use-protocollib-to-detect-commands: false
16+
1217
commands:
1318
# Permissions that control what commands players can use
1419
# you can add unlimited amount of whitelists

0 commit comments

Comments
 (0)