Replace chat words???

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

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

    Meatiex

    I have tried before to do this, but could never figure it out, any help with replacing worlds with other words?
    This code dosnt work...
    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. event.getMessage().replace("i", "I");
    11. event.setMessage(event.getMessage());
    12. }
    13. }


    Here are some sources i used to make this code: one, two
     
  2. Offline

    Alex5657

    Err... you just get the message, replace the i with an I, get rid of it and set the chat message to itself. Let me correct it for you

    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("i", "I");
    11. event.setMessage(newMessage);
    12. }
    13. }


    Also, this will replace every "i". So when I say "hi" it will be "hI".

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
    Samthelord1 likes this.
  3. Offline

    Compressions

    Meatiex
    Code:
    e.getMessage().replaceAll("i", "I");
    Replacing all instances of "i" is probably more desirable.
     
  4. Offline

    Meatiex

    Thank you thp, im testing if it works!
    lol, it was an example, and i think i would do this if i was going to do I.
    Code:
    event.getMessage().replaceAll(" i ", " I ");
    Thanks @Compressions
     
  5. Offline

    Alex5657

    "So do i" "i want cake"
    If you do this it won't work on those two ;)
    If you were doing "i" you should do this:
    Code:
    event.getMessage().replaceAll(" i ", " I ");
    event.getMessage().replaceAll("i ", "I ");
    event.getMessage().replaceAll(" i", " I");
     
  6. Offline

    Meatiex

    Good idea, Thanks!
    How would I add ignore cases? like saying grass and not replacing the "ass" part?
     
  7. Offline

    TimmyTopHat

    That won't do at all. If you use the aforementioned code, "Hi there" would become "HI there", and "an iteration" would become "an Iteration". Instead, take the input string and split it into individual words (ignoring spaces). Then, for-loop through all of the words and replace any instances of the word "i" with "I". You can also use this to censor words, making "ass" and "grass" easy to distinguish from each other.
     
  8. Offline

    Gerka


    Ohh it is so easy

    String newMessage = event.getMessage().replaceAll("ass", "***");
    event.setMessage(newMessage);
    newMessage = event.detMessage().replaceAll("gr***", "grass");
     
  9. Offline

    DevilMental

    Or you can just set an array with bad words and check if the word is in the message :
    Code:java
    1. public static String[] badWords = { "fuck", "ass", "bitch" };

    And then :
    Code:java
    1. String message = event.getMessage().toLowerCase();
    2. List<String> newMessage = new ArrayList<String>();
    3. for (String s : message.split(" ")){ // check every word
    4. if (badWords.contains(s)){
    5. newMessage.add(StringUtils.repeat('*', s.length());
    6. } else {
    7. newMessage.add(s);
    8. }
    9. }
    10. event.setMessage(StringUtils.join(newMessage, ' '));


    Tag me if you want more explaination or information about this.
     
  10. Offline

    Dragonphase

    Gerka You would have to re-replace all instances of words that would normally end in "ass".

    Meatiex Assuming you have a list of blacklisted words and replacement words, you could iterate through each word in your message and see if that word matches a blacklisted word. Then, you replace that blacklisted word with the appropriate replacement word:

    Code:java
    1. HashMap<String, String> blacklist = new HashMap<String, String>();
    2. blacklist.add("ass", "butt");
    3. blacklist.add("fuck", "fudge");
    4.  
    5. String message = e.getMessage();
    6.  
    7. for (String word: message.split("[\\p{P} \\t\\n\\r]")){
    8. for (String blackword : blacklist.keySet()){
    9. if (word.equalsIgnoreCase(blackword)){
    10. message.replace(word, blacklist.get(blackword));
    11. break;
    12. }
    13. }
    14. }
    15.  
    16. e.setMessage(message);
     
Thread Status:
Not open for further replies.

Share This Page