Skip to content

Commit f36e1fa

Browse files
committed
Add server-id hash to LoginEvent
1 parent bda1430 commit f36e1fa

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,34 @@
2222
public final class LoginEvent implements ResultedEvent<ResultedEvent.ComponentResult> {
2323

2424
private final Player player;
25+
private final String serverIdHash;
2526
private ComponentResult result;
2627

27-
public LoginEvent(Player player) {
28+
/**
29+
* Creates a new login event.
30+
*
31+
* @param player the player who logged in
32+
* @param serverIdHash the server ID hash sent to Mojang for authentication,
33+
* or {@code null} if the connection is in offline-mode
34+
*/
35+
public LoginEvent(Player player, String serverIdHash) {
2836
this.player = Preconditions.checkNotNull(player, "player");
37+
this.serverIdHash = serverIdHash;
2938
this.result = ComponentResult.allowed();
3039
}
3140

3241
public Player getPlayer() {
3342
return player;
3443
}
3544

45+
/**
46+
* Returns the server ID hash that was sent to Mojang to authenticate the player.
47+
* If the connection was in offline-mode, this returns {@code null}.
48+
*/
49+
public String getServerIdHash() {
50+
return serverIdHash;
51+
}
52+
3653
@Override
3754
public ComponentResult getResult() {
3855
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
@@ -64,14 +64,16 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
6464
private GameProfile profile;
6565
private @MonotonicNonNull ConnectedPlayer connectedPlayer;
6666
private final boolean onlineMode;
67+
private final String serverIdHash;
6768

6869
AuthSessionHandler(VelocityServer server, LoginInboundConnection inbound,
69-
GameProfile profile, boolean onlineMode) {
70+
GameProfile profile, boolean onlineMode, String serverIdHash) {
7071
this.server = Preconditions.checkNotNull(server, "server");
7172
this.inbound = Preconditions.checkNotNull(inbound, "inbound");
7273
this.profile = Preconditions.checkNotNull(profile, "profile");
7374
this.onlineMode = onlineMode;
7475
this.mcConnection = inbound.delegatedConnection();
76+
this.serverIdHash = serverIdHash;
7577
}
7678

7779
@Override
@@ -174,7 +176,7 @@ private void completeLoginProtocolPhaseAndInitialize(ConnectedPlayer player) {
174176
mcConnection.setAssociation(player);
175177
mcConnection.setState(StateRegistry.PLAY);
176178

177-
server.getEventManager().fire(new LoginEvent(player))
179+
server.getEventManager().fire(new LoginEvent(player, serverIdHash))
178180
.thenAcceptAsync(event -> {
179181
if (mcConnection.isClosed()) {
180182
// The player was disconnected

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
@@ -151,7 +151,7 @@ public boolean handle(ServerLogin packet) {
151151
this.currentState = LoginState.ENCRYPTION_REQUEST_SENT;
152152
} else {
153153
mcConnection.setSessionHandler(new AuthSessionHandler(
154-
server, inbound, GameProfile.forOfflinePlayer(login.getUsername()), false
154+
server, inbound, GameProfile.forOfflinePlayer(login.getUsername()), false, null
155155
));
156156
}
157157
});
@@ -247,7 +247,7 @@ public boolean handle(EncryptionResponse packet) {
247247
}
248248
// All went well, initialize the session.
249249
mcConnection.setSessionHandler(new AuthSessionHandler(
250-
server, inbound, profile, true
250+
server, inbound, profile, true, serverId
251251
));
252252
} else if (profileResponse.getStatusCode() == 204) {
253253
// Apparently an offline-mode user logged onto this online-mode proxy.

0 commit comments

Comments
 (0)