Per Player Chat

Discussion in 'BungeeCord Plugin Development' started by Potters, May 22, 2016.

  1. Hey! I am trying to create a plugin in which if the player uses "/chatfilter on" it blocks out words such as shit and fuck but only for them

    Example:

    Player one: "I ****ing hate this" - This player has enabled their chat filter.
    Player two "I fucking hate this" - This player has disabled their chat filter.

    Currently I am using an ArrayList to store the players information.

    How do I do this without needing a custom chat? I need this to tie in with Essentials chat etc but I am really struggling!

    Code (Text):
                   @EventHandler
                   public void onPlayerChat1(AsyncPlayerChatEvent e) {
                       Player p2 = Bukkit.getPlayer(getName());
                       for (String word : e.getMessage().split(" ")) {
                           if (getConfig().getStringList("List").contains(word)) {
                                if (chatFilter.contains(p2)) {
                                    p2.sendMessage(prefix + "┬žaWorks");
    I have tried using ProtocolLib but have never used it before and would prefer to be able to do it with Spigot, although if I need ProtocolLib I will use it.
     
  2. Use the AsyncPlayerChatEvent and put the priority on monitor (so that it gets called lastly, after all other plugins have done their chat related things). Cancel the event and send the message just as are doing in your example above.

    Don't forget, the event is async. After cancelling, use a new BukkitRunnable().runTask(plugin); to get back to the main Spigot thread. Otherwise you should never access methods of the Bukkit API.
     
  3. Yes, but if I just send the message will it not edit the chat? I need my chat to stay identical which is thing that would confuse me.
     
  4. I just set the event Priority to Monitor and for some reason it doesn't call the event ever.
     
  5. Oh, I get what you mean. I guess you're looking for something like this.

    Code (Text):
        @EventHandler(priority = EventPriority.MONITOR)
        public void onChat(AsyncPlayerChatEvent event) {
            event.setCancelled(true);

            // only disadvantage, normally this is done async, so your players can still chat when your
            // server freezes (big worldedit or something), no longer possible when doing it this way
            new BukkitRunnable() {

                @Override
                public void run() {
                    String customMessage = event.getMessage();
                    String formattedMessage = String.format(event.getFormat(), new Object[] { event.getPlayer().getDisplayName(), customMessage });
                    event.getPlayer().sendMessage(formattedMessage); // TODO change here whatever your want
                }
            }.runTask(plugin);
        }
     
  6. Ok, so with this it will automatically get the players chat format from Essentials? I Have never needed to work with chat like this before so I am a real noob!