Grab player chat message with ProtocolLib in Paper 1.16.5
flechabenx opened this issue ยท 2 comments
Make sure you're doing the following
- You're using the latest build for your server version
- This isn't an issue caused by another plugin
- You've checked for duplicate issues
- You didn't use
/reload
Describe the question
So I am trying to detect when a player writes something in chat using ProtocolLib. For some reason, when using Paper 1.16.5 I can't manage to do it. I have the code show below, and it does capture every other chat message, from other plugins, so it's working fine, the issue happens when a player writes something in chat, I got this error:
java.lang.ClassCastException: class net.kyori.adventure.text.TextComponentImpl cannot be cast to class [Lnet.md_5.bungee.api.chat.BaseComponent; (net.kyori.adventure.text.TextComponentImpl and [Lnet.md_5.bungee.api.chat.BaseComponent; are in unnamed module of loader 'app') at it.ajneb97.managers.ProtocolLibJugadorManager$1.onPacketSending(ProtocolLibJugadorManager.java:66) ~[?:?] at com.comphenix.protocol.injector.SortedPacketListenerList.invokeSendingListener(SortedPacketListenerList.java:195) ~[?:?] at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketSending(SortedPacketListenerList.java:149) ~[?:?] at com.comphenix.protocol.injector.PacketFilterManager.handlePacket(PacketFilterManager.java:535) ~[?:?] at com.comphenix.protocol.injector.PacketFilterManager.invokePacketSending(PacketFilterManager.java:516) ~[?:?] at com.comphenix.protocol.injector.netty.ProtocolInjector.packetQueued(ProtocolInjector.java:346) ~[?:?] at com.comphenix.protocol.injector.netty.ProtocolInjector.onPacketSending(ProtocolInjector.java:308) ~[?:?] at com.comphenix.protocol.injector.netty.ChannelInjector.processSending(ChannelInjector.java:433) ~[?:?] at com.comphenix.protocol.injector.netty.ChannelInjector.access$800(ChannelInjector.java:70) ~[?:?] at com.comphenix.protocol.injector.netty.ChannelInjector$3.handleScheduled(ChannelInjector.java:373) ~[?:?] at com.comphenix.protocol.injector.netty.ChannelInjector$3.onMessageScheduled(ChannelInjector.java:343) ~[?:?] at com.comphenix.protocol.injector.netty.ChannelProxy$2.schedulingRunnable(ChannelProxy.java:127) ~[?:?] at com.comphenix.protocol.injector.netty.EventLoopProxy.execute(EventLoopProxy.java:95) ~[?:?] at net.minecraft.server.v1_16_R3.NetworkManager.b(NetworkManager.java:311) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.NetworkManager.dispatchPacket(NetworkManager.java:267) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.NetworkManager.sendPacket(NetworkManager.java:243) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1903) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PlayerConnection.sendPacket(PlayerConnection.java:1876) ~[patched_1.16.5.jar:git-Paper-788] at org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer.sendMessage(CraftPlayer.java:2133) ~[patched_1.16.5.jar:git-Paper-788] at net.kyori.adventure.audience.Audience.sendMessage(Audience.java:274) ~[patched_1.16.5.jar:git-Paper-788] at io.papermc.paper.adventure.ChatProcessor.complete(ChatProcessor.java:172) ~[patched_1.16.5.jar:git-Paper-788] at io.papermc.paper.adventure.ChatProcessor.processModern(ChatProcessor.java:135) ~[patched_1.16.5.jar:git-Paper-788] at io.papermc.paper.adventure.ChatProcessor.lambda$process$1(ChatProcessor.java:68) ~[patched_1.16.5.jar:git-Paper-788] at io.papermc.paper.adventure.ChatProcessor.processingLegacyFirst(ChatProcessor.java:120) ~[patched_1.16.5.jar:git-Paper-788] at io.papermc.paper.adventure.ChatProcessor.process(ChatProcessor.java:65) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PlayerConnection.chat(PlayerConnection.java:2102) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:2017) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1955) ~[patched_1.16.5.jar:git-Paper-788] at net.minecraft.server.v1_16_R3.PacketPlayInChat$1.run(PacketPlayInChat.java:43) ~[patched_1.16.5.jar:git-Paper-788] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[?:?] at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[?:?] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[?:?] at java.lang.Thread.run(Thread.java:831) [?:?] [17:07:39 ERROR]: Parameters: net.minecraft.server.v1_16_R3.PacketPlayOutChat@1a6d9379[ a=<null> components=<null> b=CHAT c=89bbfaed-879d-3300-b78a-af6775239576 ]
I always get an error on this line:
BaseComponent[] baseComponents = (BaseComponent[]) packet.getModifier().read(1);
I searched for solutions and find that Paper added something new regarding ChatComponent that was caussing conflict with ProcotolLib, but this was fixed supposedly in newer versions of ProtocolLib. I am using 4.7.0.
API method(s) used
PacketType.Play.Server.CHAT
Expected behavior
I don't know exactly why is the player chat message showing as net.kyori.adventure.text.TextComponentImpl, I can't convert it properly to BaseComponent[] and I even tried to import that class but shows that it doesn't exists. So my question is, how can I convert it, or which one is the correct way of obtaining the player chat messages?
See the adventure docs: https://docs.adventure.kyori.net/
This part on migration may also be useful: https://docs.adventure.kyori.net/migration/