ProtocolLib

3M Downloads

Troubles with PacketEvent

xw1w1 opened this issue ยท 5 comments

commented

Im making some chat plugin and need to collect messages

public void onPacket(@NotNull PacketEvent event) {
        if (event.getPacketType() == PacketType.Play.Server.SYSTEM_CHAT) {
            PacketContainer container = event.getPacket();
            ClientboundSystemChatPacket clientboundSystemChatPacket = (ClientboundSystemChatPacket)container.getHandle();
            if (!clientboundSystemChatPacket.c()) {
                    String packetContent = clientboundSystemChatPacket.content();
                    if (packetContent.toLowerCase().replaceAll("[{}\":\\[]\\d}\\$\\^\\|,\\\\/;]", "").trim().contains("jndi")) {
                        event.setCancelled(true);
                    } else {
                        <redacted>
                    }
                }
            }
        }
    }

it throwns out when im doing player.sendMessage(net.kyori.adventure.text.Component)
Not sure what could be wrong, worked very little with ProtocolLib so hopefully getting some help

commented

The name of the method is wrong. You need to override void onPacketSending(PacketEvent event); but you implemented void onPacket(PacketEvent event);. I'd suggest to add @Override in front of the method's signature to get compilation error if you use a wrong method name.

Also, I suggest to use ProtocolLib's method for accessing the Packet fields so you do not need to use NMS, i.e:

boolean overlay = container.getBooleans().read(0);
String json = container.getStrings().read(0);

commented

net.minecraft.network.protocol.game.ClientboundSystemChatPacket@ae29021[
content=null
b=false
]

still getting this in console
image

commented

Also, I saw this piece of code:
Component adventure$content = clientboundSystemChatPacket.adventure$content();
what could it be?

commented

Paper has added another field to the packet which contains the chat component as a component of the adventure library. If clientboundSystemChatPacket.adventure$content() is not null, the clientboundSystemChatPacket.content() will be null.

So, only one of the two contains the actual message and you need to check both. Your code is failing because you are accessing content(), which apparently is null in your case.