Overhaul Chat Filter

This commit is contained in:
Ghoti 2020-02-13 00:40:44 -06:00
parent 68b138949d
commit adc0f41a87
10 changed files with 152 additions and 59 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>FPChatX</groupId>
<artifactId>FPChatX</artifactId>
<version>1.0.3-BETA</version>
<version>1.0.5-BETA</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>FPChatX</groupId>
<artifactId>FPChatX</artifactId>
<version>1.0.4-BETA</version>
<version>1.0.5-BETA</version>
<repositories>
<repository>
<id>spigot-repo</id>

View File

@ -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);
}

View File

@ -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<String> filtered = null;
public static ArrayList<String> 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<String>();
for(String s : FPChat.getPlugin().getMainConfig().getNaughtyWords().split(",")) {
filtered.add(s);
}
prefixfilter = new ArrayList<String>();
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<String>();
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<String> filter) {
String[] match = findMatchPair(word,filter);
if(match == null || match[0] == null) {
return null;
}
return match[0];
}
public static String[] findMatchPair(String word, ArrayList<String> 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;
}
}

View File

@ -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);

View File

@ -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 {

View File

@ -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() {

View File

@ -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 {

View File

@ -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");

View File

@ -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;