How to catch sendMessage

Discussion in 'Plugin Development' started by Instead, Apr 2, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Instead

    Hello!

    Is there any way to catch bukkit's "sendMessage" and replace it with for example "mysendMessage"?

    I mean almost all plugins use "sendMessage" to display something to player.
    On each call I need to send all requests not to "sendMessage", but to my plugin's "mysendMessage".

    Sorry for my bad english. ;)

    UPD:

    I'm trying to achieve this:
    I wrote a function that converts each string passing through the sendMessage, and introduced it into CraftBukkit.
    That is, it looks like this:


    Code:
        public void sendMessage (String message) {
            for (final String line: TextWrapper.wrapText (EncodeString (message))) {
                getHandle (). a.b (new Packet3Chat (line));
            }
        }
    EncodeString described below in this class.

    So, the problem is that craftbukkit often updated and I have each time to modify it, compile, etc.
    The aim is to implement it through a plugin, it was not necessary every time to modify another CraftBukkit build.
     
  2. Offline

    Instead

    bump... :(
     
  3. Offline

    willurd

    Well, sendMessage simply sends raw packets to the client:

    Code:
    public void sendMessage(String message) {
        for (final String line: TextWrapper.wrapText(message)) {
            getHandle().a.b(new Packet3Chat(line));
        }
    }
    It doesn't dispatch an event or anything you can cancel/override. So, I'm going to say no, that's not possible. If you let us know what you're trying to accomplish though, somebody might know of another way of doing it.
     
  4. Offline

    Instead

    I'm trying to achieve this:
    I wrote a function that converts each string passing through the sendMessage, and introduced it into CraftBukkit.
    That is, it looks like this:


    Code:
         public void sendMessage (String message) {
             for (final String line: TextWrapper.wrapText (EncodeString (message))) {
                 getHandle (). a.b (new Packet3Chat (line));
             }
         }
    EncodeString described below in this class.

    So, the problem is that craftbukkit often updated and I have each time to modify it, compile, etc.

    The aim is to implement it through a plugin, it was not necessary every time to modify another CraftBukkit build.
     
  5. Offline

    willurd

    Can you tell us what you're trying to accomplish? Are you trying to add your own text to chat messages or do something else with chat? If so, that IS possible with the player chat event.
     
  6. Offline

    Instead

    I'm encoding each string (searching unsupported cyrillic symbols and changing them, to get correct output ingame)
     
  7. Offline

    willurd

    Ah. I think you can do that with something like this.

    Create a PlayerListener subclass, and override its onPlayerChat method. Inside that function, you can modify the chat message as you see fit:

    Code:
    import org.bukkit.event.player.PlayerListener;
    
    public class YourPlayerListener extends PlayerListener {
        @Override
        public void onPlayerChat (PlayerChatEvent event) {
            event.setMessage(yourEncodeFunction(event.getMessage()));
        }
    }
    
    And then you can register that listener in your plugin:

    Code:
    public class YourPlugin extends JavaPlugin {
        PluginManager pm;
        YourPlayerListener playerListener;
        @Override
        public void onEnable () {
            pm = getServer().getPluginManager();
            playerListener = new YourPlayerListener(this);
            pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Priority.HIGHEST, this);
        }
    }
    
    I set the priority to HIGHEST as it sounds like this should run after all other listeners, in case they too add some text to the message that needs encoding. The docs for Priority.HIGHEST say:

    Code:
    Event call is critical and must have the final say in what happens to the event
     
  8. Offline

    Instead

    Thank u very much, but...
    The main aim is: situations, when different plugins call sendMessage to show player some info. And I need to translate them (I mean this info must be encoded too).

    Is it possible to listen plugin's calls?
     
  9. Offline

    hmpf

    I have the same problem at the moment.

    The PLAYER_CHAT event only fires when a player sends a message.
    Typing say Test in the console wont fire the event.
    Calling player.sendMessage("Test"); from within a plugin wont fire the event either.

    Id like to be able to catch every chat output that's send to the game clients before it actually gets send.
     
  10. Offline

    Instead

    I looked at the source Essentials, there is an override of sendMessage:

    Code:
        public void sendMessage(String message) {
            for (final String line: TextWrapper.wrapText(StringToRus(message))) {
                getHandle().a.b(new Packet3Chat(line));
            }
        }
    But it seems that only the plugin can use this override.
    Apparently, the other plugins will still take sendMessage of Bukkit.
     
  11. Offline

    willurd

    Ah I see. Yea, that sounds like it'd be a client plugin ('process all text before it's displayed to the player'). As of now, Bukkit only supports server plugins.
     
  12. Offline

    hmpf

    As the chat output is generated on the server (by server plugins or the server console) it must be possible to process it before it gets send to the connected clients. However, as of now, it doesn't seem to be possible.
     
  13. Offline

    Galaran

    Same problem.
    I need to send different broadcast messages to players: encoded and original.
    With

    Code:
    if (...) {
        curPlayer.sendMessage('<' + event.getPlayer().getDisplayName() + "> " + encodedMessage);
    } else {
        curPlayer.sendMessage('<' + event.getPlayer().getDisplayName() + "> " + event.getMessage());
    }
    
    it works, but some plugins(e.g. Towny) catchs regular chat messages and reformats it to e.g. " <King Nickname> message" and this fails

    Need something like before sendMessage()
    Code:
    onChatPostProcess(Player dst, String message)
    
     
  14. Offline

    repeat

    do we have some solution this problem?
     
  15. Spout has a way to listen to packets being sent AFAIK. It should be very possible to listen to packets with the id 3 and do whatever you like with them.
     
  16. Offline

    Don Redhorse

    welll I guess you should make a pull request to bukkit to get this implemented in bukkit directly... that would be probably better if it is not too many code..
     
Thread Status:
Not open for further replies.

Share This Page