Using Paper's AsyncChatEvent to change the format of a chat message

Hey all, I’m relatively new to the Paper API and trying to get used to using Components instead of Strings.

I’m coding a minigame plugin that changes the format of chat messages to the following:

[Game/Spectator/Lobby Chat] [Prefix] Player Name » Message

In Minecraft, that would look something like this:

image

How would I do that with Paper API’s ChatRenderer and AsyncChatEvent? I see from the Javadocs that there is a #render method, but that doesn’t allow me to insert the chat prefix.

The prefix also changes depending on the player speaking, so spectators will have the [Spectator Chat] prefix, players in the lobby will have the [Lobby Chat] prefix, and so on.

Any ideas? Thanks!

bump

ChatRender is an interface which you implement, the component returned from that interface (when called by the server itself, you just implement it and set it on the event), is what is displayed in chat

I got it working, thank you!

Side note, is there a way to retrieve only the String portion of a Component? I don’t see a clear method such as getString() from the Javadocs. Would toString() do exactly that?

i believe you’re looking for the PlainComponentSerializer