Home > Software engineering >  How to check if string list contains an exact string?
How to check if string list contains an exact string?

Time:11-05

What I'm trying to do is make an Anti-swear plugin for my server. 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. 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!");
                    });
                }
            }
        }
    }

CodePudding user response:

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:

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);
    //...
  }
}

CodePudding user response:

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.

  • Related