2020-05-06 11:48:49 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2016-04-14 05:23:07 +02:00
From: kashike <kashike@vq.lc>
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
2021-03-09 02:11:17 +01:00
index 0000000000000000000000000000000000000000..a077962fa786a3291849abfa823c7f0ec4664fce
2016-04-14 05:23:07 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/player/PlayerHandshakeEvent.java
2021-02-23 16:47:06 +01:00
@@ -0,0 +1,277 @@
2016-04-14 05:23:07 +02:00
+package com.destroystokyo.paper.event.player;
+
2021-02-21 20:45:33 +01:00
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.format.NamedTextColor;
2016-04-14 05:23:07 +02:00
+import org.apache.commons.lang.Validate;
2021-03-09 02:11:17 +01:00
+import org.bukkit.Bukkit;
2016-04-14 05:23:07 +02:00
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+
+import java.util.UUID;
2019-03-20 01:28:15 +01:00
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
2016-04-14 05:23:07 +02:00
+
+/**
+ * This event is fired during a player handshake.
+ *
+ * <p>If there are no listeners listening to this event, the logic default
+ * to your server platform will be ran.</p>
+ *
+ * <p>WARNING: TAMPERING WITH THIS EVENT CAN BE DANGEROUS</p>
+ */
+public class PlayerHandshakeEvent extends Event implements Cancellable {
+
+ private static final HandlerList HANDLERS = new HandlerList();
2019-03-20 01:28:15 +01:00
+ @NotNull private final String originalHandshake;
2021-02-23 16:47:06 +01:00
+ @NotNull private final String originalSocketAddressHostname;
2016-04-14 05:23:07 +02:00
+ private boolean cancelled;
2019-03-20 01:28:15 +01:00
+ @Nullable private String serverHostname;
+ @Nullable private String socketAddressHostname;
+ @Nullable private UUID uniqueId;
+ @Nullable private String propertiesJson;
2016-04-14 05:23:07 +02:00
+ private boolean failed;
2021-02-21 20:45:33 +01:00
+ private Component failMessage = Component.text("If you wish to use IP forwarding, please enable it in your BungeeCord config as well!", NamedTextColor.YELLOW);
2016-04-14 05:23:07 +02:00
+
+ /**
+ * Creates a new {@link PlayerHandshakeEvent}.
+ *
+ * @param originalHandshake the original handshake string
+ * @param cancelled if this event is enabled
2021-02-23 16:47:06 +01:00
+ *
+ * @deprecated in favour of {@link PlayerHandshakeEvent(String, String, boolean)}
2016-04-14 05:23:07 +02:00
+ */
2021-02-23 16:47:06 +01:00
+ @Deprecated
2019-03-20 01:28:15 +01:00
+ public PlayerHandshakeEvent(@NotNull String originalHandshake, boolean cancelled) {
2021-02-23 16:47:06 +01:00
+ this(originalHandshake, "127.0.0.1", cancelled);
+ }
+
+ /**
+ * Creates a new {@link PlayerHandshakeEvent}.
+ *
+ * @param originalHandshake the original handshake string
+ * @param originalSocketAddressHostname the original socket address hostname
+ * @param cancelled if this event is enabled
+ */
+ public PlayerHandshakeEvent(@NotNull String originalHandshake, @NotNull String originalSocketAddressHostname, boolean cancelled) {
2019-06-05 10:00:51 +02:00
+ super(true);
2016-04-14 05:23:07 +02:00
+ this.originalHandshake = originalHandshake;
2021-02-23 16:47:06 +01:00
+ this.originalSocketAddressHostname = originalSocketAddressHostname;
2016-04-14 05:23:07 +02:00
+ this.cancelled = cancelled;
+ }
+
+ /**
+ * Determines if this event is cancelled.
+ *
+ * <p>When this event is cancelled, custom handshake logic will not
+ * be processed.</p>
+ *
+ * @return {@code true} if this event is cancelled, {@code false} otherwise
+ */
+ @Override
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ /**
+ * Sets if this event is cancelled.
+ *
+ * <p>When this event is cancelled, custom handshake logic will not
+ * be processed.</p>
+ *
+ * @param cancelled {@code true} if this event is cancelled, {@code false} otherwise
+ */
+ @Override
+ public void setCancelled(boolean cancelled) {
+ this.cancelled = cancelled;
+ }
+
+ /**
+ * Gets the original handshake string.
+ *
+ * @return the original handshake string
+ */
2019-03-20 01:28:15 +01:00
+ @NotNull
2016-04-14 05:23:07 +02:00
+ public String getOriginalHandshake() {
+ return this.originalHandshake;
+ }
+
+ /**
2021-02-23 16:47:06 +01:00
+ * 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
+ */
+ @NotNull
+ public String getOriginalSocketAddressHostname() {
+ return this.originalSocketAddressHostname;
+ }
+
+ /**
2016-04-14 05:23:07 +02:00
+ * Gets the server hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @return the server hostname string
+ */
2019-03-20 01:28:15 +01:00
+ @Nullable
2016-04-14 05:23:07 +02:00
+ public String getServerHostname() {
+ return this.serverHostname;
+ }
+
+ /**
+ * Sets the server hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @param serverHostname the server hostname string
+ */
2019-03-20 01:28:15 +01:00
+ public void setServerHostname(@NotNull String serverHostname) {
2016-04-14 05:23:07 +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
+ */
2019-03-20 01:28:15 +01:00
+ @Nullable
2016-04-14 05:23:07 +02:00
+ public String getSocketAddressHostname() {
+ return this.socketAddressHostname;
+ }
+
+ /**
+ * Sets the socket address hostname string.
+ *
+ * <p>This should not include the port.</p>
+ *
+ * @param socketAddressHostname the socket address hostname string
+ */
2019-03-20 01:28:15 +01:00
+ public void setSocketAddressHostname(@NotNull String socketAddressHostname) {
2016-04-14 05:23:07 +02:00
+ this.socketAddressHostname = socketAddressHostname;
+ }
+
+ /**
+ * Gets the unique id.
+ *
+ * @return the unique id
+ */
2019-03-20 01:28:15 +01:00
+ @Nullable
2016-04-14 05:23:07 +02:00
+ public UUID getUniqueId() {
+ return this.uniqueId;
+ }
+
+ /**
+ * Sets the unique id.
+ *
+ * @param uniqueId the unique id
+ */
2019-03-20 01:28:15 +01:00
+ public void setUniqueId(@NotNull UUID uniqueId) {
2016-04-14 05:23:07 +02:00
+ this.uniqueId = uniqueId;
+ }
+
+ /**
+ * Gets the profile properties.
+ *
+ * <p>This should be a valid JSON string.</p>
+ *
+ * @return the profile properties, as JSON
+ */
2019-03-20 01:28:15 +01:00
+ @Nullable
2016-04-14 05:23:07 +02:00
+ public String getPropertiesJson() {
+ return this.propertiesJson;
+ }
+
+ /**
+ * Determines if authentication failed.
+ *
+ * <p>When {@code true}, the client connecting will be disconnected
+ * with the {@link #getFailMessage() fail message}.</p>
+ *
+ * @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.
+ *
+ * <p>When {@code true}, the client connecting will be disconnected
+ * with the {@link #getFailMessage() fail message}.</p>
+ *
+ * @param failed {@code true} if authentication failed, {@code false} otherwise
+ */
+ public void setFailed(boolean failed) {
+ this.failed = failed;
+ }
+
+ /**
+ * Sets the profile properties.
+ *
+ * <p>This should be a valid JSON string.</p>
+ *
+ * @param propertiesJson the profile properties, as JSON
+ */
2019-03-20 01:28:15 +01:00
+ public void setPropertiesJson(@NotNull String propertiesJson) {
2016-04-14 05:23:07 +02:00
+ this.propertiesJson = propertiesJson;
+ }
+
+ /**
+ * Gets the message to display to the client when authentication fails.
+ *
+ * @return the message to display to the client
+ */
2019-03-20 01:28:15 +01:00
+ @NotNull
2021-02-21 20:45:33 +01:00
+ public Component failMessage() {
2016-04-14 05:23:07 +02:00
+ return this.failMessage;
+ }
+
+ /**
+ * Sets the message to display to the client when authentication fails.
+ *
+ * @param failMessage the message to display to the client
+ */
2021-02-21 20:45:33 +01:00
+ public void failMessage(@NotNull Component failMessage) {
+ 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()}
+ */
+ @NotNull
+ @Deprecated
+ public String getFailMessage() {
2021-03-09 02:11:17 +01:00
+ return Bukkit.getUnsafe().legacyComponentSerializer().serialize(this.failMessage());
2021-02-21 20:45:33 +01: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
2019-03-20 01:28:15 +01:00
+ public void setFailMessage(@NotNull String failMessage) {
2016-04-14 05:23:07 +02:00
+ Validate.notEmpty(failMessage, "fail message cannot be null or empty");
2021-03-09 02:11:17 +01:00
+ this.failMessage(Bukkit.getUnsafe().legacyComponentSerializer().deserialize(failMessage));
2016-04-14 05:23:07 +02:00
+ }
+
2019-03-20 01:28:15 +01:00
+ @NotNull
2016-04-14 05:23:07 +02:00
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLERS;
+ }
+
2019-03-20 01:28:15 +01:00
+ @NotNull
2016-04-14 05:23:07 +02:00
+ public static HandlerList getHandlerList() {
+ return HANDLERS;
+ }
+}