1
我正在爲運行1.6.4版的Bukkit Minecraft服務器製作一個插件。該插件應該阻止玩家發送垃圾郵件,以及審查員發誓的單詞,但允許某些詞中嵌入發誓詞(例如HELLo,molleses)。我創建了兩個字符串數組。一個包含所有發誓的單詞,第二個包含「允許」的單詞。AsyncPlayerChatEvent,嘗試查看字符串數組中的字符串是否包含在來自第二個字符串數組的另一個字符串中
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;
}
}
}
}
}
不工作試圖找到發誓允許的話中話的唯一的事情。無論我目前在我的代碼中有什麼不起作用。