2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Riley Park <rileysebastianpark@gmail.com>
Date: Wed, 13 Apr 2016 20:20:18 -0700
Subject: [PATCH] Add handshake event to allow plugins to handle client
handshaking logic themselves
diff --git a/src/main/java/com/destroystokyo/paper/event/player/PlayerHandshakeEvent.java b/src/main/java/com/destroystokyo/paper/event/player/PlayerHandshakeEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerHandshakeEvent.java
@@ -0,0 +0,0 @@
+package com.destroystokyo.paper.event.player;
+
2022-06-07 22:23:16 +02:00
+import com.google.common.base.Preconditions;
2024-09-29 21:52:13 +02:00
+import java.util.UUID;
2021-06-11 14:02:28 +02:00
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
2022-05-26 21:12:14 +02:00
+import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
2021-06-11 14:02:28 +02:00
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2024-09-29 21:52:13 +02:00
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
2022-05-26 21:12:14 +02:00
+
2021-06-11 14:02:28 +02:00
+/**
+ * This event is fired during a player handshake.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * If there are no listeners listening to this event, the logic default
+ * to your server platform will be run.
2021-06-11 14:02:28 +02:00
+ *
+ * <p>WARNING: TAMPERING WITH THIS EVENT CAN BE DANGEROUS</p>
+ */
2024-09-29 21:52:13 +02:00
+@NullMarked
2021-06-11 14:02:28 +02:00
+public class PlayerHandshakeEvent extends Event implements Cancellable {
+
2024-02-01 10:15:57 +01:00
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2024-09-29 21:52:13 +02:00
+ private final String originalHandshake;
+ private final String originalSocketAddressHostname;
+ private @Nullable String serverHostname;
+ private @Nullable String socketAddressHostname;
+ private @Nullable UUID uniqueId;
+ private @Nullable String propertiesJson;
2021-06-11 14:02:28 +02:00
+ private boolean failed;
+ private Component failMessage = Component.text("If you wish to use IP forwarding, please enable it in your BungeeCord config as well!", NamedTextColor.YELLOW);
+
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
+
2021-06-11 14:02:28 +02:00
+ @Deprecated
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-29 21:52:13 +02:00
+ public PlayerHandshakeEvent(final String originalHandshake, final boolean cancelled) {
2021-06-11 14:02:28 +02:00
+ this(originalHandshake, "127.0.0.1", cancelled);
+ }
+
2024-02-01 10:15:57 +01:00
+ @ApiStatus.Internal
2024-09-29 21:52:13 +02:00
+ public PlayerHandshakeEvent(final String originalHandshake, final String originalSocketAddressHostname, final boolean cancelled) {
2021-06-11 14:02:28 +02:00
+ super(true);
+ this.originalHandshake = originalHandshake;
+ this.originalSocketAddressHostname = originalSocketAddressHostname;
+ this.cancelled = cancelled;
+ }
+
+ /**
+ * Determines if this event is cancelled.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * When this event is cancelled, custom handshake logic will not
+ * be processed.
2021-06-11 14:02:28 +02:00
+ *
+ * @return {@code true} if this event is cancelled, {@code false} otherwise
+ */
+ @Override
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ /**
+ * Sets if this event is cancelled.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * When this event is cancelled, custom handshake logic will not
+ * be processed.
2021-06-11 14:02:28 +02:00
+ *
2024-02-01 10:15:57 +01:00
+ * @param cancel {@code true} if this event is cancelled, {@code false} otherwise
2021-06-11 14:02:28 +02:00
+ */
+ @Override
2024-09-29 21:52:13 +02:00
+ public void setCancelled(final boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Gets the original handshake string.
+ *
+ * @return the original handshake string
+ */
+ public String getOriginalHandshake() {
+ return this.originalHandshake;
+ }
+
+ /**
+ * Gets the original socket address hostname.
+ *
+ * <p>This does not include the port.</p>
+ * <p>In cases where this event is manually fired and the plugin wasn't updated yet, the default is {@code "127.0.0.1"}.</p>
+ *
+ * @return the original socket address hostname
+ */
+ public String getOriginalSocketAddressHostname() {
+ return this.originalSocketAddressHostname;
+ }
+
+ /**
+ * Gets the server hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @return the server hostname string
+ */
2024-09-29 21:52:13 +02:00
+ public @Nullable String getServerHostname() {
2021-06-11 14:02:28 +02:00
+ return this.serverHostname;
+ }
+
+ /**
+ * Sets the server hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @param serverHostname the server hostname string
+ */
2024-09-29 21:52:13 +02:00
+ public void setServerHostname(final String serverHostname) {
2021-06-11 14:02:28 +02:00
+ this.serverHostname = serverHostname;
+ }
+
+ /**
+ * Gets the socket address hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @return the socket address hostname string
+ */
2024-09-29 21:52:13 +02:00
+ public @Nullable String getSocketAddressHostname() {
2021-06-11 14:02:28 +02:00
+ return this.socketAddressHostname;
+ }
+
+ /**
+ * Sets the socket address hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @param socketAddressHostname the socket address hostname string
+ */
2024-09-29 21:52:13 +02:00
+ public void setSocketAddressHostname(final String socketAddressHostname) {
2021-06-11 14:02:28 +02:00
+ this.socketAddressHostname = socketAddressHostname;
+ }
+
+ /**
+ * Gets the unique id.
+ *
+ * @return the unique id
+ */
2024-09-29 21:52:13 +02:00
+ public @Nullable UUID getUniqueId() {
2021-06-11 14:02:28 +02:00
+ return this.uniqueId;
+ }
+
+ /**
+ * Sets the unique id.
+ *
+ * @param uniqueId the unique id
+ */
2024-09-29 21:52:13 +02:00
+ public void setUniqueId(final UUID uniqueId) {
2021-06-11 14:02:28 +02:00
+ this.uniqueId = uniqueId;
+ }
+
+ /**
+ * Gets the profile properties.
+ *
+ * <p>This should be a valid JSON string.</p>
+ *
+ * @return the profile properties, as JSON
+ */
2024-09-29 21:52:13 +02:00
+ public @Nullable String getPropertiesJson() {
2021-06-11 14:02:28 +02:00
+ return this.propertiesJson;
+ }
+
+ /**
+ * Determines if authentication failed.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * When {@code true}, the client connecting will be disconnected
+ * with the {@link #getFailMessage() fail message}.
2021-06-11 14:02:28 +02:00
+ *
+ * @return {@code true} if authentication failed, {@code false} otherwise
+ */
+ public boolean isFailed() {
+ return this.failed;
+ }
+
+ /**
+ * Sets if authentication failed and the client should be disconnected.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * When {@code true}, the client connecting will be disconnected
+ * with the {@link #getFailMessage() fail message}.
2021-06-11 14:02:28 +02:00
+ *
+ * @param failed {@code true} if authentication failed, {@code false} otherwise
+ */
2024-09-29 21:52:13 +02:00
+ public void setFailed(final boolean failed) {
2021-06-11 14:02:28 +02:00
+ this.failed = failed;
+ }
+
+ /**
+ * Sets the profile properties.
+ *
+ * <p>This should be a valid JSON string.</p>
+ *
+ * @param propertiesJson the profile properties, as JSON
+ */
2024-09-29 21:52:13 +02:00
+ public void setPropertiesJson(final String propertiesJson) {
2021-06-11 14:02:28 +02:00
+ this.propertiesJson = propertiesJson;
+ }
+
+ /**
+ * Gets the message to display to the client when authentication fails.
+ *
+ * @return the message to display to the client
+ */
+ public Component failMessage() {
+ return this.failMessage;
+ }
+
+ /**
+ * Sets the message to display to the client when authentication fails.
+ *
+ * @param failMessage the message to display to the client
+ */
2024-09-29 21:52:13 +02:00
+ public void failMessage(final Component failMessage) {
2021-06-11 14:02:28 +02:00
+ this.failMessage = failMessage;
+ }
+
+ /**
+ * Gets the message to display to the client when authentication fails.
+ *
+ * @return the message to display to the client
+ * @deprecated use {@link #failMessage()}
+ */
+ @Deprecated
+ public String getFailMessage() {
2022-05-26 21:12:14 +02:00
+ return LegacyComponentSerializer.legacySection().serialize(this.failMessage());
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * Sets the message to display to the client when authentication fails.
+ *
+ * @param failMessage the message to display to the client
+ * @deprecated use {@link #failMessage(Component)}
+ */
+ @Deprecated
2024-09-29 21:52:13 +02:00
+ public void setFailMessage(final String failMessage) {
2022-06-11 09:37:00 +02:00
+ Preconditions.checkArgument(failMessage != null && !failMessage.isEmpty(), "fail message cannot be null or empty");
2022-05-26 21:12:14 +02:00
+ this.failMessage(LegacyComponentSerializer.legacySection().deserialize(failMessage));
2021-06-11 14:02:28 +02:00
+ }
+
+ @Override
+ public HandlerList getHandlers() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+
+ public static HandlerList getHandlerList() {
2024-02-01 10:15:57 +01:00
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}