From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: kashike <kashike@vq.lc>
Date: Wed, 13 Apr 2016 20:21:38 -0700
Subject: [PATCH] Add handshake event to allow plugins to handle client
 handshaking logic themselves


diff --git a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
index 64e3d3626a30d4dfe15ed1f7cb3f0480ece5edce..7ae4279768b70a4fdc8f4438898871a17c8fe402 100644
--- a/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerHandshakePacketListenerImpl.java
@@ -121,9 +121,43 @@ public class ServerHandshakePacketListenerImpl implements ServerHandshakePacketL
             this.connection.disconnect((Component) ichatmutablecomponent);
         } else {
             this.connection.setupInboundProtocol(LoginProtocols.SERVERBOUND, new ServerLoginPacketListenerImpl(this.server, this.connection, transfer));
+            // Paper start - PlayerHandshakeEvent
+            boolean proxyLogicEnabled = org.spigotmc.SpigotConfig.bungee;
+            boolean handledByEvent = false;
+            // Try and handle the handshake through the event
+            if (com.destroystokyo.paper.event.player.PlayerHandshakeEvent.getHandlerList().getRegisteredListeners().length != 0) { // Hello? Can you hear me?
+                java.net.SocketAddress socketAddress = this.connection.address;
+                String hostnameOfRemote = socketAddress instanceof java.net.InetSocketAddress ? ((java.net.InetSocketAddress) socketAddress).getHostString() : InetAddress.getLoopbackAddress().getHostAddress();
+                com.destroystokyo.paper.event.player.PlayerHandshakeEvent event = new com.destroystokyo.paper.event.player.PlayerHandshakeEvent(packet.hostName(), hostnameOfRemote, !proxyLogicEnabled);
+                if (event.callEvent()) {
+                    // If we've failed somehow, let the client know so and go no further.
+                    if (event.isFailed()) {
+                        Component component = io.papermc.paper.adventure.PaperAdventure.asVanilla(event.failMessage());
+                        this.connection.send(new ClientboundLoginDisconnectPacket(component));
+                        this.connection.disconnect(component);
+                        return;
+                    }
+
+                    if (event.getServerHostname() != null) {
+                        // change hostname
+                        packet = new ClientIntentionPacket(
+                            packet.protocolVersion(),
+                            event.getServerHostname(),
+                            packet.port(),
+                            packet.intention()
+                        );
+                    }
+                    if (event.getSocketAddressHostname() != null) this.connection.address = new java.net.InetSocketAddress(event.getSocketAddressHostname(), socketAddress instanceof java.net.InetSocketAddress ? ((java.net.InetSocketAddress) socketAddress).getPort() : 0);
+                    this.connection.spoofedUUID = event.getUniqueId();
+                    this.connection.spoofedProfile = gson.fromJson(event.getPropertiesJson(), com.mojang.authlib.properties.Property[].class);
+                    handledByEvent = true; // Hooray, we did it!
+                }
+            }
+            // Paper end
             // Spigot Start
             String[] split = packet.hostName().split("\00");
-            if (org.spigotmc.SpigotConfig.bungee) {
+            if (!handledByEvent && proxyLogicEnabled) { // Paper
+                // if (org.spigotmc.SpigotConfig.bungee) { // Paper - comment out, we check above!
                 if ( ( split.length == 3 || split.length == 4 ) && ( ServerHandshakePacketListenerImpl.HOST_PATTERN.matcher( split[1] ).matches() ) ) {
                     this.connection.hostname = split[0];
                     this.connection.address = new java.net.InetSocketAddress(split[1], ((java.net.InetSocketAddress) this.connection.getRemoteAddress()).getPort());