Question Censorship of words: replacing bad words with *

DiceMa

New member
Jul 27, 2023
4
0
1
I'm censoring the chat. Censorship works, but the plugin replaces the word, even if this word does not need to be blocked.

For example:
Blocking the word "port"
The plugin blocks as follows: Tele****ing

What check should I add to this method to make it work? swearList() this is the get method for the config.yml

private String returnCorrected(String str) { JsonObject jsonObject = (new JsonParser()).parse(str).getAsJsonObject(); JsonArray element = jsonObject.get("extra").getAsJsonArray(); for (JsonElement objects : element) { if (objects instanceof JsonObject) { JsonObject actual = (JsonObject)objects; String text = actual.get("text").getAsString(); for (String badWord : swearList()) { if (badWord.getChars();) if (text.contains(badWord)) { String repeat = StringUtils.repeat("*", badWord.length()); text = text.replace(badWord, repeat); } } actual.addProperty("text", text); } } return jsonObject.toString(); }
 
Last edited:

stefvanschie

Moderator
Staff member
Dec 17, 2021
102
3
16
18
What you probably want is to only block those when they are the entire word, not part of the word. If so, the easiest way to do that is probably to only replace the word if there's a word boundary before and after it. You can do this with regex, by looking for the pattern \b<word>\b where <word> is the word you want to replace.