Solved My word blocker does not work.

Discussion in 'Spigot Plugin Development' started by avighnash, May 27, 2016.

Thread Status:
Not open for further replies.
  1. Hello! I have this simple word blocker which seems to not work:

    Code (Text):
    package us.universalpvp.te.chat;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.event.player.PlayerJoinEvent;

    import net.md_5.bungee.api.ChatColor;

    public class ChatFilter implements Listener {

        private List<String> blocked = new ArrayList<>();

        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            blocked.add("ass");
            blocked.add("fuck");
            blocked.add("shit");
            blocked.add("hell");

        }

        @EventHandler
        public void onChat(AsyncPlayerChatEvent e) {

            Player p = e.getPlayer();

            String message = e.getMessage().toLowerCase();

            for (String words : blocked) {

                if (message.contains(words)) {
                    e.setCancelled(true);

                    p.sendMessage(
                            ChatColor.translateAlternateColorCodes('&', "&6----------------------------------------"));
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                            "&c&lSwearing or trying to bypass the\n&c&lchat filter is against the rules!"));
                    p.sendMessage(
                            ChatColor.translateAlternateColorCodes('&', "&6----------------------------------------"));

                }

            }
        }

    }
    and when I mean by "not work" is like whenever I say "hello", it thinks I am trying to say "hell" so it blocks that. How do I fix this? Thanks!
     
  2. Geka

    Supporter

    Try changing the second line code.
     
  3. The second line of code is blank... its a space...
     
  4. Change to the org.bukkit ChatColor.
     
  5. Sure/
    But how would that make this not work?
     
  6. First why add you the words on every Join? Move it to onEnable :)

    Check every word.
    Example:
    Code (Text):
        @EventHandler
        public void onChat(AsyncPlayerChatEvent event){
            Player player = event.getPlayer();
            String[] message = event.getMessage().split(" ");
            for(String word : message){
                if(blocked.contains(word)){
                    event.setCancelled(true);
                    player.sendMessage("§cYour message was blocked!");
                    return;
                }
            }
        }
     
    • Useful Useful x 1
  7. Thanks! This worked! :)
     
Thread Status:
Not open for further replies.