Block a player from receiving all chat

Discussion in 'Plugin Development' started by Tehyoda, May 11, 2013.

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

    Tehyoda

    I want to block a player from receiving all chat, including from the server.
    I'm currently trying to use AsyncPlayerChatEvent, but the server chat doesn't appear to fire this event.
    Is there another event I can use? How could I achieve this?
     
  2. Offline

    Hoolean

    May we see your code monsieur? ;)
     
  3. Offline

    ZeusAllMighty11

    You can getRecipents() and remove the player
     
  4. Offline

    Tehyoda

    Well this is my current code, so I can see what fires the events.
    But the /say hello world command from the server console doesn't seem to fire the event.

    Code:
    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent evt)
    {
        System.out.println("MSGDBG: " + evt.getMessage());
    }
    I'm trying to block a player from receiving all chat, including from the server.
     
  5. Offline

    Hoolean

    The reason /say doesn't fire the event is because it is AsyncPlayerChatEvent. Unfortunately there is no Event fired when any message is sent to a player, so you may have to catch Packets :(
     
  6. Offline

    Tehyoda

    Oh boy sounds like fun :D
    Got any tutorials that explain how to do this? Or at least point me in the right direction?
     
  7. Offline

    Hoolean

    Unfortunately, I've never really meddled with it although if I remember correctly there is a nice library called ProtocolLib that helps on the way :)

    Tehyoda

    Found it!

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

    Comphenix

    You simply listen for all chat packet sent by the server, and cancel them if they happen to be sent to a player who should not receive chat:
    Code:java
    1. // Players that cannot recieve packets
    2. private Set<String> banned = new HashSet<String>();
    3.  
    4. @Override
    5. public void onEnable() {
    6. banned.add("aadnk");
    7.  
    8. ProtocolLibrary.getProtocolManager().addPacketListener(
    9. new PacketAdapter(this, ConnectionSide.SERVER_SIDE, Packets.Server.CHAT) {
    10. @Override
    11. public void onPacketSending(PacketEvent event) {
    12. if (banned.contains(event.getPlayer().getName())) {
    13. // Prevent it from ever being sent
    14. event.setCancelled(true);
    15. }
    16. }
    17. });
    18. }
     
    Tehyoda likes this.
  9. Offline

    Hoolean

    That's quite a nice library you have there :)

    Just one question, why doesn't it use the Bukkit Event system? It supports custom events! :)
     
  10. Offline

    Comphenix

    Thanks. :)

    It's for performance reasons - one, the Bukkit event system uses reflection, but secondly, I don't want to fire a packet event for every packet sent and received.

    The simplest way to do that in the Bukkit event system is to create a separate PacketEvent for every packet type and direction, but this would have mean a lot of bloat, and potentially a lot of work in updating ProtocolLib for a new version of Minecraft. It also doesn't support custom packets sent by Forge mods in MCPC+.

    And finally, the Bukkit system would have been less powerful to work with. For instance, to log every packet, you would have to create a method for every packet type, while in the current event system this is very easily achieved with MonitorAdapter.
     
  11. Offline

    Hoolean

  12. Offline

    Tehyoda

    Thanks for that! It works perfectly.
     
    MrBluebear3 likes this.
Thread Status:
Not open for further replies.

Share This Page