Replace bad words?

Discussion in 'Plugin Development' started by CostelabrMn, Jul 3, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    CostelabrMn

    [​IMG]

    Hi guys! Well, i'd like to make an anti-curse plugin to my server, and basically, im doing a system that replace the bad words, but i have no idea how to do this.

    For example, when an player says: "Go F*ck yourself", in chat, his message will be: "Go **** yourself".


    Cya.
     
    Last edited: Jul 3, 2016
  2. Offline

    SuperSniper

    AsyncPlayerChatEvent
    get the message
    if it contains a bad word
    replace it
     
  3. Offline

    ChipDev

    Exactly. A little more tips for logic would be:
    Turn the message into an List of strings by splitting the message by spaces,
    if one of the strings equals a bad word,
    get the length of the word, and replace it with * multiplied by the word length.
    OR, You could just remove spaces, " _ ", " - " "," etc and then check if there are bad words.
    See, because
    F-_,o-o wouldn't be caught with the first logic.
    AND NO CHILD SHOULD EVER HEAR "FOO"!
    SORRY MOMMY CHIPDEV SAID FOO!
    WHAT? JIMMY? FOO? AHHHH! GET OFF BUKKIT!
     
  4. Offline

    Zombie_Striker

    @ChipDev @CostelabrMn
    Not only should you remove spaces or dashes, but you should also remove periods, commands, and any other marks to separate words, and replace letters in words to similar looking letters letters (I.e "0" and "O", or "7" and "T", ect.). You don't want "f.0o" to not be censored if you're banning the word "foo".
     
  5. Offline

    ChipDev

    Its a hard process, that was just an example.
    Please, somebody make a resource to convert FO0o to "FOOO" (It was F - Capital O - Zero - Lowercase o) :p
     
  6. Offline

    CostelabrMn

    @ChipDev

    This 'list' that you said, is like this?

    upload_2016-7-3_15-31-28.png

    I've tried with List<String> but that didn't work.
     

    Attached Files:

  7. Offline

    Zombie_Striker

    @CostelabrMn
    Don't use arrays for this. Lists have the ".contains" method which will make searching for those words easier.
    Just use the following:
    Code:
    List<String> badwords = new ArrayList<String>();
    badwords.add("badword");
    //Make sure all the words are lower case. You can then check if a word is a bad word by using the following
    if(badwords.contains(word.toLowerCase())){
     
    bwfcwalshy likes this.
  8. Offline

    CostelabrMn

  9. Offline

    SirGregg

    @CostelabrMn
    Please don't ask for someone to spoon feed you code :)

    Under the if (badwords.contains(word.toLowerCase())) {
    you should replace the word(s) with a character such as * or simply remove it. You could also send the player a message such as: Please do not use such filthy language.
     
  10. Offline

    CostelabrMn

    @SirGregg im really sorry. Thx for warning!

    @Zombie_Striker sorry, but that code still not working, can you help me? Eclipse says that i have to cast badwords to (CharSequence). Thx again bro :)
     
  11. Offline

    Zombie_Striker

  12. Offline

    CostelabrMn

    @Zombie_Striker that's the code, but that's the error:
    upload_2016-7-3_22-10-45.png



    Code:
    package me.costelabr.AntiSwear;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class Antis implements Listener{
    
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            List<String> badwords = new ArrayList<String>();
            badwords.add("example");
            if(e.getMessage().contains(badwords)){
                e.getMessage().replace(badwords, "****");
            }
           
        }
       
       
       
       
       
        
     
  13. Offline

    Zombie_Striker

    @CostelabrMn
    I think you misunderstood what those variables mean. What you are checking is if a message contains an entire list of bad words. What you need to do is test if the badwords list contains a word from that message.

    Simply put, let me just explain again what you need to do:
    1. Create a new String that will be equal to the message, but you will replace all commas, underscores, dashes, and periods with spaces. After that, the message will also be converted to lower case. This will allow for better detection for bad words.
    2. Create a "String array "(String[]) that will split the string by spaces. In other words, each value in the array will be just one word.
    3. Create a for int loop to loop through all the values inside the array. Basically, you are now going through each word to test if it's a bad word.
    4. After that, you will check if the "badwords" List contains the word.
    5. If it does contain the word, then censor that word by replacing it with those asterisks.
    6. Finally, rebuild the string and set the message.
    If something does not work or you do not know how to do something, post your code and what you are having trouble with.
     
  14. Offline

    CostelabrMn

    @Zombie_Striker srry, i still dont understand, (im really sorry, i dont understand english so good), can you show me an example? With code?
     
  15. Offline

    mc_myster

    @CostelabrMn tbh i wouldn't use an ArrayList to check for bad words, why not use config string list? is all that is, is a string consiting of multiple strings, for example, instead of using :
    string1: string2

    you would use:
    string1:
    - string2
    - string3
    - string4
    and then when the player speaks check if the messages contains any of the strings from the string list by creating a for() statement then checking if the message contains any words from the stringlist.
     
  16. You we're actually very close with this code. The thing is that doing anything with
    Code:
    e.getMessage()
    doesn't actually change the message in the event. You have to use
    Code:
    e.setMessage()
    to set the message being displayed. Also, when you use the replace method on the message, don't use the actual list of strings as an argument, you need to put in an entry of the list.
     
Thread Status:
Not open for further replies.

Share This Page