@@ -5,21 +5,21 @@ slug: paper/dev/chat-events
55---
66
77The chat event has evolved a few times over the years.
8- This guide will explain how to properly use the new [ ` AsyncChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
9- and its [ ` ChatRenderer ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) .
10- The [ ` AsyncChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
11- is an improved version of the old [ ` AsyncPlayerChatEvent ` ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent )
8+ This guide will explain how to properly use the new [ ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
9+ and its [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) .
10+ The [ ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
11+ is an improved version of the old [ ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent )
1212that allows you to render chat messages individually for each player.
1313
1414::: note [ ` AsyncChatEvent ` vs ` ChatEvent ` ]
1515
16- The key difference between [ ` AsyncChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
17- and [ ` ChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.ChatEvent ) is that
18- [ ` AsyncChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent ) is fired asynchronously.
16+ The key difference between [ ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent )
17+ and [ ] ( jd:paper:io.papermc.paper.event.player.ChatEvent ) is that
18+ [ ] ( jd:paper:io.papermc.paper.event.player.AsyncChatEvent ) is fired asynchronously.
1919
2020This means that it does not block the main thread and sends the chat message when the listener has completed.
2121Be aware that using the Bukkit API in an asynchronous context (i.e. the event handler) is unsafe and exceptions may be thrown.
22- If you need to use the Bukkit API, you can use [ ` ChatEvent ` ] ( jd:paper:io.papermc.paper.event.player.ChatEvent ) .
22+ If you need to use the Bukkit API, you can use [ ] ( jd:paper:io.papermc.paper.event.player.ChatEvent ) .
2323However, we recommend using [ ` BukkitScheduler ` ] ( /paper/dev/scheduler ) .
2424
2525:::
@@ -28,10 +28,10 @@ However, we recommend using [`BukkitScheduler`](/paper/dev/scheduler).
2828
2929Before we can start using the new chat event, we need to understand how the new renderer works.
3030The renderer is Paper's way of allowing plugins to modify the chat message before it is sent to the player.
31- This is done by using the [ ` ChatRenderer ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface with its
32- [ ` render ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer#render(org.bukkit.entity.Player,net.kyori.adventure.text.Component,net.kyori.adventure.text.Component,net.kyori.adventure.audience.Audience) )
33- method. Previously, this was done by using the [ ` AsyncPlayerChatEvent ` ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent )
34- with its [ ` setFormat ` ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent#setFormat(java.lang.String) ) method.
31+ This is done by using the [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface with its
32+ [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer#render(org.bukkit.entity.Player,net.kyori.adventure.text.Component,net.kyori.adventure.text.Component,net.kyori.adventure.audience.Audience) )
33+ method. Previously, this was done by using the [ ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent )
34+ with its [ ] ( jd:paper:org.bukkit.event.player.AsyncPlayerChatEvent#setFormat(java.lang.String) ) method.
3535
3636``` java title="ChatRenderer#render"
3737public Component render(Player source, Component sourceDisplayName, Component message, Audience viewer) {
@@ -48,27 +48,27 @@ public Component render(Player source, Component sourceDisplayName, Component me
4848::: tip [ ` ChatRenderer.ViewerUnaware ` ]
4949
5050If your renderer does not need to know about the viewer, you can use the
51- [ ` ChatRenderer.ViewerUnaware ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer$ViewerUnaware )
52- interface instead of the [ ` ChatRenderer ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface.
51+ [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer$ViewerUnaware )
52+ interface instead of the [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface.
5353This will benefit performance as the message will only be rendered once instead of each individual player.
5454
5555:::
5656
5757## Using the renderer
5858
5959There are two ways to use the renderer.
60- 1 . Implementing the [ ` ChatRenderer ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface in a class.
60+ 1 . Implementing the [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer ) interface in a class.
61612 . Using a lambda expression.
6262
6363Depending on the complexity of your renderer, you may want to use one or the other.
6464
6565### Implementing the ` ChatRenderer ` interface
6666
67- The first way of using the renderer is by implementing the [ ` ChatRenderer ` ] ( jd:paper:io.papermc.paper.chat.ChatRenderer )
67+ The first way of using the renderer is by implementing the [ ] ( jd:paper:io.papermc.paper.chat.ChatRenderer )
6868interface in a class. In this example, we will be using our ` ChatListener ` class.
6969
7070Next, we need to tell the event to use the renderer by using the
71- [ ` renderer ` ] ( jd:paper:io.papermc.paper.event.player.AbstractChatEvent#renderer() ) method.
71+ [ ] ( jd:paper:io.papermc.paper.event.player.AbstractChatEvent#renderer() ) method.
7272
7373``` java title="ChatListener.java"
7474public class ChatListener implements Listener , ChatRenderer { // Implement the ChatRenderer and Listener interface
0 commit comments