Chat Filter

Discussion in 'Spigot Plugin Development' started by Potters, May 29, 2016.

  1. Hey! I am trying to develop a chat filter on a per-player basis. Players can toggle it on or off deciding on their personal preference on swearing

    Example:

    Player 1 » "Fuck this shit" - Toggled off
    Player 2 » "**** this ****" - Toggled on

    My current code is:

    Command:

    Code (Text):
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player){
                Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("chatfilter")) {
                if(args.length == 0) {
                    sender.sendMessage(prefix + "Please enter if chatfilter it §aenabled §aor §cdisabled§a! §l(On/Off)");
                } else {
                if (args[0].equalsIgnoreCase("on")) {
                    chatFilter.add(p);
                    sender.sendMessage(prefix + "§aYour chat is now filtered.");
                }
                else if (args[0].equalsIgnoreCase("off")) {
                    chatFilter.remove(p);
                    sender.sendMessage(prefix + "§cYour chat is now not filtered.");
               
                } else {
                    sender.sendMessage(prefix + "Please enter if chatfilter it §aenabled §aor §cdisabled§a! §l(On/Off)");
             
            }
    Actual chat filter:

    (Please note, it is recognizing the config and the words but isn't filtering them)

    Code (Text):
                @EventHandler
                public void Chat(AsyncPlayerChatEvent e)
                {
                    List<String> list = getConfig().getStringList("List");
                    String original = e.getMessage();
                    boolean bad = false;
                    for(String word : original.split(" "))
                    {
                        if(list.contains(word))
                        {

                            bad = true;
                            break;
                        }
                    }
                    List<Player> toRemove = new ArrayList<Player>();
                    if(bad)
                    {
                        for(Player p : e.getRecipients())
                        {
                            if(chatFilter.contains(p))
                            {
                                toRemove.add(p);
                               
                            }
                        }
                        for(String badword : list)
                        {
                            if(original.contains(badword))
                            {
                                original.replaceAll(badword, "****");
                            }
                        }
                        for(Player p : toRemove)
                        {
                            e.getRecipients().remove(p);
                            String formattedMessage = String.format(e.getFormat(), new Object[] { e.getPlayer().getDisplayName(), original});
                            formattedMessage.replaceAll("Fuck ", "****");
                            p.sendMessage(formattedMessage);
                        }
                       
                    }
     
  2. and...? whats the problem with it? any Exceptions? any Bugs?
     
  3. It recognizes if the player has the chat toggled on.
    The problem is it doesn't censor the words at all
     
  4. getting loaded from config.yml or a yml file?
     
  5. had the same problem until i got some help from a close friend and developed it by my self a year after


    Code (Text):
                  List<String> WordsList = Main.getConfig().getStringList("blacklisted_words_path_here");
                  for (String BlockedWords : WordsList)
                  if (event.getMessage().contains(BlockedWords)) {
                      event.setCancelled(true);
     
  6. Hmm, the problem with this is that it is simply cancelling the event. I am removing recipients (Those who have the chat filter on) and replacing swearing with * where as those who do not their chat remains unchanged.
     
  7. Code (Text):
    List<String> wordsList = Main.getConfig().getStringList("blacklisted_words_path_here");
    for (String word : wordsList)
    if (event.getMessage().contains(word) && shouldChatFilterBeEnabledForThatPlayer) {
      event.getMessage().replace(word, "****");
     
    #8 Ferdz, May 29, 2016
    Last edited: May 30, 2016
  8. _Ug

    _Ug

    A problem I notice right away is you have original.replaceAll(badword, "****"). If you look at replaceAll you will find that it returns a String and does not alter the original message. The way this line should look would be:
    Code (Text):
    original = original.replaceAll(badword, "****");
     
    • Like Like x 1
  9. Do you happen to have HeroChat installed?
     
  10. #11 Potters, May 30, 2016
    Last edited: May 30, 2016