Troubles with PacketEvent
xw1w1 opened this issue ยท 5 comments
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
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);
Also, I saw this piece of code:
Component adventure$content = clientboundSystemChatPacket.adventure$content();
what could it be?
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.
Please refer to this example on how you can access both fields with a single method call: https://github.com/lukalt/PacketWrapper/blob/master/src/main/java/com/comphenix/packetwrapper/wrappers/play/clientbound/WrapperPlayServerSystemChat.java#L44 @xw1w1