How to detect and change error chat messages

Discussion in 'Spigot Plugin Development' started by FrostyTigerXP, May 5, 2017.

  1. TL;DR: I'm trying to make a plugin that replaces no-permission messages from the server and plugins to something completely custom. I have made some progress but I'm still stuck.

    The problem I am trying to solve is that vanilla Minecraft, Bukkit, and all the plugins on my server all have completely different no-permission messages; I'm trying to find/create a plugin that can intercept messages from the plugins to the client and replace it with my own message. I would like this to affect every plugin on the server as well as the default Minecraft and Bukkit messages. So all messages that result from a player not having sufficient permissions will return the same error message instead of several other ones.

    I think it might be challenging because of the fact that many plugins use APIs/Libraries to manage player interaction, but I think it might still be possible.

    This is the code I wrote so far (it's not very good):
    Code (Text):
    package com.luckycloudmc.frostytigerxp;

    import org.bukkit.command.*;
    import org.bukkit.plugin.java.*;

    public class NoPermMessage extends JavaPlugin {

        public void onEnable(){}
        public void onDisable(){}
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            System.out.println("Working");
            if (!sender.hasPermission(command.getPermission())){
                command.setPermissionMessage("No Permission");
                return true;
            }
            return false;
        }
    }
    I wasn't sure whether to use the onCommand() method or my own method with the
    PlayerCommandPreprocessEvent event. I used onCommand() because I could check if the player had the permission or not. (I have read a lot of documentation but it still doesn't work)

    P.S. I have around 2 years of Java experience but I have very little experience with Bukkit plugins and the Bukkit API, so please let me know if I make any silly errors.
     
  2. sothatsit

    Patron

    You would need to listen to the event PlayerCommandPreProcessEvent as onCommand is only called for the commands registered with your own plugin.

    What you can then do is decode the message from the event to get the command label. Something like:
    Code (Text):
    String message = event.getMessage();
    String commandLabel = message.split(" ")[0].substring(1);
    You can then use Bukkit.getPluginCommand to get a command from this label, returning null if a command is not found.
    Code (Text):
    PluginCommand command = Bukkit.getPluginCommand(commandLabel);

    if(command == null)
       return;
    This may only work for commands added by plugins. You will have to do more digging to see if you can find a way to get the information you need for the default commands if they do not work.

    Then you can do your check with the permission from command.

    The issue though, is that often plugins don't put the permission for the command in their plugin.yml or when they're registering their commands. This means that often the permission from the command will be empty because the plugin implements the permissions itself instead.

    For plugins like this, of which there are many, you will have to take another approach. One such approach is listening to all messages sent to players using a packet listener framework such as ProtocolLib, and filtering and changing messages that you know to be permissions messages.

    If the plugins do let you change their permissions messages in a language file, that would be best, although many plugins do not have this functionality either.

    Best of luck,
    ~Sothatsit.
     
  3. Thanks dude (sorry for the late reply!), I'm probably going to use ProtocolLib for this.