Making messages and chat togglable

Discussion in 'Spigot Plugin Development' started by MrSpyMan, Jun 13, 2015.

  1. I am trying to make something where you can turn your messages and chat on and off. I am using protocollib to do this and I have having some difficulty. I do not get any errors but this should work in theory because whenever the server sends the chat packet it checks if the packet is a command, bungee alert or a private message and cancels it if the player's name is in an ArrayList. The problem is it still sends messages and chat to players who have chat and messages off.


    Code (Text):


    private ProtocolManager pm;
    public ArrayList<String> chatoff = new ArrayList<String>();
    public ArrayList<String> messageoff = new ArrayList<String>();

    public void onEnable() {
            loadConfiguration();
            Bukkit.getPluginManager().registerEvents(this, this);
            pm = ProtocolLibrary.getProtocolManager();
            pm.addPacketListener(new PacketAdapter(this, ListenerPriority.NORMAL,
                    PacketType.Play.Server.CHAT) {

                @Override
                public void onPacketSending(PacketEvent event) {

                    if (event.getPacketType().equals(
                            PacketType.Play.Server.CHAT)) {
     
                        PacketContainer packet = event.getPacket();
                        String message = packet.getStrings().read(0);
                        ChatColor.stripColor(message);

                        if (message.startsWith("/")) {
                            // It's a command.

                            return;
                        }

                            if(message.startsWith("[Alert]")) {
                            // Bungee Alert

                            return;
                        }

                        if (message.startsWith("[")) {

                            // Messages
                            for (Player player : Bukkit.getOnlinePlayers()) {
                                if (message
                                        .startsWith(
                                                "["
                                                       
                                                        + player.getDisplayName()
                                                        + " -> ")) {

                                    // Found message
                                    // Chat Off
                                    if (chatoff.contains(event.getPlayer()
                                            .getDisplayName())) {
                                        // Chat is off
                                        player.sendMessage(ChatColor.AQUA + event.getPlayer().getDisplayName() + "'s" + ChatColor.DARK_AQUA + "chat is currently off.");

                                        event.setCancelled(true);
                                        return;
                                    }
                                    // Message Off
                                    if (messageoff.contains(event.getPlayer()
                                            .getDisplayName())) {
                                        player.sendMessage(ChatColor.AQUA + event.getPlayer().getDisplayName() + "'s" + ChatColor.DARK_AQUA + "messages is currently off.");
                                        event.setCancelled(true);
                                        return;
                                    }

                                }
                            }
                        } else {
                            // Player is in Arraylist
                            if (chatoff
                                    .contains(event.getPlayer().getDisplayName())) {
                                event.setCancelled(true);
                                return;
                            }
                            return;
                        }

                    }
                }
            });
     
  2. Try AsyncPlayerChatEvent
     
  3. If I use AsyncPlayerChatEvent that only cancels chat and I cannot cancel messages from players. Also this event is only called when a player chats. So I could loop through all players and see who has their chat off and not send the chat message to them, but nevertheless thank you for the suggestion.
     
  4. clip

    Benefactor

    You can remove recipients in the AsyncPlayerChatEvent. Iterate through each recipient, remove them if they have chat off. Simple as that.
     
  5. That will work for chat messages. If player A saids Hello and player B has their chat off then player B will not get the chat message. But if player A did "/msg player hi" then AsyncPlayerChatEvent will not be called so I cannot remove the recipient for that.