Skip to content

Commit ec8ad47

Browse files
committed
Add server-id hash to LoginEvent
1 parent 8f65a81 commit ec8ad47

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

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

Lines changed: 19 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,17 +23,34 @@
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

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

3242
public Player getPlayer() {
3343
return player;
3444
}
3545

46+
/**
47+
* Returns the server ID hash that was sent to Mojang to authenticate the player.
48+
* If the connection was in offline-mode, this returns {@code null}.
49+
*/
50+
public @Nullable String getServerIdHash() {
51+
return serverIdHash;
52+
}
53+
3654
@Override
3755
public ComponentResult getResult() {
3856
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
});
@@ -254,7 +254,7 @@ public boolean handle(EncryptionResponsePacket packet) {
254254
}
255255
// All went well, initialize the session.
256256
mcConnection.setActiveSessionHandler(StateRegistry.LOGIN,
257-
new AuthSessionHandler(server, inbound, profile, true));
257+
new AuthSessionHandler(server, inbound, profile, true, serverId));
258258
} else if (response.statusCode() == 204) {
259259
// Apparently an offline-mode user logged onto this online-mode proxy.
260260
inbound.disconnect(

0 commit comments

Comments
 (0)