--- a/net/minecraft/server/network/LoginListener.java
+++ b/net/minecraft/server/network/LoginListener.java
@@ -36,6 +36,13 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import net.minecraft.network.chat.ChatComponentText;
+import org.bukkit.craftbukkit.util.Waitable;
+import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
+import org.bukkit.event.player.PlayerPreLoginEvent;
+// CraftBukkit end
+
 public class LoginListener implements PacketLoginInListener {
 
     private static final AtomicInteger UNIQUE_THREAD_ID = new AtomicInteger(0);
@@ -52,6 +59,7 @@
     private final String serverId;
     @Nullable
     private EntityPlayer delayedAcceptPlayer;
+    public String hostname = ""; // CraftBukkit - add field
 
     public LoginListener(MinecraftServer minecraftserver, NetworkManager networkmanager) {
         this.state = LoginListener.EnumProtocolState.HELLO;
@@ -80,6 +88,20 @@
 
     }
 
+    // CraftBukkit start
+    @Deprecated
+    public void disconnect(String s) {
+        try {
+            IChatBaseComponent ichatbasecomponent = new ChatComponentText(s);
+            LoginListener.LOGGER.info("Disconnecting {}: {}", this.d(), s);
+            this.connection.sendPacket(new PacketLoginOutDisconnect(ichatbasecomponent));
+            this.connection.close(ichatbasecomponent);
+        } catch (Exception exception) {
+            LoginListener.LOGGER.error("Error whilst disconnecting player", exception);
+        }
+    }
+    // CraftBukkit end
+
     @Override
     public NetworkManager a() {
         return this.connection;
@@ -101,10 +123,12 @@
             this.gameProfile = this.a(this.gameProfile);
         }
 
-        IChatBaseComponent ichatbasecomponent = this.server.getPlayerList().attemptLogin(this.connection.getSocketAddress(), this.gameProfile);
+        // CraftBukkit start - fire PlayerLoginEvent
+        EntityPlayer s = this.server.getPlayerList().attemptLogin(this, this.gameProfile, hostname);
 
-        if (ichatbasecomponent != null) {
-            this.disconnect(ichatbasecomponent);
+        if (s == null) {
+            // this.disconnect(ichatbasecomponent);
+            // CraftBukkit end
         } else {
             this.state = LoginListener.EnumProtocolState.ACCEPTED;
             if (this.server.aw() >= 0 && !this.connection.isLocal()) {
@@ -117,7 +141,7 @@
             EntityPlayer entityplayer = this.server.getPlayerList().getPlayer(this.gameProfile.getId());
 
             try {
-                EntityPlayer entityplayer1 = this.server.getPlayerList().processLogin(this.gameProfile);
+                EntityPlayer entityplayer1 = this.server.getPlayerList().processLogin(this.gameProfile, s); // CraftBukkit - add player reference
 
                 if (entityplayer != null) {
                     this.state = LoginListener.EnumProtocolState.DELAY_ACCEPT;
@@ -191,6 +215,43 @@
                 try {
                     LoginListener.this.gameProfile = LoginListener.this.server.getMinecraftSessionService().hasJoinedServer(new GameProfile((UUID) null, gameprofile.getName()), s, this.a());
                     if (LoginListener.this.gameProfile != null) {
+                        // CraftBukkit start - fire PlayerPreLoginEvent
+                        if (!connection.isConnected()) {
+                            return;
+                        }
+
+                        String playerName = gameProfile.getName();
+                        java.net.InetAddress address = ((java.net.InetSocketAddress) connection.getSocketAddress()).getAddress();
+                        java.util.UUID uniqueId = gameProfile.getId();
+                        final org.bukkit.craftbukkit.CraftServer server = LoginListener.this.server.server;
+
+                        AsyncPlayerPreLoginEvent asyncEvent = new AsyncPlayerPreLoginEvent(playerName, address, uniqueId);
+                        server.getPluginManager().callEvent(asyncEvent);
+
+                        if (PlayerPreLoginEvent.getHandlerList().getRegisteredListeners().length != 0) {
+                            final PlayerPreLoginEvent event = new PlayerPreLoginEvent(playerName, address, uniqueId);
+                            if (asyncEvent.getResult() != PlayerPreLoginEvent.Result.ALLOWED) {
+                                event.disallow(asyncEvent.getResult(), asyncEvent.getKickMessage());
+                            }
+                            Waitable<PlayerPreLoginEvent.Result> waitable = new Waitable<PlayerPreLoginEvent.Result>() {
+                                @Override
+                                protected PlayerPreLoginEvent.Result evaluate() {
+                                    server.getPluginManager().callEvent(event);
+                                    return event.getResult();
+                                }};
+
+                            LoginListener.this.server.processQueue.add(waitable);
+                            if (waitable.get() != PlayerPreLoginEvent.Result.ALLOWED) {
+                                disconnect(event.getKickMessage());
+                                return;
+                            }
+                        } else {
+                            if (asyncEvent.getLoginResult() != AsyncPlayerPreLoginEvent.Result.ALLOWED) {
+                                disconnect(asyncEvent.getKickMessage());
+                                return;
+                            }
+                        }
+                        // CraftBukkit end
                         LoginListener.LOGGER.info("UUID of player {} is {}", LoginListener.this.gameProfile.getName(), LoginListener.this.gameProfile.getId());
                         LoginListener.this.state = LoginListener.EnumProtocolState.READY_TO_ACCEPT;
                     } else if (LoginListener.this.server.isEmbeddedServer()) {
@@ -210,6 +271,11 @@
                         LoginListener.this.disconnect(new ChatMessage("multiplayer.disconnect.authservers_down"));
                         LoginListener.LOGGER.error("Couldn't verify username because servers are unavailable");
                     }
+                    // CraftBukkit start - catch all exceptions
+                } catch (Exception exception) {
+                    disconnect("Failed to verify username!");
+                    server.server.getLogger().log(java.util.logging.Level.WARNING, "Exception verifying " + gameprofile.getName(), exception);
+                    // CraftBukkit end
                 }
 
             }
