ProtocolLib

3M Downloads

[API] Help with chat packet

david85258 opened this issue ยท 2 comments

commented

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
It's the first time I handle chat message packet, and I'm trying to get the packet that the server sends to the players when someone writes a chat message, but when I try to get the WrappedChatComponent it gives me the error "Field index 0 is out of bounds for length 0" is this normal or am I making some silly mistake? Could someone give me an example or how I could solve it?
(I'm using Purpur 1.19.2)

Expected behavior
Return the WrappedChatComponent with the message sent.

Code

        manager.addPacketListener(new PacketAdapter(plugin, ListenerPriority.NORMAL, PacketType.Play.Server.CHAT) {
            @Override
            public void onPacketSending(PacketEvent event) {
                Player player = event.getPlayer();
                Bukkit.getConsoleSender().sendMessage("Packet sent to " + player.getName());
                if (event.getPacketType() != PacketType.Play.Server.CHAT) return;

                WrappedChatComponent wrappedChatComponent = event.getPacket().getChatComponents().read(0);
                Bukkit.getConsoleSender().sendMessage(wrappedChatComponent.toString());
            }
        });

I am sorry for my bad English. I'm using google translator.

commented

You need to study the packet that you want to manipulate, the 1.19.2 Chat packet has not a ChatComponent field.

commented

Thanks, in the end, I ended up using 1.19.3, since the plugin is for a specific server and I don't need backward compatibility, but thanks anyway