Is there an equivalent of async player chat event?


(((20 chars )))

MessageChannelEvent.Chat, although I am not sure if it is async or not

Chat can safely be handled asynchronously, so it is possible to just run all of your chat code in a separate thread.

While that was true in Bukkit, Sponge’s MessageChatEvent is fired synchronously. tl;dr: Treat it like you would any other event, and only use it/other API methods from the main thread.

Going into some more detail:

In Bukkit/Spigot, AsyncPlayerChatEvent is fired off the main thread when a player chats. Since chatting doesn’t really involve interaction with the world or server, this can be done (fairly) safely, unlike other events.

However, firing chat events asynchronously has some important limiations. Plugins doing more than some limited processing of the event will usually need to interact with things that aren’t thread safe (the permissions API, information about other players, etc). For this and other reasons, Sponge currently fires MessageChatEvent on the main thread, like all other events.

While you can certainly run code asynchronously by starting a Task from your event handler, this is of limited value for hat events. Critically, you cannot modify the event in any way from your asynchronous Task (e.g. setting the final message or cancelling your event), because the event may finish being fired either before or during your Task's execution.

There are some reasons you might want to do that, however. For example, you could store chat messages into a database or other storage mechanism from an Order.POST listener (though starting a new task for each message would be inefficient).

1 Like