Triggering a player chat event

Hey guys, so I’m working on a new project that will allow multiple servers to share chats.
I need a way to trigger a a what I believe is a MessageChannelEvent.Chat Event.
I’ve been looking around but I don’t see a clear way of doing it. The sponge docs do show how to make your own custom events, but That’s not what I want. Unless if I were to extend a Chat Event as a “CustomChatEvent” it would still fire for things that look for the Chat event.

I’m not sure what the best way to go about this is,
I figure I should use Sponge.getEventManager().post() to put in a new event but when Trying to do so I’d have to override a whole bunch of methods which don’t make sense to override in this context. Am I missing something here.

public void onReceiveProtoBuff(){
        Text message = new Text;

        MessageChannelEvent.Chat chatEvent = new MessageChannelEvent.Chat() {
            @Override
            public Text getRawMessage() {
                return message;
            }

            @Override
            public boolean isCancelled() {
                return false;
            }

            @Override
            public void setCancelled(boolean cancel) {

            }

            @Override
            public MessageChannel getOriginalChannel() {
                return MessageChannel.TO_ALL;
            }

            @Override
            public Optional<MessageChannel> getChannel() {
                return Optional.of(MessageChannel.TO_ALL);
            }

            @Override
            public void setChannel(@Nullable MessageChannel channel) {
            }

            @Override
            public Text getOriginalMessage() {
                return null;
            }

            @Override
            public boolean isMessageCancelled() {
                return false;
            }

            @Override
            public void setMessageCancelled(boolean cancelled) {

            }

            @Override
            public MessageFormatter getFormatter() {
                return null;
            }

            @Override
            public Cause getCause() {
                return null;
            }
        };

        Sponge.getEventManager().post(chatEvent);
    }
1 Like

What you’re looking for is to use the SpongeEventFactory, this contains static methods which allow you to create any event in sponge to be fired by a plugin. So in your case you want something like this

public void doThing() {
    MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(paramaters);
    Sponge.getEventManager().post(event);
}
1 Like

Thanks again for your help friend. I discovered that this might also work, with a lot less work involved.
player.simulateChat(message,Cause.of(NamedCause.simulated(player)));