From 4c3d9de5fe9e7a4fe262c8122dae4e2f43c34752 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 12 Jul 2020 16:13:04 -0400 Subject: [PATCH] Add ServerPostConnectEvent as a less confusing (and non-blocking) alternative to ServerConnectedEvent --- .../event/player/ServerPostConnectEvent.java | 39 +++++++++++++++++++ .../backend/TransitionSessionHandler.java | 3 ++ 2 files changed, 42 insertions(+) create mode 100644 api/src/main/java/com/velocitypowered/api/event/player/ServerPostConnectEvent.java diff --git a/api/src/main/java/com/velocitypowered/api/event/player/ServerPostConnectEvent.java b/api/src/main/java/com/velocitypowered/api/event/player/ServerPostConnectEvent.java new file mode 100644 index 000000000..e638df49e --- /dev/null +++ b/api/src/main/java/com/velocitypowered/api/event/player/ServerPostConnectEvent.java @@ -0,0 +1,39 @@ +package com.velocitypowered.api.event.player; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.server.RegisteredServer; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Fired after the player has connected to a server. The server the player is now connected to is + * available in {@link Player#getCurrentServer()}. + */ +@Beta +public class ServerPostConnectEvent { + private final Player player; + private final RegisteredServer previousServer; + + public ServerPostConnectEvent(Player player, + @Nullable RegisteredServer previousServer) { + this.player = Preconditions.checkNotNull(player, "player"); + this.previousServer = previousServer; + } + + public Player getPlayer() { + return player; + } + + public @Nullable RegisteredServer getPreviousServer() { + return previousServer; + } + + @Override + public String toString() { + return "ServerPostConnectEvent{" + + "player=" + player + + ", previousServer=" + previousServer + + '}'; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java index 119e03785..9eb90094c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/TransitionSessionHandler.java @@ -4,6 +4,7 @@ import static com.velocitypowered.proxy.connection.backend.BackendConnectionPhas import static com.velocitypowered.proxy.connection.forge.legacy.LegacyForgeHandshakeBackendPhase.HELLO; import com.velocitypowered.api.event.player.ServerConnectedEvent; +import com.velocitypowered.api.event.player.ServerPostConnectEvent; import com.velocitypowered.proxy.VelocityServer; import com.velocitypowered.proxy.connection.ConnectionTypes; import com.velocitypowered.proxy.connection.MinecraftConnection; @@ -113,6 +114,8 @@ public class TransitionSessionHandler implements MinecraftSessionHandler { serverConn.getPlayer().setConnectedServer(serverConn); // We're done! :) + server.getEventManager().fireAndForget(new ServerPostConnectEvent(serverConn.getPlayer(), + existingConnection == null ? null : existingConnection.getServer())); resultFuture.complete(ConnectionRequestResults.successful(serverConn.getServer())); }, smc.eventLoop()) .exceptionally(exc -> {