简体   繁体   English

如何检查字符串列表是否包含确切的字符串?

[英]How to check if string list contains an exact string?

What I'm trying to do is make an Anti-swear plugin for my server.我想要做的是为我的服务器制作一个 Anti-swear 插件。 It works fine, but players can bypass it easly.它工作正常,但玩家可以轻松绕过它。 Let's say that in my config i have the word "test" listed.假设在我的配置中,我列出了“测试”这个词。 If the player types the word "test" in the chat normally, the plugin will detect it.如果玩家在聊天中正常输入“测试”这个词,插件会检测到它。 But if the player types in, for example "testtttttttt" the the plugin will not detect it.但是如果玩家输入,例如“testtttttttt”,插件将检测不到它。 How can I make it so that it detects the word as long as its just there?我怎样才能让它检测到这个词,只要它就在那里? Here is the code I have currently:这是我目前拥有的代码:

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!");
                    });
                }
            }
        }
    }

Have you tried this?你试过这个吗? Instead of splitting and checking a contains on the list, since you want any matches to hit, why not do a contains on the string for all of the "swear" words.与其拆分和检查列表上的包含,因为您希望匹配任何匹配项,为什么不在字符串上为所有“发誓”字词执行包含。 So basically an inverse search from what you're doing.所以基本上是你正在做的反向搜索。 I assume you use the String s only for the search and do nothing else with it.我假设您仅将 String 用于搜索,并且不使用它做其他任何事情。

Instead of a contains check on the list which checks for exact matches, you'll need to loop over the list and for each item do a contains check:取而代之的是contains在名单上检查其精确匹配检查,你需要遍历列表,并为每个项目做一个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);
    //...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM