java - AsyncPlayerChatEvent, Trying to see if a string from a string array is contained within another string from a second string array -
i making plugin bukkit minecraft server running version 1.6.4. plugin supposed stop players spamming, censor swear words allow words have swear words embedded in them (e.g. hello, molasses). have created 2 string arrays. 1 containing swear words , second containing "allowed" words.
string[] curse = {"swear1", "swear2", "etc..."}; string[] allowed = {"allowed1", "allowed2", "etc..."};
what trying whenever player speaks in chat, have plugin check message swears while ignoring spaces , special characters.
here have far (sorry if it's bit messy):
public int seconds1 = 0; public int seconds2 = 0; public int spam = 0; @eventhandler public void onplayerchat(asyncplayerchatevent event){ player player = event.getplayer(); string name = player.getname(); seconds2 = seconds1; seconds1 = ((int) (system.currenttimemillis())); if (seconds1 - seconds2 <= 2500){ spam++; switch (spam){ case 3: player.damage(1); player.sendmessage(chatcolor.gold + "spam warning: " + chatcolor.red + "do not spam! spamming reduces health."); break; case 4: player.damage(3); break; case 6: player.damage(5); player.sendmessage(chatcolor.gold + "spam warning: " + chatcolor.red + "i'm warning you."); break; case 7: player.damage(6); break; case 8: player.damage(10); player.kickplayer("what did tell spamming?"); for(player p : bukkit.getonlineplayers()){ p.sendmessage(chatcolor.blue + name + chatcolor.red + " has been kicked spamming!"); } spam = 0; break; } }else if(seconds1 - seconds2 > 2000){ spam = 0; } string message = event.getmessage().tolowercase().replace("-", "").replace(" ", "").replace(",", "") .replace(".", "").replace("~", "").replace("!", "").replace("@", "").replace("#", "") .replace("$", "").replace("%", "").replace("^", "").replace("&", "").replace("*", "") .replace("(", "").replace(")", "").replace("_", "").replace("+", "").replace("=", "") .replace("{", "").replace("}", "").replace("[", "").replace("]", "").replace("|", "") .replace("'", "").replace("<", "").replace(">", "").replace("/", "").replace("?", "") .replace("`", "").replace("¸", "").replace("^", "").replace("@", "a"); string[] curse = {"all", "curse", "words"}; //i won't list swears in post. string[] allowed = {"all", "allowed", "words"}; for(string blacklist : curse){ if(message.contains(blacklist)){ (int = 0; < allowed.length; i++){ if(allowed[i].contains(blacklist)){ event.setcancelled(true); }else{ event.setcancelled(true); player.chat(message.replaceall(blacklist, "*bleep*")); player.damage(5); return; } } } } }
the thing not working trying find swear words within allowed words. whatever have in code isn't working.
i'd try use regular expressions you're trying do. try like
string[] swearwords = {"all", "swear", "words", "in", "here"}; // replaces every punctuation symbol except @ space string message = event.getmessage().tolowercase().replaceall("@", "a").replaceall("\\p{punct}", " "); for(string word : swearwords){ if(message.matches("(.* )?"+word+"( .*)?")) { // there has been swear word in message event.setmessage(event.getmessage().replaceall(word, "*censored*")); } else { // current swear word not found in message. } }
this has higher performance ;)
please note: has been written phone, please apologize bad formatting , :)
Comments
Post a Comment