Add server-id hash to LoginEvent (#1027)

This commit is contained in:
Rocco
2026-02-10 18:59:34 +00:00
committed by GitHub
parent 7e01491e2f
commit 2535751cd9
3 changed files with 33 additions and 5 deletions

View File

@@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.ResultedEvent;
import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import org.checkerframework.checker.nullness.qual.Nullable;
/** /**
* This event is fired once the player has been authenticated, but before they connect to a server. * This event is fired once the player has been authenticated, but before they connect to a server.
@@ -22,18 +23,33 @@ import com.velocitypowered.api.proxy.Player;
public final class LoginEvent implements ResultedEvent<ResultedEvent.ComponentResult> { public final class LoginEvent implements ResultedEvent<ResultedEvent.ComponentResult> {
private final Player player; private final Player player;
private final String serverIdHash;
private ComponentResult result; private ComponentResult result;
/** /**
* Constructs a new {@link LoginEvent}. * Constructs a new {@link LoginEvent}.
* *
* @param player the player who has completed authentication * @param player the player who has completed authentication
* @param serverIdHash the server ID hash sent to Mojang for authentication,
* or {@code null} if the connection is in offline-mode
*/ */
public LoginEvent(Player player) { public LoginEvent(Player player, @Nullable String serverIdHash) {
this.player = Preconditions.checkNotNull(player, "player"); this.player = Preconditions.checkNotNull(player, "player");
this.serverIdHash = serverIdHash;
this.result = ComponentResult.allowed(); this.result = ComponentResult.allowed();
} }
/**
* Constructs a new {@link LoginEvent}.
*
* @param player the player who has completed authentication
* @deprecated Use {@link #LoginEvent(Player, String)}.
*/
@Deprecated(forRemoval = true)
public LoginEvent(Player player) {
this(player, null);
}
/** /**
* Returns the player who has completed authentication. * Returns the player who has completed authentication.
* *
@@ -43,6 +59,16 @@ public final class LoginEvent implements ResultedEvent<ResultedEvent.ComponentRe
return player; return player;
} }
/**
* Returns the server ID hash that was sent to Mojang to authenticate the player.
* If the connection was in offline-mode, this returns {@code null}.
*
* @return the server ID hash that was sent to Mojang to authenticate the player
*/
public @Nullable String getServerIdHash() {
return serverIdHash;
}
@Override @Override
public ComponentResult getResult() { public ComponentResult getResult() {
return result; return result;

View File

@@ -69,14 +69,16 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
private @MonotonicNonNull ConnectedPlayer connectedPlayer; private @MonotonicNonNull ConnectedPlayer connectedPlayer;
private final boolean onlineMode; private final boolean onlineMode;
private State loginState = State.START; // 1.20.2+ private State loginState = State.START; // 1.20.2+
private final String serverIdHash;
AuthSessionHandler(VelocityServer server, LoginInboundConnection inbound, AuthSessionHandler(VelocityServer server, LoginInboundConnection inbound,
GameProfile profile, boolean onlineMode) { GameProfile profile, boolean onlineMode, String serverIdHash) {
this.server = Preconditions.checkNotNull(server, "server"); this.server = Preconditions.checkNotNull(server, "server");
this.inbound = Preconditions.checkNotNull(inbound, "inbound"); this.inbound = Preconditions.checkNotNull(inbound, "inbound");
this.profile = Preconditions.checkNotNull(profile, "profile"); this.profile = Preconditions.checkNotNull(profile, "profile");
this.onlineMode = onlineMode; this.onlineMode = onlineMode;
this.mcConnection = inbound.delegatedConnection(); this.mcConnection = inbound.delegatedConnection();
this.serverIdHash = serverIdHash;
} }
@Override @Override
@@ -213,7 +215,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
private void completeLoginProtocolPhaseAndInitialize(ConnectedPlayer player) { private void completeLoginProtocolPhaseAndInitialize(ConnectedPlayer player) {
mcConnection.setAssociation(player); mcConnection.setAssociation(player);
server.getEventManager().fire(new LoginEvent(player)).thenAcceptAsync(event -> { server.getEventManager().fire(new LoginEvent(player, serverIdHash)).thenAcceptAsync(event -> {
if (mcConnection.isClosed()) { if (mcConnection.isClosed()) {
// The player was disconnected // The player was disconnected
server.getEventManager().fireAndForget(new DisconnectEvent(player, server.getEventManager().fireAndForget(new DisconnectEvent(player,

View File

@@ -152,7 +152,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
} else { } else {
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN, mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
new AuthSessionHandler(server, inbound, new AuthSessionHandler(server, inbound,
GameProfile.forOfflinePlayer(login.getUsername()), false)); GameProfile.forOfflinePlayer(login.getUsername()), false, null));
} }
}); });
}); });
@@ -255,7 +255,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler {
} }
// All went well, initialize the session. // All went well, initialize the session.
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN, mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
new AuthSessionHandler(server, inbound, profile, true)); new AuthSessionHandler(server, inbound, profile, true, serverId));
} else if (response.statusCode() == 204) { } else if (response.statusCode() == 204) {
// Apparently an offline-mode user logged onto this online-mode proxy. // Apparently an offline-mode user logged onto this online-mode proxy.
inbound.disconnect( inbound.disconnect(