How to stop message firing twice (or more) and replace all listed words in it

Discussion in 'Spigot Plugin Development' started by Goatii, Sep 12, 2017.

  1. How do you replace every letter in a word with *'s. I can get the words from a list in config, but I can't manage to get them to all change to *'s, and can instead make any word condense into 1 *.

    Current code:
    Code (Text):
    pl.sendMessage(ChatColor.YELLOW + e.getPlayer().getDisplayName() + " " + ChatColor.GRAY + message.replaceAll(word, "*"));
    This replaces all words (in a list) in the message with one * each.
    Fixed, Thanks
    My current system works perfectly if there is only one word checked for in the sentence. However, if there is multiple ones, the message ends up being like

    Original Input: badword stop that badword1

    Outcome: ******* stop that badword1
    . . . . . . . : badword stop that ********

    Not only does it send two messages, it also censors only one per message. I've been told not to use replace in the messages, but I don't know any alternative way..

    Thanks for the help,
    #1 Goatii, Sep 12, 2017
    Last edited: Sep 12, 2017
  2. Code (Text):
    StringUtils.repeat("*", str.length());
    As for firing twice for my Smite Filter plugin to stop players being damanged multiple times, I added them to an array list and then on the event, checked if they were in it and if they were just skipped.
    Not exactly what I done but close enough.
    You'll want to not include the replacement of words in this check or you'll only get one word changed to ****
  3. I don't get how to stop it firing twice, or how to avoid the replacement of words in the check..
  4. MiniDigger


    you want something like this
    Code (Text):
    String msg = ....
    List<String> badWords = ...
    badWords.forEach(word -> msg.replace(word, StringUtils.repeat("*", word.length())));
  5. Code (Text):
                    String msg = e.getMessage();
                            List<String> badWords = words;
                            badWords.forEach(wordz -> msg.replace(word, StringUtils.repeat("*", word.length())));
    This doesn't seem to filter out the word, and instead just says it to the person. Also, I get the feeling that doing this would mean that the word "BadWoRd" would not be found if I didn't put it into the config with exact lettering, so "badword" would not be found. e.GetMessage().toLowerCase(); would work, but then other capital letters in the word would not work, which is a problem I was also having with the old system.
    • Optimistic Optimistic x 1
  6. MiniDigger


    to fix casing I would suggest to compile a list of patterns on init (Pattern.compile("(?i)" + badword) and then use those patterns to replace strings.
    not sure how well that deals with special chars tho (like Ä vs ä)

Share This Page