Intercepting messages from other plugins

Hello! I’m working on a plugin that does things when certain messages in chat appear. For example when the server says something I relay it to discord. This all works fine. But now I want to do the same when certain messages from other plugins come in. One plugin for example is using the MessageChannel.TO_ALL.send() method to send the message.My current function looks like:

@Listener(order = Order.EARLY)
public void onChat(MessageChannelEvent event) {
    String chatMessage = event.getMessage().toPlain();
    logger.info(chatMessage);
    if (event.getChannel().isPresent()) {
        MessageChannel channel = event.getChannel().get();
        logger.info(channel.getClass().getName().toString());
        if (channel.getClass().getName().startsWith("org.spongepowered.api.text.channel.MessageChannel")) {

            Boolean foundLiteral = checkForLiteral(chatMessage);
            Boolean foundContains = checkForContains(chatMessage);
            Boolean foundStartsWith = checkForStartsWith(chatMessage);
            Boolean foundEndsWith = checkForEndsWith(chatMessage);

            if (foundLiteral || foundContains || foundStartsWith || foundEndsWith) {
                event.setMessageCancelled(true);
                logger.info("Attempting to relay server message to Discord");
                chatMessage.replaceAll("\\*\\*", "");
                sendMessage(Text.of(TextColors.LIGHT_PURPLE, chatMessage));
                sendDiscordMessage(chatMessage);
            }
        }
    }
}

But I noticed that messages from plugins are not coming in. What is the correct way of catching those?

Move your hardcoded class name to a list in a config file.

What do you mean? It’s never even gets in the event when a plugin sends a to_all. That’s why I have the logger on line 2 in the function.

As far as I know, if a plugin calls player.sendMessage(...), the server either fires no event at all or just a MessageEvent. You can try to listen to MessageEvent and cast it for the channel if you want, but if that doesn’t work your only choice will be to create a Mixin that modifies the player’s sendMessage method to support what you want.

the player.sendMessage(...) and server chat is fine with my function. The event is fired and I check the channel and some other stuff and then I do stuff with it. But for example Nations plugin uses MessageChannel.TO_ALL.send(Text.of(TextColors.AQUA, LanguageHandler.CL));. Wich doesnt fire MessageChannelEvent or MessageChannel. I also made the following test function:

@Listener(order = Order.EARLY)
public void onMessage(MessageChannel event) {
  logger.info("Something happened here");
}

That one doesnt get fired either. I suspect the MessageChannel.TO_ALL.send(...) is a broadcast event and that it is handled in a different event.

Any other thoughts on this matter? Or is this a bug and should it be reported?

If you’ve fully tested it and it doesn’t happen, then yeah, you should file an issue.