From 999e2f9deeabe1155abd8482b4aaac062a9f259e Mon Sep 17 00:00:00 2001 From: Adrian <68704415+4drian3d@users.noreply.github.com> Date: Tue, 4 Jul 2023 02:24:19 -0500 Subject: [PATCH] Improved Velocity listeners registration and execution performance (#257) * perf(velocity): Improved listener registration and execution performance Also, changed Velocity ChatListener PostOrder to LATE * Reverted ChatListener PostOrder change * Applied suggested changes * Fixed checkstyle --- .../carbon/velocity/CarbonChatVelocity.java | 11 ++-- .../listeners/VelocityChatListener.java | 30 +++++++-- .../velocity/listeners/VelocityListener.java | 29 +++++++++ .../listeners/VelocityPlayerJoinListener.java | 23 +++---- .../VelocityPlayerLeaveListener.java | 65 +++++++++++++++++++ 5 files changed, 136 insertions(+), 22 deletions(-) create mode 100644 velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityListener.java create mode 100644 velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerLeaveListener.java diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java b/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java index 3e3764be..52075d61 100644 --- a/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java +++ b/velocity/src/main/java/net/draycia/carbon/velocity/CarbonChatVelocity.java @@ -40,7 +40,9 @@ import net.draycia.carbon.common.messaging.MessagingManager; import net.draycia.carbon.common.users.ProfileCache; import net.draycia.carbon.common.users.ProfileResolver; import net.draycia.carbon.velocity.listeners.VelocityChatListener; +import net.draycia.carbon.velocity.listeners.VelocityListener; import net.draycia.carbon.velocity.listeners.VelocityPlayerJoinListener; +import net.draycia.carbon.velocity.listeners.VelocityPlayerLeaveListener; import net.draycia.carbon.velocity.users.CarbonPlayerVelocity; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; @@ -53,9 +55,10 @@ import org.checkerframework.framework.qual.DefaultQualifier; @Singleton public class CarbonChatVelocity extends CarbonChatInternal { - private static final Set> LISTENER_CLASSES = Set.of( + private static final Set>> LISTENER_CLASSES = Set.of( VelocityChatListener.class, - VelocityPlayerJoinListener.class + VelocityPlayerJoinListener.class, + VelocityPlayerLeaveListener.class ); private final ProxyServer proxyServer; @@ -102,8 +105,8 @@ public class CarbonChatVelocity extends CarbonChatInternal this.init(); this.packetService(); - for (final Class clazz : LISTENER_CLASSES) { - this.proxyServer.getEventManager().register(carbonVelocityBootstrap, this.injector().getInstance(clazz)); + for (final Class> clazz : LISTENER_CLASSES) { + this.injector().getInstance(clazz).register(this.proxyServer.getEventManager(), carbonVelocityBootstrap); } } diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityChatListener.java b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityChatListener.java index af8ce830..6010c4da 100644 --- a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityChatListener.java +++ b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityChatListener.java @@ -19,9 +19,11 @@ */ package net.draycia.carbon.velocity.listeners; +import com.google.common.base.Suppliers; import com.google.inject.Inject; +import com.velocitypowered.api.event.EventManager; +import com.velocitypowered.api.event.EventTask; import com.velocitypowered.api.event.PostOrder; -import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.api.plugin.PluginManager; @@ -29,6 +31,7 @@ import com.velocitypowered.api.proxy.Player; import java.util.ArrayList; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.event.events.CarbonChatEvent; @@ -38,6 +41,7 @@ import net.draycia.carbon.api.util.KeyedRenderer; import net.draycia.carbon.common.config.ConfigFactory; import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.velocity.CarbonChatVelocity; +import net.draycia.carbon.velocity.CarbonVelocityBootstrap; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; @@ -50,15 +54,15 @@ import static net.kyori.adventure.key.Key.key; import static net.kyori.adventure.text.Component.text; @DefaultQualifier(NonNull.class) -public final class VelocityChatListener { +public final class VelocityChatListener implements VelocityListener { private final CarbonChatVelocity carbonChat; private final ChannelRegistry registry; private final UserManager userManager; private final Logger logger; private final AtomicInteger timesWarned = new AtomicInteger(0); - private final PluginManager pluginManager; private final CarbonMessages carbonMessages; + private final Supplier signedSupplier; final ConfigFactory configFactory; @Inject @@ -75,13 +79,25 @@ public final class VelocityChatListener { this.registry = registry; this.userManager = userManager; this.logger = logger; - this.pluginManager = pluginManager; this.carbonMessages = carbonMessages; this.configFactory = configFactory; + this.signedSupplier = Suppliers.memoize( + () -> pluginManager.isLoaded("unsignedvelocity") + || pluginManager.isLoaded("signedvelocity") + ); } - @Subscribe(order = PostOrder.LATE) - public void onPlayerChat(final PlayerChatEvent event) { + @Override + public void register(final EventManager eventManager, final CarbonVelocityBootstrap bootstrap) { + eventManager.register(bootstrap, PlayerChatEvent.class, PostOrder.LAST, this); + } + + @Override + public EventTask executeAsync(final PlayerChatEvent event) { + return EventTask.async(() -> this.executeEvent(event)); + } + + private void executeEvent(final PlayerChatEvent event) { if (!event.getResult().isAllowed()) { return; } @@ -89,7 +105,7 @@ public final class VelocityChatListener { final Player player = event.getPlayer(); final boolean signedVersion = player.getIdentifiedKey() != null && player.getProtocolVersion().compareTo(ProtocolVersion.MINECRAFT_1_19_1) >= 0; - if (signedVersion && !this.pluginManager.isLoaded("unsignedvelocity")) { + if (signedVersion && !this.signedSupplier.get()) { if (this.timesWarned.getAndIncrement() < 3) { this.logger.warn(""" diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityListener.java b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityListener.java new file mode 100644 index 00000000..0a7d448e --- /dev/null +++ b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityListener.java @@ -0,0 +1,29 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.velocity.listeners; + +import com.velocitypowered.api.event.AwaitingEventExecutor; +import com.velocitypowered.api.event.EventManager; +import net.draycia.carbon.velocity.CarbonVelocityBootstrap; + +public interface VelocityListener extends AwaitingEventExecutor { + + void register(EventManager eventManager, CarbonVelocityBootstrap bootstrap); +} diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerJoinListener.java b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerJoinListener.java index 03c0e02c..fa13c7ea 100644 --- a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerJoinListener.java +++ b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerJoinListener.java @@ -20,19 +20,19 @@ package net.draycia.carbon.velocity.listeners; import com.google.inject.Inject; -import com.velocitypowered.api.event.Subscribe; -import com.velocitypowered.api.event.connection.DisconnectEvent; +import com.velocitypowered.api.event.EventManager; +import com.velocitypowered.api.event.EventTask; import com.velocitypowered.api.event.connection.LoginEvent; +import net.draycia.carbon.velocity.CarbonVelocityBootstrap; import net.draycia.carbon.velocity.VelocityUserManager; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.framework.qual.DefaultQualifier; import static net.draycia.carbon.common.util.PlayerUtils.joinExceptionHandler; -import static net.draycia.carbon.common.util.PlayerUtils.saveExceptionHandler; @DefaultQualifier(NonNull.class) -public class VelocityPlayerJoinListener { +public class VelocityPlayerJoinListener implements VelocityListener { private final VelocityUserManager userManager; private final Logger logger; @@ -46,15 +46,16 @@ public class VelocityPlayerJoinListener { this.logger = logger; } - @Subscribe - public void onPlayerJoin(final LoginEvent event) { - this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger)); + @Override + public void register(final EventManager eventManager, final CarbonVelocityBootstrap bootstrap) { + eventManager.register(bootstrap, LoginEvent.class, this); } - @Subscribe - public void onPlayerLeave(final DisconnectEvent event) { - this.userManager.loggedOut(event.getPlayer().getUniqueId()) - .exceptionally(saveExceptionHandler(this.logger, event.getPlayer().getUsername(), event.getPlayer().getUniqueId())); + @Override + public EventTask executeAsync(final LoginEvent event) { + return EventTask.async( + () -> this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger)) + ); } } diff --git a/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerLeaveListener.java b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerLeaveListener.java new file mode 100644 index 00000000..af5f0a42 --- /dev/null +++ b/velocity/src/main/java/net/draycia/carbon/velocity/listeners/VelocityPlayerLeaveListener.java @@ -0,0 +1,65 @@ +/* + * CarbonChat + * + * Copyright (c) 2023 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.velocity.listeners; + +import com.google.inject.Inject; +import com.velocitypowered.api.event.EventManager; +import com.velocitypowered.api.event.EventTask; +import com.velocitypowered.api.event.connection.DisconnectEvent; +import net.draycia.carbon.velocity.CarbonVelocityBootstrap; +import net.draycia.carbon.velocity.VelocityUserManager; +import org.apache.logging.log4j.Logger; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +import static net.draycia.carbon.common.util.PlayerUtils.saveExceptionHandler; + +@DefaultQualifier(NonNull.class) +public final class VelocityPlayerLeaveListener implements VelocityListener { + + private final VelocityUserManager userManager; + private final Logger logger; + + @Inject + public VelocityPlayerLeaveListener( + final VelocityUserManager userManager, + final Logger logger + ) { + this.userManager = userManager; + this.logger = logger; + } + + @Override + public void register(final EventManager eventManager, final CarbonVelocityBootstrap bootstrap) { + eventManager.register(bootstrap, DisconnectEvent.class, this); + } + + @Override + public EventTask executeAsync(final DisconnectEvent event) { + return EventTask.async(() -> { + if (event.getLoginStatus() == DisconnectEvent.LoginStatus.CONFLICTING_LOGIN) { + return; + } + this.userManager.loggedOut(event.getPlayer().getUniqueId()) + .exceptionally(saveExceptionHandler(this.logger, event.getPlayer().getUsername(), event.getPlayer().getUniqueId())); + }); + } + +}