From e1f3b6b66f101a63233e6a732b982699e5ef578c Mon Sep 17 00:00:00 2001 From: Kezz Date: Fri, 9 Feb 2024 16:23:47 +0100 Subject: [PATCH] Expose connecting player's UUID in the PreLoginEvent (#1009) * feature: Expose connecting player's UUID in the PreLoginEvent * Applied suggestions * Updated the javadocs compatible version to Java 17 --------- Co-authored-by: Adrian --- api/build.gradle.kts | 2 +- .../api/event/connection/PreLoginEvent.java | 45 ++++++++++++++----- .../client/InitialLoginSessionHandler.java | 10 ++--- .../protocol/packet/ServerLoginPacket.java | 6 ++- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/api/build.gradle.kts b/api/build.gradle.kts index b68555040..8f0dec6cd 100644 --- a/api/build.gradle.kts +++ b/api/build.gradle.kts @@ -59,7 +59,7 @@ tasks { val o = options as StandardJavadocDocletOptions o.encoding = "UTF-8" - o.source = "8" + o.source = "17" o.links( "https://www.slf4j.org/apidocs/", diff --git a/api/src/main/java/com/velocitypowered/api/event/connection/PreLoginEvent.java b/api/src/main/java/com/velocitypowered/api/event/connection/PreLoginEvent.java index ae3812d27..6ac3f57e1 100644 --- a/api/src/main/java/com/velocitypowered/api/event/connection/PreLoginEvent.java +++ b/api/src/main/java/com/velocitypowered/api/event/connection/PreLoginEvent.java @@ -12,6 +12,7 @@ import com.velocitypowered.api.event.ResultedEvent; import com.velocitypowered.api.event.annotation.AwaitingEvent; import com.velocitypowered.api.proxy.InboundConnection; import java.util.Optional; +import java.util.UUID; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -33,17 +34,32 @@ public final class PreLoginEvent implements ResultedEvent "allowed"; + case FORCE_OFFLINE -> "allowed with force offline mode"; + case FORCE_ONLINE -> "allowed with online mode"; + default -> "denied"; + }; } /** diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java index 3a49172ec..6b1ab3707 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/InitialLoginSessionHandler.java @@ -74,7 +74,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { private @MonotonicNonNull ServerLoginPacket login; private byte[] verify = EMPTY_BYTE_ARRAY; private LoginState currentState = LoginState.LOGIN_PACKET_EXPECTED; - private boolean forceKeyAuthentication; + private final boolean forceKeyAuthentication; InitialLoginSessionHandler(VelocityServer server, MinecraftConnection mcConnection, LoginInboundConnection inbound) { @@ -100,8 +100,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { boolean isKeyValid; if (playerKey.getKeyRevision() == IdentifiedKey.Revision.LINKED_V2 - && playerKey instanceof IdentifiedKeyImpl) { - IdentifiedKeyImpl keyImpl = (IdentifiedKeyImpl) playerKey; + && playerKey instanceof final IdentifiedKeyImpl keyImpl) { isKeyValid = keyImpl.internalAddHolder(packet.getHolderUuid()); } else { isKeyValid = playerKey.isSignatureValid(); @@ -120,7 +119,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { inbound.setPlayerKey(playerKey); this.login = packet; - PreLoginEvent event = new PreLoginEvent(inbound, login.getUsername()); + final PreLoginEvent event = new PreLoginEvent(inbound, login.getUsername(), login.getHolderUuid()); server.getEventManager().fire(event).thenRunAsync(() -> { if (mcConnection.isClosed()) { // The player was disconnected @@ -245,8 +244,7 @@ public class InitialLoginSessionHandler implements MinecraftSessionHandler { // Not so fast, now we verify the public key for 1.19.1+ if (inbound.getIdentifiedKey() != null && inbound.getIdentifiedKey().getKeyRevision() == IdentifiedKey.Revision.LINKED_V2 - && inbound.getIdentifiedKey() instanceof IdentifiedKeyImpl) { - IdentifiedKeyImpl key = (IdentifiedKeyImpl) inbound.getIdentifiedKey(); + && inbound.getIdentifiedKey() instanceof final IdentifiedKeyImpl key) { if (!key.internalAddHolder(profile.getId())) { inbound.disconnect( Component.translatable("multiplayer.disconnect.invalid_public_key")); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java index f3f2cfb8c..210447cc7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/ServerLoginPacket.java @@ -74,7 +74,11 @@ public class ServerLoginPacket implements MinecraftPacket { @Override public String toString() { - return "ServerLogin{" + "username='" + username + '\'' + "playerKey='" + playerKey + '\'' + '}'; + return "ServerLogin{" + + "username='" + username + '\'' + + "uuid='" + holderUuid + '\'' + + "playerKey='" + playerKey + '\'' + + '}'; } @Override