From adc0f41a87a47e307a1436ad2784a3f728b08d2d Mon Sep 17 00:00:00 2001 From: thmsdy Date: Thu, 13 Feb 2020 00:40:44 -0600 Subject: [PATCH] Overhaul Chat Filter --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- src/main/java/com/fpghoti/fpchatx/FPChat.java | 3 + .../com/fpghoti/fpchatx/chat/ChatFilter.java | 163 ++++++++++++------ .../command/commands/PrefixCommand.java | 1 + .../fpghoti/fpchatx/config/ChannelFile.java | 4 +- .../fpghoti/fpchatx/config/MainConfig.java | 18 +- .../fpghoti/fpchatx/config/PlayerFile.java | 2 +- .../fpchatx/permission/Permission.java | 12 ++ .../com/fpghoti/fpchatx/player/FPlayer.java | 4 +- 10 files changed, 152 insertions(+), 59 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 0c3fc47..7636fc8 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 FPChatX FPChatX - 1.0.3-BETA + 1.0.5-BETA src/main/java diff --git a/pom.xml b/pom.xml index 89c0d72..4fdc86f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 FPChatX FPChatX - 1.0.4-BETA + 1.0.5-BETA spigot-repo diff --git a/src/main/java/com/fpghoti/fpchatx/FPChat.java b/src/main/java/com/fpghoti/fpchatx/FPChat.java index c1f5161..39e0546 100644 --- a/src/main/java/com/fpghoti/fpchatx/FPChat.java +++ b/src/main/java/com/fpghoti/fpchatx/FPChat.java @@ -16,6 +16,7 @@ import org.bukkit.scheduler.BukkitTask; import com.fpghoti.fpchatx.badge.BadgeList; import com.fpghoti.fpchatx.chat.ChatChannel; +import com.fpghoti.fpchatx.chat.ChatFilter; import com.fpghoti.fpchatx.chat.ShoutChannel; import com.fpghoti.fpchatx.command.Commands; import com.fpghoti.fpchatx.command.commands.BadgeClearCommand; @@ -89,6 +90,7 @@ public class FPChat extends JavaPlugin { BadgeList.badgelist.put(i, BadgeList.badgelist.get(i).replace("&", "§")); } cache = new PlayerCache(this); + ChatFilter.loadFilter(); for(Player bp : Bukkit.getOnlinePlayers()){ FPlayer.getPlayer(bp); } @@ -133,6 +135,7 @@ public class FPChat extends JavaPlugin { BadgeList.badgelist.put(i, BadgeList.badgelist.get(i).replace("&", "§")); } cache = new PlayerCache(this); + ChatFilter.loadFilter(); for(Player bp : Bukkit.getOnlinePlayers()){ FPlayer.getPlayer(bp); } diff --git a/src/main/java/com/fpghoti/fpchatx/chat/ChatFilter.java b/src/main/java/com/fpghoti/fpchatx/chat/ChatFilter.java index 8bb4156..f40fd29 100644 --- a/src/main/java/com/fpghoti/fpchatx/chat/ChatFilter.java +++ b/src/main/java/com/fpghoti/fpchatx/chat/ChatFilter.java @@ -5,15 +5,29 @@ import java.util.ArrayList; import org.bukkit.ChatColor; import com.fpghoti.fpchatx.FPChat; +import com.fpghoti.fpchatx.permission.Permission; +import com.fpghoti.fpchatx.player.FPlayer; public class ChatFilter { public static ArrayList filtered = null; + public static ArrayList prefixfilter = null; - //CHAT FILTER + public static String[] suffixes = {"ing","s","ed","er","es","y","ers","ier","iest","ies","ys"}; - public static String filter(String sentence){ // THIS FUNCTION TAKES THE RAW CHAT MESSAGE AND SENDS EACH - String msg = sentence; // INDIVIDUAL WORD INTO THE FILTER WORD FUNCTION BELOW + public static void loadFilter() { + filtered = new ArrayList(); + for(String s : FPChat.getPlugin().getMainConfig().getNaughtyWords().split(",")) { + filtered.add(s); + } + prefixfilter = new ArrayList(); + for(String s : FPChat.getPlugin().getMainConfig().getPrefixNaughtyWords().split(",")) { + prefixfilter.add(s); + } + } + + public static String filter(String sentence){ + String msg = sentence; if(FPChat.getPlugin().getMainConfig().chatFilterEnabled()){ msg = ""; int i = 0; @@ -27,61 +41,106 @@ public class ChatFilter { return msg; } - public static String filterWord(String word){ // THIS TAKES THE WORD, LOWERCASES IT, - String wordp = ""; // AND REPLACES CHARACTERS "1" and "!" with "i", "5" with "s", "6" with "g", and - if(FPChat.getPlugin().getMainConfig().chatFilterEnabled()){ // "3" with "e". It then compares it to the list of - String word2 = word.toLowerCase(); // Naughty words and replaces it with "Frank" or "bleep" if a match is found - String color = ChatColor.getLastColors(word2); - if(word2.length() >= 2 && word2.charAt(word2.length() -1) == '!'){ - for(int i = 0; i < word2.length() -1; i++ ){ - wordp += word2.charAt(i); + public static String filterWord(String word) { + if(FPChat.getPlugin().getMainConfig().chatFilterEnabled()) { + String color = ChatColor.getLastColors(word); + String[] match = findMatchPair(word, filtered); + if(match != null) { + String suffix = match[1]; + if(suffix == null) { + suffix = ""; } - word2 = wordp; + return color + getReplacement() + suffix; } - wordp = ""; - for(int i = 0; i < word2.length(); i++ ){ - if(word2.charAt(i) != '!'){ - wordp += word2.charAt(i); - }else{ - wordp += 'i'; - } - } - word2 = wordp; - word2 = ChatColor.translateAlternateColorCodes('§', word2); - word2 = ChatColor.translateAlternateColorCodes('&', word2); - word2 = ChatColor.stripColor(word2); - word2 = word2.replaceAll("\\p{Punct}+", "").replaceAll("1", "i").replaceAll("5", "s").replaceAll("6", "g").replaceAll("3", "e"); - if(filtered == null) { - filtered = new ArrayList(); - for(String s : FPChat.getPlugin().getMainConfig().getNaughtyWords().split(",")) { - filtered.add(s); - } - } - - for(String item : filtered){ - if(ChatColor.stripColor(word2).equalsIgnoreCase(item) || ChatColor.stripColor(word2).equalsIgnoreCase(item + "s")){ - if (word2.length() > 2) { - if(word2.substring(word2.length() - 3).equalsIgnoreCase("ing")){ - word = "bleeping"; - if(FPChat.getPlugin().getMainConfig().frankModeEnabled()){ - word = "Franking"; - } - }else{ - word = "bleep"; - if(FPChat.getPlugin().getMainConfig().frankModeEnabled()){ - word = "Frank"; - } - } - } - } - word = color + word; - } - word2 = null; } return word; } + public static String filterPrefix(FPlayer p, String prefix) { + if(FPChat.getPlugin().getMainConfig().prefixFilterEnabled()) { + String color = ChatColor.getLastColors(prefix); + String[] match = findMatchPair(prefix, prefixfilter); + if(match != null) { + String found = match[0]; + String suffix = match[1]; + if(suffix == null) { + suffix = ""; + } + if(!Permission.canUseWordInPrefix(p, found)) { + return color + getReplacement() + suffix; + } + } + } + return prefix; + } - // By the end of the loop, the sentence is reconstructed with the naughty words replaced + public static String getReplacement() { + if(FPChat.getPlugin().getMainConfig().frankModeEnabled()){ + return "Frank"; + } + return "bleep"; + } + + public static String findMatch(String word, ArrayList filter) { + String[] match = findMatchPair(word,filter); + if(match == null || match[0] == null) { + return null; + } + return match[0]; + } + + public static String[] findMatchPair(String word, ArrayList filter) { + String cleaned = ""; + word = word.toLowerCase(); + if(word.length() >= 2 && word.charAt(word.length() -1) == '!'){ + for(int i = 0; i < word.length() -1; i++ ){ + cleaned += word.charAt(i); + } + word = cleaned; + } + cleaned = ""; + for(int i = 0; i < word.length(); i++ ){ + if(word.charAt(i) != '!'){ + cleaned += word.charAt(i); + }else{ + cleaned += 'i'; + } + } + cleaned = ChatColor.translateAlternateColorCodes('§', cleaned); + cleaned = ChatColor.translateAlternateColorCodes('&', cleaned); + cleaned = ChatColor.stripColor(cleaned); + cleaned = cleaned.replace(" ", ""); + cleaned = cleaned.replaceAll("\\p{Punct}+", "").replaceAll("1", "i").replaceAll("5", "s").replaceAll("6", "g").replaceAll("3", "e").replaceAll("0", "o").replaceAll("9", "g").replaceAll("8", "b"); + if(cleaned.equals("")) { + return null; + } + String[] wordSuf = {null,null}; + for(String item : filter) { + if(cleaned.equalsIgnoreCase(item)){ + wordSuf[0] = item; + return wordSuf; + } + for(String suffix : suffixes) { + if(cleaned.equalsIgnoreCase(item + suffix)){ + wordSuf[0] = item; + wordSuf[1] = suffix; + return wordSuf; + } + String last = item.substring(item.length() - 1); + if(cleaned.equalsIgnoreCase(item + last + suffix)){ + wordSuf[0] = item; + wordSuf[1] = last + suffix; + return wordSuf; + } + if(cleaned.equalsIgnoreCase(item + last + last + suffix)){ + wordSuf[0] = item; + wordSuf[1] = last + last + suffix; + return wordSuf; + } + } + + } + return null; + } } diff --git a/src/main/java/com/fpghoti/fpchatx/command/commands/PrefixCommand.java b/src/main/java/com/fpghoti/fpchatx/command/commands/PrefixCommand.java index 08a53ca..9c7956f 100644 --- a/src/main/java/com/fpghoti/fpchatx/command/commands/PrefixCommand.java +++ b/src/main/java/com/fpghoti/fpchatx/command/commands/PrefixCommand.java @@ -40,6 +40,7 @@ public class PrefixCommand extends Commands { if(Permission.canChangePrefix(p)){ String prefix = ChatColor.translateAlternateColorCodes('&', ChatFilter.filterWord(args[0])); + prefix = ChatFilter.filterPrefix(p, prefix); if(Permission.canChangePrefix(p)) { p.setPrefix(prefix); FPlayer.goodMsg(p, "Set prefix to: " + prefix); diff --git a/src/main/java/com/fpghoti/fpchatx/config/ChannelFile.java b/src/main/java/com/fpghoti/fpchatx/config/ChannelFile.java index e32f413..b625477 100644 --- a/src/main/java/com/fpghoti/fpchatx/config/ChannelFile.java +++ b/src/main/java/com/fpghoti/fpchatx/config/ChannelFile.java @@ -172,7 +172,7 @@ public class ChannelFile { } public String getWhitelistedPermissionNode() { - return config.getString("WhitelistedPermissionNode"); + return config.getString("WhitelistedPermissionNode").replace(" ", ""); } public void saveWhitelistedPermissionNode(String perm) throws IOException{ @@ -208,7 +208,7 @@ public class ChannelFile { } public String getBannedUUIDs() { - return config.getString("BannedUUIDs"); + return config.getString("BannedUUIDs").replace(" ", ""); } public void saveBannedUUIDs(String uuids) throws IOException { diff --git a/src/main/java/com/fpghoti/fpchatx/config/MainConfig.java b/src/main/java/com/fpghoti/fpchatx/config/MainConfig.java index 15810b8..5c8f226 100644 --- a/src/main/java/com/fpghoti/fpchatx/config/MainConfig.java +++ b/src/main/java/com/fpghoti/fpchatx/config/MainConfig.java @@ -97,6 +97,14 @@ public class MainConfig { config.createSection("NaughtyWords"); config.set("NaughtyWords", "word1,word2,word3"); } + if (config.get("PrefixFilter")==null){ + config.createSection("PrefixFilter"); + config.set("PrefixFilter", false); + } + if (config.get("PrefixNaughtyWords")==null){ + config.createSection("PrefixNaughtyWords"); + config.set("PrefixNaughtyWords", "trusted,helper,mod,admin"); + } if (config.get("FrankMode")==null){ config.createSection("FrankMode"); config.set("FrankMode", false); @@ -228,7 +236,15 @@ public class MainConfig { } public String getNaughtyWords() { - return config.getString("NaughtyWords"); + return config.getString("NaughtyWords").replace(" ", ""); + } + + public boolean prefixFilterEnabled() { + return config.getBoolean("PrefixFilter"); + } + + public String getPrefixNaughtyWords() { + return config.getString("PrefixNaughtyWords").replace(" ", ""); } public boolean frankModeEnabled() { diff --git a/src/main/java/com/fpghoti/fpchatx/config/PlayerFile.java b/src/main/java/com/fpghoti/fpchatx/config/PlayerFile.java index 4a51588..59c5d22 100644 --- a/src/main/java/com/fpghoti/fpchatx/config/PlayerFile.java +++ b/src/main/java/com/fpghoti/fpchatx/config/PlayerFile.java @@ -131,7 +131,7 @@ public class PlayerFile { } public String getIgnore() { - return config.getString("Ignore"); + return config.getString("Ignore").replace(" ", ""); } public void saveIgnore(String ignore) throws IOException { diff --git a/src/main/java/com/fpghoti/fpchatx/permission/Permission.java b/src/main/java/com/fpghoti/fpchatx/permission/Permission.java index d9427ed..7adbd4a 100644 --- a/src/main/java/com/fpghoti/fpchatx/permission/Permission.java +++ b/src/main/java/com/fpghoti/fpchatx/permission/Permission.java @@ -5,6 +5,18 @@ import com.fpghoti.fpchatx.player.FPlayer; public class Permission { public static String noPerm = "You lack the permission required to perform this action."; + + public static boolean canUseWordInPrefix(FPlayer p, String word) { + return p.hasPermission("fpchat.prefix." + word.toLowerCase()); + } + + public static boolean canUseWordInPrefix(String playername, String word) { + if(FPlayer.getPlayer(playername) != null) { + FPlayer p = FPlayer.getPlayer(playername); + return canUseWordInPrefix(p, word); + } + return false; + } public static boolean canUseColor(FPlayer p) { return p.hasPermission("fpchat.colorcodes"); diff --git a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java index 1d7f65d..20bbc34 100644 --- a/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java +++ b/src/main/java/com/fpghoti/fpchatx/player/FPlayer.java @@ -10,6 +10,7 @@ import java.util.logging.Level; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -275,7 +276,8 @@ public class FPlayer { public boolean setPrefix(String prefix) { if(isOnline() && getPlayer() != null) { - VaultUtil.chat.setPlayerPrefix(getPlayer(), prefix); + World world = null; + VaultUtil.chat.setPlayerPrefix(world, name, prefix); return true; } return false;