Question

I currently have this code:

    if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word1")))
    {
        String message = event.getMessage().replaceAll(plugin.getConfig().getString("PBSwears.blocked.word1"), plugin.getConfig().getString("PBSwears.replace.word1"));
        event.setMessage(message);
    } 
    else if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word2")))
    {
        String message = event.getMessage().replaceAll(plugin.getConfig().getString("PBSwears.blocked.word2"), plugin.getConfig().getString("PBSwears.replace.word2"));
        event.setMessage(message);
    }

And it is going to go on for about 6 times that...I'm trying to condense it into something like:

    String number = {find whitch number the message contains};
    if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word" + number)))
    {
        String message = event.getMessage().replaceAll(plugin.getConfig().getString("PBSwears.blocked.word" + number), plugin.getConfig().getString("PBSwears.replace.word" + number));
        event.setMessage(message);
    } 

But I'm struggling with trying to find which number the word is contained in(PBSwears.words.word1 or PBSwears.words.word2). I've tried something like this:

    char number='0';
    if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word1")))
    {
        number='1';
    }
    else if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word2")))
    {
        number='2';
    }

But then once again there is a long amount of else if statements. Also, I'm reading the values out of a config file that looks like this:

PBSwears:
  blocked:
    word1: *insert vulgar language*
    word2: *insert vulgar language*
  replace:
    word1: *insert funny replacement*
    word2: *insert funny replacement*

Any insight would be very helpful.

Was it helpful?

Solution

Why don't you iterate?

String[] badWords = {"word1", "word2", ...};
for (int i=0; i<badWords.length; i++){

  if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words." + badWords[i]))){
     String message = event.getMessage().replaceAll(plugin.getConfig().getString("PBSwears.blocked." + badWords[i]), plugin.getConfig().getString("PBSwears.replace." + badWords[i]));
     event.setMessage(message);
  }
}

OTHER TIPS

I would recommend loading a Map with the replacement words keyed by the blocked words. Then you could leverage a method like this:

public Map<String, String> replacementWords = new HashMap<String, String>();
public String getWordReplacement(String word)
{
   if (replacementWords.containsKey(word) {
      return replacementWords.get(word);
   }

   return word;
}

You would have to pre-populate your map similar to this:

public void populateReplacementWordMap() {
   int numberOfWords = 6;
   for (int i = 1; i <= numberOfWords; i++) {
      String blockedWord = plugin.getConfig().getString("PBSwears.blocked.word" + i);
      String replacementWord = plugin.getConfig().getString("PBSwears.replace.word" + i);

      replacementWords.put(blockedWord, replacementWord);
   }
}

The only thing in the following is that MAX_COUNT would have to be defined. If you know you're going to be checking 6 times, then define it as 6, and so on.

public int getCorrectNumber() {
    for(int i = 1; i <= MAX_COUNT; i ++) {
        if(event.getMessage().contains(plugin.getConfig().getString("PBSwears.words.word" + i))) {
            return i;
        }
    }

    return -1;
}

And then

String number = "" + getCorrectNumber();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top