Solved Nullpointer on Chat Listener (ProtocolLib)

Discussion in 'Spigot Plugin Development' started by Kiryu144, Feb 2, 2020.

  1. Sometimes if the player gets a message from any other plugin, I get a nullpointer exception on line 6.
    This is especially the case for WorldEdit, but some other Plugins aswell.

    Code (Java):
    ProtocolLibrary.getProtocolManager().addPacketListener(
        new PacketAdapter(KiryuChat.instance, ListenerPriority.HIGHEST, PacketType.Play.Server.CHAT) {
            @Override
            public void onPacketSending(PacketEvent event) {
                if(chatType.equals(EnumWrappers.ChatType.SYSTEM)){
                    BaseComponent[] messages = ComponentSerializer.parse(event.getPacket().getChatComponents().getValues().get(0).getJson());
                }
            }
        }
    );
    What am I doing wrong?
     
  2. SteelPhoenix

    Moderator

    1) What is chatType, it's not defined anywhere in the code you sent
    2) Why aren't you reading the components using StructureModifier#read(int) / StructureModifier#readSafely(int)
     
  3. Oh, I left that in. Just ignore that. :rolleyes:

    I'll look it up in the JavaDoc later when I am back home again. If you know any up to date tutorial/documentation for the ProtocolAPI that doesn't target script kiddies, I would be very thankful!
     
  4. That doesn't solve the issue.
    Code (Text):
    WrappedChatComponent wrappedChatComponent = event.getPacket().getChatComponents().read(0); // Returns null
    even though
    Code (Java):
    System.out.println(event.getPacket().getChatComponents().size()); // Returns 1
    Could there be something wrong with the ProtocolLib?
    This btw only happens when I type some commands. Example: '//help' (WorldEdit).
     
    #5 Kiryu144, Feb 2, 2020
    Last edited: Feb 2, 2020
  5. Okay, solution found, thanks to @AlanZ.

     
    • Like Like x 1