如何检查字符串列表是否包含确切的字符串?
How to check if string list contains an exact string?
我想做的是为我的服务器制作一个 Anti-swear 插件。它工作正常,但玩家可以轻松绕过它。假设在我的配置中列出了“测试”一词。如果玩家在聊天中正常输入“测试”一词,插件将检测到它。但是如果玩家输入,例如“testttttttt”,插件将不会检测到它。我怎样才能让它检测到这个词,只要它就在那里?
这是我目前的代码:
public class EventsClass implements Listener {
AntiSwear plugin = AntiSwear.getPlugin(AntiSwear.class);
// Message when player joins the server
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0");
// The punishment system
}
@EventHandler
public void chatevent(AsyncPlayerChatEvent event) {
for (String s : event.getMessage().split(" ")) {
if (plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "§lProfanity is not allowed on this server!");
// The punishment itself
Bukkit.getScheduler().runTask(plugin, () -> {
event.getPlayer().damage(10);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "smite " + event.getPlayer().getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco take " + event.getPlayer().getName() + " 100");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "burn " + event.getPlayer().getName() + " 5");
plugin.getServer().broadcastMessage(ChatColor.GOLD + "§lPlayer " + event.getPlayer().getName() + " §lused a swear word and has been punished!");
});
}
}
}
}
你试过这个吗?既然您希望命中任何匹配项,而不是拆分和检查列表中的 contains,为什么不对所有“发誓”单词的字符串执行 contains。所以基本上是从你正在做的事情中进行反向搜索。我假设您仅将 String 用于搜索而不用它做任何其他事情。
您需要遍历列表并为每个项目执行 contains
检查,而不是检查完全匹配的列表:
List<String> swears = plugin.getConfig().getStringList("swears")
.stream()
.map(swear -> swear.toLowerCase() )
.collect(Collectors.toList());
for(String s : event.getMessage().split(" ")){
if(swears.stream().anyMatch(swear -> s.toLowerCase().contains(swear)){
event.setCancelled(true);
//...
}
}
我想做的是为我的服务器制作一个 Anti-swear 插件。它工作正常,但玩家可以轻松绕过它。假设在我的配置中列出了“测试”一词。如果玩家在聊天中正常输入“测试”一词,插件将检测到它。但是如果玩家输入,例如“testttttttt”,插件将不会检测到它。我怎样才能让它检测到这个词,只要它就在那里? 这是我目前的代码:
public class EventsClass implements Listener {
AntiSwear plugin = AntiSwear.getPlugin(AntiSwear.class);
// Message when player joins the server
@EventHandler
public void onPlayerJoinEvent(PlayerJoinEvent event) {
Player player = event.getPlayer();
player.sendMessage(ChatColor.GOLD + "This server is running AntiSwear v1.0");
// The punishment system
}
@EventHandler
public void chatevent(AsyncPlayerChatEvent event) {
for (String s : event.getMessage().split(" ")) {
if (plugin.getConfig().getStringList("swears").contains(s)) {
event.setCancelled(true);
event.getPlayer().sendMessage(ChatColor.RED + "§lProfanity is not allowed on this server!");
// The punishment itself
Bukkit.getScheduler().runTask(plugin, () -> {
event.getPlayer().damage(10);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "smite " + event.getPlayer().getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "eco take " + event.getPlayer().getName() + " 100");
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "burn " + event.getPlayer().getName() + " 5");
plugin.getServer().broadcastMessage(ChatColor.GOLD + "§lPlayer " + event.getPlayer().getName() + " §lused a swear word and has been punished!");
});
}
}
}
}
你试过这个吗?既然您希望命中任何匹配项,而不是拆分和检查列表中的 contains,为什么不对所有“发誓”单词的字符串执行 contains。所以基本上是从你正在做的事情中进行反向搜索。我假设您仅将 String 用于搜索而不用它做任何其他事情。
您需要遍历列表并为每个项目执行 contains
检查,而不是检查完全匹配的列表:
List<String> swears = plugin.getConfig().getStringList("swears")
.stream()
.map(swear -> swear.toLowerCase() )
.collect(Collectors.toList());
for(String s : event.getMessage().split(" ")){
if(swears.stream().anyMatch(swear -> s.toLowerCase().contains(swear)){
event.setCancelled(true);
//...
}
}