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
This commit is contained in:
Adrian authored and GitHub committed 2023-07-04 00:24:19 -07:00
1 parent 307395ce5b
commit 999e2f9dee
5 files changed
+136 -22

No files matched your search

@@ -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<CarbonPlayerVelocity> {
private static final Set<Class<?>> LISTENER_CLASSES = Set.of(
private static final Set<Class<? extends VelocityListener<?>>> 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<CarbonPlayerVelocity>
this.init();
this.packetService();
for (final Class<?> clazz : LISTENER_CLASSES) {
this.proxyServer.getEventManager().register(carbonVelocityBootstrap, this.injector().getInstance(clazz));
for (final Class<? extends VelocityListener<?>> clazz : LISTENER_CLASSES) {
this.injector().getInstance(clazz).register(this.proxyServer.getEventManager(), carbonVelocityBootstrap);
}
}
@@ -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<PlayerChatEvent> {
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<Boolean> 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("""
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<E> extends AwaitingEventExecutor<E> {
void register(EventManager eventManager, CarbonVelocityBootstrap bootstrap);
}
@@ -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<LoginEvent> {
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))
);
}
}
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<DisconnectEvent> {
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()));
});
}
}