ProtocolLib - get the content of the message

Discussion in 'Plugin Development' started by zwoosks, Mar 7, 2019.

Thread Status:
Not open for further replies.
  1. Hello! I'm coding a plugin that in my mind it would be able to block certain messages from the server, but to do this I need to get the content of the message. I'm using ProtocolLib.

    My code:
    Code:
    @Override
        public void onEnable() {
            getLogger().info("¡El remover de mensajes vacíos ha sido activado! (Hecho por Zwoosks)");
           
            ProtocolLibrary.getProtocolManager().addPacketListener(
                    new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.CHAT) {
                @Override
                public void onPacketSending(PacketEvent event) {
                    Bukkit.broadcastMessage(ChatColor.RED + "" + ChatColor.BOLD + "NEW PACKET: " + event.getPacket().toString());
                }
            });
           
        }
    Thanks! :)
     
  2. Offline

    CraftCreeper6

    @zwoosks
    Why ProtocolLib compared to PlayerChatEvent?
     
  3. Well, I wanted to check server messages and I found this way to get the packet
     
  4. Offline

    CraftCreeper6

    @zwoosks
    Server messages from the server or from the player chat?
     
  5. Both. I saw how to get the message:
    Code:
    ProtocolLibrary.getProtocolManager().addPacketListener(
                    new PacketAdapter(this, ListenerPriority.NORMAL, PacketType.Play.Server.CHAT) {
                @Override
                public void onPacketSending(PacketEvent event) {
                   
                    try {
                       
                        String message = "";
                        try {
                            String jsonMessage = event.getPacket().getChatComponents().getValues().get(0).getJson();
                            if (jsonMessage != null)
                                message = jsonMessage;
                        } catch (Throwable ignore) {
                        }
                       
                        System.out.println(":v");
                        System.out.println(message);
                       
                    } catch(Exception e) {
                       
                        System.err.println("error");
                        e.printStackTrace();
                       
                    }
                   
                }
            });
    But I get a json string. How could I pass the message to a plain text?
     
  6. Offline

    CraftCreeper6

    @zwoosks
    either use a json library or parse it yourself.

    JSON is one of the easiest formats you'll see, it's pretty simple. Could you send an example JSON message that you are receiving? (I.E. broadcast the message you receive)
     
  7. Well, I want to remove empty messages, and they're only represented by "". I tried to check if the message equals to these two consecutive chars and cancel the event if it's true and it doesn't work,
     
  8. Offline

    CraftCreeper6

    @zwoosks
    You could just check the length of the message, if it doesn't meet a certain threshold then discard it.
     
  9. Thanks!!! It worked.
     
    CraftCreeper6 likes this.
  10. Is there a way yo ger what plugin sends the message?
     
  11. Offline

    CraftCreeper6

    @zwoosks
    Unlikely, the chat doesn't transmit a packet with Plugin metadata, only the message contents.
     
  12. Online

    timtower Administrator Administrator Moderator

    Highly doubt it.
    If possible then with the stacktrace if the messages get send right away.
     
Thread Status:
Not open for further replies.

Share This Page