Basic Chat Muting

Feb 9, 2017
Basic Chat Muting
  • Basic Chat Muting

    How to work with chat events



    In this article, we will create a simple plugin to mute the chat on your server. This will also include some exercises you can do on your own to improve the plugin.


    The Basics(top)

    A very basic way to get started is to create a small example on how to cancel an event. This will be useful later on, so let's take a look at how we cancel the chat event.

    In this case, we are using the AsyncPlayerChatEvent, and we would like to cancel all events. Effectively, this removes chat functionality from the server. Let's take a look at it.

    Code (Java):
    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        event.setCancelled(true);
    }
    We will not discuss how to register this listener, but if you installed this plugin, chat would be disabled.

    Adding a Toggle(top)

    It does seem limiting to add this plugin each time you need to silence the chat, right? Let's add in a command to toggle it! First, let's revise our listener:

    Code (Java):
    private volatile boolean chatEnabled = true;

    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent event) {
        if (!chatEnabled) {
            event.setCancelled(false);
        }
    }
    We've now changed the check and added in a new field. Note that the volatile is required, as the chat event is typically run asynchronously (not on the server thread).

    Now we can add in our command handler (adding the command to your plugin.yml is outside the scope of this snippet). Let's assume your listener and plugin are the same class:

    Code (Text):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            // Check what command was just typed
            if (cmd.getName().equalsIgnoreCase("mutechat")) {
                // Invert the chatEnabled boolean to the opposite
                chatEnabled = !chatEnabled;
                // See if the chatEnabled boolean is true if it is print the string 'Unmuted the chat' if not then 'Muted the chat'
                sender.sendMessage(ChatColor.GREEN + (chatEnabled ? "Unmuted the chat" : "Muted the chat"));
            }
            // Return true always since there is no actual usage other than the name of the command.
            return true;
        }
    Once this plugin is run, you can easily use /mutechat to mute chat, and again to unmute it. It's that simple!

    Exercises(top)

    • How could you better improve the organization of this plugin?
    • How could you make all chat messages sent be fully handled but only seen by the person sending them?
    • Notice that we did not include any permission checks. How might you include a permission check for the /mutechat command?
    • If you use /mutechat, even as an administrator, your chat will not go through. How might you add in an override for administrators?
  • Loading...
  • Loading...