我目前有这段代码:
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);
}
它将持续大约6倍......我正在尝试将其浓缩成类似的东西:
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);
}
但我正在努力寻找包含这个词的号码(PBSwears.words.word1或PBSwears.words.word2)。我尝试过这样的事情:
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';
}
但是再一次,如果有陈述,还有很多其他的。 另外,我正在读取配置文件中的值,如下所示:
PBSwears:
blocked:
word1: *insert vulgar language*
word2: *insert vulgar language*
replace:
word1: *insert funny replacement*
word2: *insert funny replacement*
任何见解都会非常有用。
答案 0 :(得分:1)
我建议加载一个地图,其中包含由被阻止的单词键入的替换单词。然后你可以利用这样的方法:
public Map<String, String> replacementWords = new HashMap<String, String>();
public String getWordReplacement(String word)
{
if (replacementWords.containsKey(word) {
return replacementWords.get(word);
}
return word;
}
您必须预先填充地图,类似于:
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);
}
}
答案 1 :(得分:0)
以下唯一的事情是必须定义MAX_COUNT
。如果您知道自己要进行6次检查,请将其定义为6,依此类推。
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;
}
然后
String number = "" + getCorrectNumber();
答案 2 :(得分:0)
你为什么不迭代?
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);
}
}