1.15.2 Client IndexOutOfBoundsException on reload command

Discussion in 'Spigot Plugin Development' started by Bjorno43, Feb 10, 2020.

  1. I'm working on a chat plugin and the following code is causing all clients to disconnect with the exception:

    Code (Text):
    DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(0) + length(1) exceeds writerIndex(0) UnpooledSlicedByteBuf(ridx: 0, widx: 0, cap: 0/0, unwrapped: PooledUnsafeDirectByteBuf(ridx: 2, widx: 6, cap: 16384)) @ io.netty.handler.codec.MessageToMessageDecoder:98
    Code:

    Code (Java):
    private void initPacketListener(){
        protocolManager.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL, new PacketType[] { PacketType.Play.Server.CHAT })
        {
            @SuppressWarnings("unused")
            @Override
            public void onPacketSending(PacketEvent event) {

                if ((event.getPacketType() == PacketType.Play.Server.CHAT)){
                    PacketContainer packet = event.getPacket();
                    Player player = event.getPlayer();
                    String message = "";
                   
                    try {
                        String jsonMessage = event.getPacket().getChatComponents().getValues().get(0).getJson();
                       
                        if (jsonMessage!=null&&!jsonMessage.isEmpty()) {
                            message = jsonToString(jsonMessage);
                           
                            if (message.isEmpty()) return;
                           
                            Subscriber subscriber = UMM.plugin.getSubscribers().get(player.getUniqueId());
                            String uuid = player.getUniqueId().toString();
                            Channel messageChannel = UMM.serverChannels.get(uuid);
                            messageChannel.addMessage(message);
                            ChatListener cl = new ChatListener(UMM.plugin);

                            cl.notifyChanges(messageChannel);
                            event.setCancelled(true);
                        } else {
                            System.out.println("Not a system msg");
                        }
                    } catch (Throwable e){
                        System.out.println("UMM Packet Error:" + e.getMessage());
                    }
                }
            }
        });
    }
    Should probably explain that this is ProtocolLib. I'm catching all chat messages here in order to display them in the correct chat channel. It works perfectly fine in game and isn't causing any issues with players connecting or disconnecting. Only when a reload is performed, the client error pops up. No errors in console though.
     
  2. Make sure that the size/length of event.getPacket().getChatComponents().getValues() is greater than 0 before making the #get call.
     
  3. Thanks for your reply. I tried what you advised me, but unfortunately it didn't have any effect:
    Code (Java):
    List<WrappedChatComponent> wrap = event.getPacket().getChatComponents().getValues();
                 
    if(wrap.size() > 0) {
        if ((event.getPacketType() == PacketType.Play.Server.CHAT)){
            ...
        }
    }