Solved ProtocolLib PacketListener not working

Discussion in 'Plugin Development' started by stokdam, Feb 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    stokdam

    I want to censure words using protocollib. But when i type something in chat nothing happens. I just see the message as the packet has not been intercepted.

    Code:
    public void onLoad() 
        {
            protocolManager = ProtocolLibrary.getProtocolManager();
            protocolManager.addPacketListener(new PacketListener() 
            {
                @Override
                public void onPacketReceiving(PacketEvent event) 
                {
                    
                    if (event.getPacketType().getCurrentId() == 0x01) 
                    {
                        PacketContainer packet = event.getPacket();
                        String message = packet.getStrings().read(0);
                        if (message.contains("shit") || message.contains("fuck"))  //I'll put words in a words.lst in a second time
                        {
                            event.setCancelled(true);
                            event.getPlayer().sendMessage("Bad manners!");
                        }
                    }
                }
    
                @Override
                public Plugin getPlugin() 
                {
                    // TODO Auto-generated method stub
                    return null;
                }
    
                @Override
                public ListeningWhitelist getReceivingWhitelist() 
                {
                    // TODO Auto-generated method stub
                    return null;
                }
    
                @Override
                public ListeningWhitelist getSendingWhitelist() 
                {
                    // TODO Auto-generated method stub
                    return null;
                }
    
                @Override
                public void onPacketSending(PacketEvent event) 
                {
                    // TODO Auto-generated method stub
                }
            });
        }
    
    up

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    Comphenix

    You have to provide a receiving whitelist for your packet listener to be informed of any packets.

    But it's probably easier to use PacketAdapter, like the example you're basing it on (except updated for PL 3.0.0):
    Code:java
    1. public class ExampleMod extends JavaPlugin {
    2. @Override
    3. public void onEnable() {
    4. ProtocolLibrary.getProtocolManager().addPacketListener(
    5. new PacketAdapter(this, PacketType.Play.Client.CHAT) {
    6. // Note that this is executed asynchronously
    7. @Override
    8. public void onPacketReceiving(PacketEvent event) {
    9. PacketContainer packet = event.getPacket();
    10. String message = packet.getStrings().read(0);
    11.  
    12. if (message.contains("shit") || message.contains("fuck")) {
    13. event.setCancelled(true);
    14. event.getPlayer().sendMessage("Bad manners!");
    15. }
    16. }
    17. });
    18. }
    19. }

    Keep in mind that this is executed asynchronously (which you can avoid, at the cost of performance), so don't call any Bukkit API. If you want the main thread to be able to edit the banned list of words, I suggest using a concurrent data structure, or synchronization.
     
  3. Offline

    stokdam

    It works!
     
Thread Status:
Not open for further replies.

Share This Page