我正在为运行1.6.4版本的Bukkit Minecraft服务器制作一个插件。该插件应该阻止玩家发送垃圾邮件,以及审查发誓的单词,但允许某些单词中嵌入咒骂的单词(例如HELL,molASSes)。我创建了两个字符串数组。一个包含所有咒骂词,第二个包含"允许"话。
String[] curse = {"Swear1", "Swear2", "etc..."};
String[] allowed = {"Allowed1", "Allowed2", "etc..."};
我想要的是每当玩家在聊天中说话时,让插件检查消息是否有任何咒骂而忽略空格和特殊字符。
这是我到目前为止所做的事情(对不起,如果它有点太乱):
public int seconds1 = 0;
public int seconds2 = 0;
public int spam = 0;
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event){
Player player = event.getPlayer();
String name = player.getName();
seconds2 = seconds1;
seconds1 = ((int) (System.currentTimeMillis()));
if (seconds1 - seconds2 <= 2500){
spam++;
switch (spam){
case 3: player.damage(1);
player.sendMessage(ChatColor.GOLD + "Spam Warning: " + ChatColor.RED + "Do not spam! Spamming reduces your health.");
break;
case 4: player.damage(3);
break;
case 6: player.damage(5);
player.sendMessage(ChatColor.GOLD + "Spam Warning: " + ChatColor.RED + "I'm warning you.");
break;
case 7: player.damage(6);
break;
case 8: player.damage(10);
player.kickPlayer("What did I tell you about spamming?");
for(Player p : Bukkit.getOnlinePlayers()){
p.sendMessage(ChatColor.BLUE + name + ChatColor.RED + " has been kicked for spamming!");
}
spam = 0;
break;
}
}else if(seconds1 - seconds2 > 2000){
spam = 0;
}
String message = event.getMessage().toLowerCase().replace("-", "").replace(" ", "").replace(",", "")
.replace(".", "").replace("~", "").replace("!", "").replace("@", "").replace("#", "")
.replace("$", "").replace("%", "").replace("^", "").replace("&", "").replace("*", "")
.replace("(", "").replace(")", "").replace("_", "").replace("+", "").replace("=", "")
.replace("{", "").replace("}", "").replace("[", "").replace("]", "").replace("|", "")
.replace("'", "").replace("<", "").replace(">", "").replace("/", "").replace("?", "")
.replace("`", "").replace("¸", "").replace("^", "").replace("@", "a");
String[] curse = {"All", "Curse", "Words"};
//I won't list the swears in this post.
String[] allowed = {"All", "Allowed", "Words"};
for(String blacklist : curse){
if(message.contains(blacklist)){
for (int i = 0; i < allowed.length; i++){
if(allowed[i].contains(blacklist)){
event.setCancelled(true);
}else{
event.setCancelled(true);
player.chat(message.replaceAll(blacklist, "*bleep*"));
player.damage(5);
return;
}
}
}
}
}
唯一不起作用的是试图在允许的单词中找到咒骂词。无论我目前在我的代码中有什么作品都没有用。
答案 0 :(得分:1)
我会尝试使用regular expressions来做你想做的事情。尝试像
这样的东西String[] swearWords = {"all", "swear", "words", "in", "here"};
// This replaces every punctuation symbol except @ by a space
String message = event.getMessage().toLowerCase().replaceAll("@", "a").replaceAll("\\p{Punct}", " ");
for(String word : swearWords){
if(message.matches("(.* )?"+word+"( .*)?")) {
// There has been a swear word in the message
event.setMessage(event.getMessage().replaceAll(word, "*censored*"));
} else {
// The current swear word was not found in the message.
}
}
这甚至具有更高的性能;)
请注意:这是我手机上写的,所以请为糟糕的格式等道歉:)