情况和问题:
我想在他们输入脏话时(不区分大小写)踢球员。 以下是我的尝试,但它不会踢球员。我做错了什么? 根本不显示任何错误。
源代码:
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
if (muted.contains(player.getUniqueId())) {
e.setCancelled(true);
player.sendMessage(ChatColor.DARK_BLUE + "[TC]" + ChatColor.DARK_RED + "You are muted and cannot chat.");
} else {
String message = e.getMessage();
if (message.contains("(?i)fuck") || message.contains("(?i)shit") || message.contains("(?i)ass") || message.contains("(?i)porno") || message.contains("(?i)porn") || message.contains("(?i)crap") || message.contains("(?i)dumb")) {
player.kickPlayer(ChatColor.DARK_RED + "Watch your language, please!");
e.setCancelled(true);
}
message = ChatColor.translateAlternateColorCodes('§', message);
message = ChatColor.translateAlternateColorCodes('&', message);
String text = ChatColor.DARK_BLUE + "[Chat]" + ChatColor.YELLOW + player.getName() + " " + ChatColor.DARK_RED + "Says: " + ChatColor.AQUA + message;
player.playNote(player.getLocation(),Instrument.PIANO, Note.natural(1, Tone.A));
text.replace("(?i)", "");
text = text.replace("<3", "❤");
text = text.replace(":)", "☺");
text = text.replace(":-)", "☺");
text = text.replaceAll("(?i)fuck", "****");
text = text.replaceAll("(?i)ass", "***");
text = text.replaceAll("(?i)shit", "****");
text = text.replaceAll("(?i)porno", "*****");
text = text.replaceAll("(?i)porn", "****");
text = text.replaceAll("(?i)dumb", "****");
text = text.replaceAll("(?i)crap", "****");
e.setFormat(text);
e.setMessage(text);
}
}
答案 0 :(得分:-2)
使用String.matches匹配正则表达式。此外,您可以将所有发誓的单词添加到某个数组/集合中,并使用Java 8功能来简化代码,例如。
public static void main(String[] args) {
String message = "hello fuck world ass shit here i am";
final String[] swearWords = {"ass", "fuck", "shit", "porno", "porn"};
final boolean hasSwearWords = Stream.of(swearWords).anyMatch(w -> message.matches("(?i).*\\s" + w + "\\s.*"));
if (hasSwearWords) {
System.out.printf("Bad user!");
}
final String cleanMessage = Stream.of(swearWords).reduce(message,
(msg, w) -> msg.replaceAll(
"(?i)(\\s)" + w + "(\\s)",
"$1" + new String(new char[w.length()]).replace("\0", "*")+ "$2"));
System.out.println(cleanMessage);
}
以下是不需要Java 8的代码:
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
if (muted.contains(player.getUniqueId())) {
e.setCancelled(true);
player.sendMessage(ChatColor.DARK_BLUE + "[TC]" + ChatColor.DARK_RED + "You are muted and cannot chat.");
} else {
String message = e.getMessage();
String[] swearWords = {"fuck", "shit", "ass", "porno", "porn", "crap", "dumb"};
for (String word : swearWords) {
if (message.matches("(?i).*\\s" + word + "\\s.*")) {
player.kickPlayer(ChatColor.DARK_RED + "Watch your language, please!");
e.setCancelled(true);
break;
}
}
message = ChatColor.translateAlternateColorCodes('§', message);
message = ChatColor.translateAlternateColorCodes('&', message);
String text =
ChatColor.DARK_BLUE + "[Chat]" + ChatColor.YELLOW + player.getName() + " " + ChatColor.DARK_RED + "Says: " +
ChatColor.AQUA + message;
player.playNote(player.getLocation(), Instrument.PIANO, Note.natural(1, Tone.A));
text = text.replace("<3", "❤")
.replace(":)", "☺")
.replace(":-)", "☺");
for (String word : swearWords) {
text = text.replaceAll(
"(?i)(\\s)" + word + "(\\s)",
"$1" + new String(new char[word.length()]).replace("\0", "*")+ "$2");
}
e.setFormat(text);
e.setMessage(text);
}
}