Skip to content

Commit

Permalink
implemented packet version of commands declare
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Oct 10, 2021
1 parent f101b68 commit 5d7cd9d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CommandWhitelistBukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,11 @@
<version>2.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.mojang</groupId>
<artifactId>brigadier</artifactId>
<version>1.0.17</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import eu.endermite.commandwhitelist.bukkit.command.MainCommandExecutor;
import eu.endermite.commandwhitelist.bukkit.listeners.*;
import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandPreProcessListener;
import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandSendListener;
import eu.endermite.commandwhitelist.common.CWGroup;
import eu.endermite.commandwhitelist.common.ConfigCache;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
Expand Down Expand Up @@ -39,11 +41,12 @@ public void onEnable() {

if (!getConfigCache().useProtocolLib || protocollib == null || !protocollib.isEnabled()) {
getServer().getPluginManager().registerEvents(new PlayerCommandPreProcessListener(), this);
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
} else {
PacketCommandPreProcessListener.protocol(this);
PacketCommandSendListener.protocol(this);
getLogger().info(ChatColor.AQUA + "Using ProtocolLib for command filter!");
}
getServer().getPluginManager().registerEvents(new PlayerCommandSendListener(), this);
try {
// Use paper's async tab completions if possible
Class.forName("com.destroystokyo.paper.event.server.AsyncTabCompleteEvent");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.endermite.commandwhitelist.bukkit.listeners;
package eu.endermite.commandwhitelist.bukkit.listeners.protocollib;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package eu.endermite.commandwhitelist.bukkit.listeners.protocollib;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.mojang.brigadier.tree.RootCommandNode;
import eu.endermite.commandwhitelist.bukkit.CommandWhitelistBukkit;
import eu.endermite.commandwhitelist.common.CWPermission;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.HashSet;

public class PacketCommandSendListener {

public static void protocol(CommandWhitelistBukkit plugin) {
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
commandSendListener(protocolManager, plugin);
}

public static void commandSendListener(ProtocolManager protocolManager, Plugin plugin) {
protocolManager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.HIGHEST, PacketType.Play.Server.COMMANDS) {
@Override
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
if (player.hasPermission(CWPermission.BYPASS.permission())) return;

HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player);
PacketContainer packet = event.getPacket();
RootCommandNode<?> node = (RootCommandNode<?>) packet.getModifier().getValues().get(0);
node.getChildren().removeIf((cmd) -> !commandList.contains(cmd.getName()));
}
});
}

}

0 comments on commit 5d7cd9d

Please sign in to comment.