Basic Chat Muting

Discussion in 'Wiki Discussion' started by jflory7, May 27, 2015.

  1. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    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(true);
        }
    }
    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 (Java):
        @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?
     
    #1 jflory7, May 27, 2015
    Last edited by a moderator: Jun 11, 2015
    • Like Like x 1
  2. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Hey @Justin393, this is definitely a great article and has potential to be useful – however, to me, it seems a bit more of a "giveaway" of code rather than an explanation or breakdown of what you're doing as you're writing. I know you have lots of comments in your code, but it would be easy for a user to just copy all the code without reading the comments at all. I think the purpose should be to break it down into bits and pieces with paragraph explanations to help users understand a bit more about what they're doing.

    For some good examples, see Example #1 and Example #2. I think it would be great if you rewrote it a bit to be in a similar format as some of those other articles. :)
     
    • Agree Agree x 2
  3. Alright, thanks for the feedback, I'll work on it as soon as I get a chance, finals and all v.v
     
  4. Tux

    Tux

    I've rewritten the article. Hope it's better now.

    By the way, long time no see! I've been quite busy.
     
    • Like Like x 1
  5. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Wooo, awesome, looks great, Tux! And welcome back. :)
     
  6. Here's how I would write it, bit more concise.

    Code (Java):

    @Override
    publicboolean onCommand(CommandSender sender, Command cmd, [URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string']String[/URL] label, [URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+string']String[/URL][] args){
         // Check what command was just typed
         if(cmd.getName().equalsIgnoreCase("mutechat")){
             // If the chat isn't muted then mute it

             String enabled;
             if (chatEnabled)
                 enabled = "Muted";
             else
                 enabled = "Unmuted";
           
             chatEnabled = !chatEnabled;
             sender.sendMessage(ChatColor.GREEN + enabled + " the chat");
             return true;
         }
         // If something went wrong display the usage in plugin.yml
         return false;
    }
     
    The "enabled = !chatEnabled" part is a technique I don't see anyone using, but I think it would definitely be good for people to realise you can do this, saves a lot of typing :).
     
  7. A very nice idea, I hope the private chat rooms are built into the project.

    Best regards from Germany
     
  8. This is good, but I can't really understand what is really happening. Can you explain a bit more as you go along?

    Thanks