[HOW TO] Create an simple/powerful antiswear

Aug 18, 2017
[HOW TO] Create an simple/powerful antiswear
  • This wiki page was outdated and using some stupid API, so I've updated it.

    Feel free to add better code or whatever, but the code before this edit was terrible.
    Code (Java):
    package your.package.name;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class AntiSwear implements Listener {

        @EventHandler // Listening for the event.
        public void onChat(AsyncPlayerChatEvent event) {
            // When the event is called the code inside this will run.
            Player player = event.getPlayer(); // Getting the player who sent the message.
            String message = event.getMessage().toLowerCase(); // Getting the message, and making it lowercase.

            for(String blockedWord : getConfig().getStringList("Blocked-Words")) { // Looping through all the blocked words in the string list.
                if(message.contains(blockedWord.toLowerCase())) { // If the message contains one or more blocked word
                    player.sendMessage(ChatColor.RED + "Your message was cancelled because it had a blocked word in it!"); // Tell the player their message was blocked.
                    event.setCancelled(true); // Cancel the message from being sent
                    break; // No need to continue checking
                }
            }
        }

    }
    Code (KOTLIN):
    //Code in Kotlin cause why not ;^)

    package your.package.name

    import org.bukkit.ChatColor
    import org.bukkit.entity.Player
    import org.bukkit.event.EventHandler
    import org.bukkit.event.Listener
    import org.bukkit.event.player.AsyncPlayerChatEvent

    class AntiSwear : Listener {

        @EventHandler // Listening for the event.
        fun onChat(event: AsyncPlayerChatEvent) {
            // When the event is called the code inside this will run.
            val player = event.player // Getting the player who sent the message.
            val message = event.message.toLowerCase() // Getting the message, and making it lowercase.

            for (blockedWords in config.getStringList("Blocked-Words")) { // Looping through all the blocked words in the string list.
                if (message.contains(blockedWords)) { // If the message contains one or more blocked word
                    player.sendMessage(ChatColor.RED.toString() + "Your message was cancelled because it had a blocked word in it!") // Tell the player their message was blocked.
                    event.isCancelled = true // Cancel the message from being sent
                }
            }
        }

    }
  • Loading...
  • Loading...