1

我正在为运行版本 1.6.4 的 Bukkit Minecraft 服务器制作插件。该插件应该阻止玩家发送垃圾邮件,以及审查脏话,但允许某些包含脏话的单词(例如 HELLo、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;

                }

            }

        }

    }

}

唯一不起作用的是试图在允许的单词中找到脏话。我目前在我的代码中的任何东西都不起作用。

4

1 回答 1

1

我会尝试使用正则表达式来做你想做的事情。尝试类似的东西

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.
  }
}

这甚至具有更高的性能;)

请注意:这是从我的手机写的,所以请对格式错误等表示歉意:)

于 2013-10-08T05:17:27.510 回答