Code event.setMessage(newMessage); fix?

Discussion in 'Plugin Development' started by Meatiex, Jul 25, 2013.

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

    Meatiex

    ok, I posted here about making this code and it works fine with one replace, but I would like to replace more than one bad word...

    Code:java
    1. package me.Meatie.plugin;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.AsyncPlayerChatEvent;
    6. public class badwords implements Listener {
    7.  
    8. @EventHandler
    9. public void onChat(AsyncPlayerChatEvent event){
    10. String newMessage = event.getMessage().replace("hate", "like");
    11. String newMessage2 = event.getMessage().replace("gay", "happyy");
    12. //It told me to do it like this instead of "String newMessage"
    13. //event.getMessage().replace("fag", "bro");
    14. event.setMessage(newMessage);
    15. event.setMessage(newMessage2);
    16. }
    17. }


    Any help? thanks!
     
  2. Offline

    Milkywayz

    Code:
    @EventHandler
        public void onChat(AsyncPlayerChatEvent event){
            String msg = event.getMessage().replace("hate", "like").replace("gay", "happyy");
            event.setMessage(msg);
        }
    Thats usually how I do it if I was simply replacing a few strings.
     
  3. Offline

    soulofw0lf

    String s = event.getMessage();
    s = s.replace("hate", "love).replace("gay", "happy").replace("Bad", "Good").replace(..... i hope you get it?
    event.setMessage(s);

    ouch ninjad by a galaxy :)
     
  4. Offline

    Meatiex

    Thanks! any way to add a blacklist of words like Grass coming out "Grbutt" because of "ass" ?
     
  5. Offline

    soulofw0lf

    um yeah but it's a bit more indepth
    String[] args = event.getMessage().split(" ");
    for (String word : args){
    if (word.equalsIgnoreCase("Ass")){
    word = "butt";
    }
    //all your check in here
    }
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < args.length; i++) {
    buffer.append(' ').append(args);
    }
    String s = buffer.toString();
    event.setMessage(s);
     
  6. Offline

    Meatiex

    you shure i can't just do something like this?
    Code:java
    1. if (word.equalsIgnoreCase("Grass")){
    2. event.setCanceled;
    3. }
     
  7. Offline

    soulofw0lf

    well if you do that and i type "grass ass hate rape death" no message will go through.. and if i type "i would like to take a picnic on the grass" no message will go through.
     
  8. Offline

    Meatiex

    oh, i forgot its the chat event :p hmm, can you think of any other way?
     
  9. Offline

    soulofw0lf

    the way i gave you is the best most reliable way and the way the profanity filter is being applied for my rpg chat plugin.:)
     
  10. Offline

    Milkywayz

    Perfect chat filtering is not easy, I've never attempted to filter words out specifically because swearing is allowed on my server and even if it is excessive, theres moderators to handle it.

    Anyways, what I can do is show you how to efficiently filter chat for caps and links?
    code (open)


    It force lower cases when a chat message has over 3 letters that are caps and the whole message contains 70% caps, also if the threshold decreases to 45% if the message has over 40 characters.
     
  11. Offline

    Meatiex

    also, for the chat replacing thing, couldn't i just do this? I think it would work the same...
    Code:java
    1. .replace(" ass ", " butt ")

    //Sorry for all the bad words...
     
  12. Offline

    soulofw0lf

    yeah unless i type Ass or aSs or aSS or ASs or AsS there's a reason .equalsIgnoreCase is prefered ((though i may be wrong and replace may ignore case, but i kinda doubt it))
     
  13. Offline

    Meatiex

    oh, its a lot of code for one word... I guess i'll replace it. But I don't really understand what it does... would it block you from saying a$$ or grass?
     
  14. Offline

    soulofw0lf

    it just ignore upper and lowercase and matches exact words so it would not block a$$ or grass just and variation of Ass AsS ASS ect..
     
  15. Offline

    Meatiex

    oh that helps, Thanks!
    ...but how would I allow saying Grass? Ill test it in the morning, thanks for your help!
    like this??
    Code:java
    1.  
    2. if (word.equalsIgnoreCase("Ass")){
    3. if (!word.equalsIgnoreCase("grass")){
     
  16. Offline

    soulofw0lf

    you don't have to do anything at all for grass, it only matches exact words so it has nothing to do with grass
     
  17. Offline

    Scr3am

    Java is CaSe SeNsItIvE as you know.
    Code:
    .equalsIgnoreCase
    enables the plugin to block the word ass no matter what the capitalization is.

    If you have:
    Code:
    .replace (ass)
    it will only block the word if the user spells ass (no capitalized letters). However if the user types ASS the plugin would not block it.

    That is why you use .equalsIgnoreCase.

    EDIT: Nevermind. You understand it now. xD
     
  18. Offline

    Meatiex

    yes, but doesn't that mean you could type in asss and it would show up?

    This is what showed up when I chated anything...
    Code:
    08:08:58 [INFO] <Meatie>  [Ljava.lang.String;@2afa5456
    08:09:01 [INFO] <Meatie>  [Ljava.lang.String;@2b284cfc
    08:09:05 [INFO] <Meatie>  [Ljava.lang.String;@1342256f
    08:09:06 [INFO] <Meatie>  [Ljava.lang.String;@3dcbeabf
    08:09:08 [INFO] <Meatie>  [Ljava.lang.String;@500e1b57
    08:09:18 [INFO] <Meatie>  [Ljava.lang.String;@2da6188
    08:09:19 [INFO] <Meatie>  [Ljava.lang.String;@184f4477
    08:09:21 [INFO] <Meatie>  [Ljava.lang.String;@430ebd93
    08:09:22 [INFO] <Meatie>  [Ljava.lang.String;@71141793
    08:09:26 [INFO] <Meatie>  [Ljava.lang.String;@1c5c5ab6
    08:09:28 [INFO] <Meatie>  [Ljava.lang.String;@47ab000c
    With this code:
    Code:java
    1. package me.Meatie.plugin;
    2.  
    3. import org.bukkit.event.EventHandler;
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.event.player.AsyncPlayerChatEvent;
    6. public class badwords implements Listener {
    7.  
    8. @EventHandler
    9. public void onChat(AsyncPlayerChatEvent event){
    10.  
    11. String[] args = event.getMessage().split(" ");
    12. for (String word : args){
    13. if (word.equalsIgnoreCase("Ass")){word = "butt";}
    14. if (word.equalsIgnoreCase("i")){word = "I";}
    15. if (word.equalsIgnoreCase("hate")){word = "love";}
    16. if (word.equalsIgnoreCase("vagina")){word = "sweets";}
    17. if (word.equalsIgnoreCase("penis")){word = "twig";}
    18. if (word.equalsIgnoreCase("dick")){word = "twig";}
    19. if (word.equalsIgnoreCase("cock")){word = "twig";}
    20. if (word.equalsIgnoreCase("rectum")){word = "toilet";}
    21. if (word.equalsIgnoreCase("cunt")){word = "sweets";}
    22. if (word.equalsIgnoreCase("fuck")){word = "love";}
    23. if (word.equalsIgnoreCase("fuc")){word = "love";}
    24. if (word.equalsIgnoreCase("fuk")){word = "love";}
    25. if (word.equalsIgnoreCase("rape")){word = "tickle";}
    26. if (word.equalsIgnoreCase("slut")){word = "woman";}
    27. if (word.equalsIgnoreCase("bitch")){word = "sister";}
    28. if (word.equalsIgnoreCase("bastard")){word = "brother";}
    29. if (word.equalsIgnoreCase("shit")){word = "shoot";}
    30. if (word.equalsIgnoreCase("crap")){word = "shoot";}
    31. if (word.equalsIgnoreCase("asshole")){word = "toilet";}
    32. if (word.equalsIgnoreCase("ass")){word = "hand";}
    33. if (word.equalsIgnoreCase("horny")){word = "magical";}
    34. if (word.equalsIgnoreCase("gay")){word = "happy";}
    35. if (word.equalsIgnoreCase("rape")){word = "touch me";}
    36. if (word.equalsIgnoreCase("nigger")){word = "cutie";}
    37. if (word.equalsIgnoreCase("pussy")){word = "cat";}
    38. if (word.equalsIgnoreCase("puss")){word = "cat";}
    39. if (word.equalsIgnoreCase("sex")){word = "dinner";}
    40. if (word.equalsIgnoreCase("homo")){word = "happy";}
    41. if (word.equalsIgnoreCase("suck balls")){word = "rule my world";}
    42. if (word.equalsIgnoreCase("boob")){word = "hand";}
    43. if (word.equalsIgnoreCase("tit")){word = "hand";}
    44. if (word.equalsIgnoreCase("nipple")){word = "circle";}
    45. if (word.equalsIgnoreCase("tampon")){word = "wig";}
    46. if (word.equalsIgnoreCase("piss")){word = "pee";}
    47. if (word.equalsIgnoreCase("condom")){word = "pocket";}
    48. if (word.equalsIgnoreCase("dumbass")){word = "hot stuff";}
    49. }
    50. StringBuilder buffer = new StringBuilder();
    51. for (int i = 0; i < args.length; i++) {
    52. buffer.append(' ').append(args);}
    53. String s = buffer.toString();
    54. event.setMessage(s);
    55. }
    56. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page