Skip to content

Commit 2535751

Browse files
authored
Add server-id hash to LoginEvent (#1027)
1 parent 7e01491 commit 2535751

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

api/src/main/java/com/velocitypowered/api/event/connection/LoginEvent.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.velocitypowered.api.event.ResultedEvent;
1212
import com.velocitypowered.api.event.annotation.AwaitingEvent;
1313
import com.velocitypowered.api.proxy.Player;
14+
import org.checkerframework.checker.nullness.qual.Nullable;
1415

1516
/**
1617
* This event is fired once the player has been authenticated, but before they connect to a server.
@@ -22,18 +23,33 @@
2223
public final class LoginEvent implements ResultedEvent<ResultedEvent.ComponentResult> {
2324

2425
private final Player player;
26+
private final String serverIdHash;
2527
private ComponentResult result;
2628

2729
/**
2830
* Constructs a new {@link LoginEvent}.
2931
*
3032
* @param player the player who has completed authentication
33+
* @param serverIdHash the server ID hash sent to Mojang for authentication,
34+
* or {@code null} if the connection is in offline-mode
3135
*/
32-
public LoginEvent(Player player) {
36+
public LoginEvent(Player player, @Nullable String serverIdHash) {
3337
this.player = Preconditions.checkNotNull(player, "player");
38+
this.serverIdHash = serverIdHash;
3439
this.result = ComponentResult.allowed();
3540
}
3641

42+
/**
43+
* Constructs a new {@link LoginEvent}.
44+
*
45+
* @param player the player who has completed authentication
46+
* @deprecated Use {@link #LoginEvent(Player, String)}.
47+
*/
48+
@Deprecated(forRemoval = true)
49+
public LoginEvent(Player player) {
50+
this(player, null);
51+
}
52+
3753
/**
3854
* Returns the player who has completed authentication.
3955
*
@@ -43,6 +59,16 @@ public Player getPlayer() {
4359
return player;
4460
}
4561

62+
/**
63+
* Returns the server ID hash that was sent to Mojang to authenticate the player.
64+
* If the connection was in offline-mode, this returns {@code null}.
65+
*
66+
* @return the server ID hash that was sent to Mojang to authenticate the player
67+
*/
68+
public @Nullable String getServerIdHash() {
69+
return serverIdHash;
70+
}
71+
4672
@Override
4773
public ComponentResult getResult() {
4874
return result;

proxy/src/main/java/com/velocitypowered/proxy/connection/client/AuthSessionHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,16 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
6969
private @MonotonicNonNull ConnectedPlayer connectedPlayer;
7070
private final boolean onlineMode;
7171
private State loginState = State.START; // 1.20.2+
72+
private final String serverIdHash;
7273

7374
AuthSessionHandler(VelocityServer server, LoginInboundConnection inbound,
74-
GameProfile profile, boolean onlineMode) {
75+
GameProfile profile, boolean onlineMode, String serverIdHash) {
7576
this.server = Preconditions.checkNotNull(server, "server");
7677
this.inbound = Preconditions.checkNotNull(inbound, "inbound");
7778
this.profile = Preconditions.checkNotNull(profile, "profile");
7879
this.onlineMode = onlineMode;
7980
this.mcConnection = inbound.delegatedConnection();
81+
this.serverIdHash = serverIdHash;
8082
}
8183

8284
@Override
@@ -213,7 +215,7 @@ public boolean handle(ServerboundCookieResponsePacket packet) {
213215
private void completeLoginProtocolPhaseAndInitialize(ConnectedPlayer player) {
214216
mcConnection.setAssociation(player);
215217

216-
server.getEventManager().fire(new LoginEvent(player)).thenAcceptAsync(event -> {
218+
server.getEventManager().fire(new LoginEvent(player, serverIdHash)).thenAcceptAsync(event -> {
217219
if (mcConnection.isClosed()) {
218220
// The player was disconnected
219221
server.getEventManager().fireAndForget(new DisconnectEvent(player,

proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public boolean handle(ServerLoginPacket packet) {
152152
} else {
153153
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
154154
new AuthSessionHandler(server, inbound,
155-
GameProfile.forOfflinePlayer(login.getUsername()), false));
155+
GameProfile.forOfflinePlayer(login.getUsername()), false, null));
156156
}
157157
});
158158
});
@@ -255,7 +255,7 @@ public boolean handle(EncryptionResponsePacket packet) {
255255
}
256256
// All went well, initialize the session.
257257
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
258-
new AuthSessionHandler(server, inbound, profile, true));
258+
new AuthSessionHandler(server, inbound, profile, true, serverId));
259259
} else if (response.statusCode() == 204) {
260260
// Apparently an offline-mode user logged onto this online-mode proxy.
261261
inbound.disconnect(

0 commit comments

Comments
 (0)