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

Improved Velocity listeners registration and execution performance #257

Merged
merged 6 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,7 +40,9 @@
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;
Expand All @@ -53,9 +55,10 @@
@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;
Expand Down Expand Up @@ -102,8 +105,8 @@ public void onInitialization(final CarbonVelocityBootstrap carbonVelocityBootstr
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);
}
}

Expand Down
Expand Up @@ -19,16 +19,19 @@
*/
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;
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;
Expand All @@ -38,6 +41,7 @@
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;
Expand All @@ -50,15 +54,15 @@
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
Expand All @@ -75,21 +79,33 @@ private 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;
}

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("""

Expand Down
@@ -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);
}
Expand Up @@ -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;
Expand All @@ -46,15 +46,16 @@ public 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()));
});
}

}