Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AsyncChatEvent in Paper server #5908

Merged
merged 10 commits into from
Feb 22, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void updateListener() {
try {
Class.forName("io.papermc.paper.event.player.AsyncChatEvent");
chatListener = new PaperChatListener(this);
paperChatListener = new PaperAsyncChatListenerProvider();
paperChatListener = new PaperAsyncChatListenerProvider(plugin);
} catch (ClassNotFoundException ignored) {
chatListener = new BukkitChatListener(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.stream.Collectors;

public class PaperAsyncChatListenerProvider implements Listener {
private final ComponentSerializer<Component, TextComponent, String> serializer;
private final JavaPlugin plugin;

public PaperAsyncChatListenerProvider() {
public PaperAsyncChatListenerProvider(JavaPlugin plugin) {
this.plugin = plugin;
ComponentSerializer<Component, TextComponent, String> yeOldSerializer;
try {
// This method is only available in Paper 1.18.1+ and replaces the old deprecated method below.
Expand All @@ -32,12 +35,17 @@ public PaperAsyncChatListenerProvider() {

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onAsyncChatEvent(final AsyncChatEvent event) {
Bukkit.getPluginManager().callEvent(new AbstractAsyncChatEvent(
event.getPlayer(),
serializer.serialize(event.message()),
event.viewers().stream()
.filter(v -> v instanceof Player)
.map(v -> (Player) v)
.collect(Collectors.toSet())));
Bukkit.getScheduler().runTaskAsynchronously(plugin, () ->
Bukkit.getPluginManager().callEvent(
new AbstractAsyncChatEvent(
event.getPlayer(),
serializer.serialize(event.message()),
event.viewers().stream()
.filter(v -> v instanceof Player)
.map(v -> (Player) v)
.collect(Collectors.toSet())
)
)
);
}
}