package com.forgeessentials.chat;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.DamageSource;
import net.minecraftforge.common.config.Configuration;

import com.forgeessentials.core.ForgeEssentials;
import com.forgeessentials.core.moduleLauncher.config.ConfigLoader.ConfigLoaderBase;
import com.forgeessentials.util.OutputHandler;
import com.google.common.base.Strings;

public class Censor extends ConfigLoaderBase
{

    private static final String CONFIG_CATEGORY = "Chat.Censor";

    private static final String[] DEFAULT_WORDS = new String[] { "fuck.*", "bastard", "moron", "ass", "asshole", "bitch", "shit" };

    private static final String CENSOR_HELP = "Words to be censored. Prepend with ! to disable word boundary check.";

    private List<CensoredWord> filterList = new ArrayList<>();

    public boolean enabled;

    public String censorSymbol;

    public int censorSlap;

    public class CensoredWord
    {

        public String word;

        public String blank;

        public Pattern pattern;

        public CensoredWord(String word)
        {
            if (word.startsWith("!"))
            {
                blank = Strings.repeat(censorSymbol, word.length() - 1);
                word = word.substring(1);
            }
            else
            {
                blank = Strings.repeat(censorSymbol, word.length());
                word = "\\b" + word + "\\b";
            }
            pattern = Pattern.compile(word, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
        }

    }

    public Censor()
    {
        ForgeEssentials.getConfigManager().registerLoader(ModuleChat.CONFIG_FILE, this);
    }

    @Override
    public void load(Configuration config, boolean isReload)
    {
        enabled = config.get(CONFIG_CATEGORY, "enable", true).getBoolean(true);
        censorSlap = config.get(CONFIG_CATEGORY, "slapDamage", 1, "Damage to a player when he uses a censored word").getInt();
        censorSymbol = config.get(CONFIG_CATEGORY, "censorSymbol", "#", "Replace censored words with this character").getString();
        if (censorSymbol.length() > 1)
        {
            OutputHandler.felog.warning("Censor symbol is too long!");
            censorSymbol = censorSymbol.substring(1);
        }
        else if (censorSymbol.isEmpty())
        {
            OutputHandler.felog.warning("Censor symbol is empty!");
            censorSymbol = "#";
        }
        for (String word : config.get(CONFIG_CATEGORY, "words", DEFAULT_WORDS, CENSOR_HELP).getStringList())
            filterList.add(new CensoredWord(word));
    }

    public String filter(EntityPlayerMP player, String message)
    {
        if (!enabled)
            return message;
        for (CensoredWord filter : filterList)
        {
            Matcher m = filter.pattern.matcher(message);
            if (m.find())
            {
                message = m.replaceAll(filter.blank);
                if (censorSlap != 0)
                    player.attackEntityFrom(DamageSource.generic, censorSlap);
            }
        }
        return message;
    }

}

@@ -696,17 +696,19 @@
  %7B %22fuck
-.
+%5C%5CS
 *%22, %22bas
@@ -1041,24 +1041,31 @@
 %0A    public 
+static 
 class Censor
@@ -1263,95 +1263,8 @@
 %22))%0A
-            %7B%0A                blank = Strings.repeat(censorSymbol, word.length() - 1);%0A
     
@@ -1301,30 +1301,16 @@
 ing(1);%0A
-            %7D%0A
         
@@ -1322,91 +1322,8 @@
 lse%0A
-            %7B%0A                blank = Strings.repeat(censorSymbol, word.length());%0A
     
@@ -1363,30 +1363,16 @@
  %22%5C%5Cb%22;%0A
-            %7D%0A
         
@@ -2373,32 +2373,60 @@
  %22#%22;%0A        %7D%0A
+        filterList.clear();%0A
         for (Str
@@ -2833,24 +2833,24 @@
  (m.find())%0A
-
             
@@ -2843,32 +2843,160 @@
 )%0A            %7B%0A
+                if (filter.blank == null)%0A                    filter.blank = Strings.repeat(censorSymbol, m.end() - m.start());%0A
                 

Update to censor.