summaryrefslogtreecommitdiffstats
path: root/nms-patches/LoginListener.patch
blob: 789331f05a9281980afc70faa683e7fe67c8f1c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--- a/net/minecraft/server/LoginListener.java
+++ b/net/minecraft/server/LoginListener.java
@@ -22,6 +22,12 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+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, ITickable {
 
     private static final AtomicInteger b = new AtomicInteger(0);
@@ -36,6 +42,7 @@
     private final String j;
     private SecretKey loginKey;
     private EntityPlayer l;
+    public String hostname = ""; // CraftBukkit - add field
 
     public LoginListener(MinecraftServer minecraftserver, NetworkManager networkmanager) {
         this.g = LoginListener.EnumProtocolState.HELLO;
@@ -64,6 +71,20 @@
 
     }
 
+    // CraftBukkit start
+    @Deprecated
+    public void disconnect(String s) {
+        try {
+            IChatBaseComponent ichatbasecomponent = new ChatComponentText(s);
+            LoginListener.c.info("Disconnecting {}: {}", this.c(), s);
+            this.networkManager.sendPacket(new PacketLoginOutDisconnect(ichatbasecomponent));
+            this.networkManager.close(ichatbasecomponent);
+        } catch (Exception exception) {
+            LoginListener.c.error("Error whilst disconnecting player", exception);
+        }
+    }
+    // CraftBukkit end
+
     public void disconnect(IChatBaseComponent ichatbasecomponent) {
         try {
             LoginListener.c.info("Disconnecting {}: {}", this.c(), ichatbasecomponent.toPlainText());
@@ -80,10 +101,12 @@
             this.i = this.a(this.i);
         }
 
-        String s = this.server.getPlayerList().attemptLogin(this.networkManager.getSocketAddress(), this.i);
+        // CraftBukkit start - fire PlayerLoginEvent
+        EntityPlayer s = this.server.getPlayerList().attemptLogin(this, this.i, hostname);
 
-        if (s != null) {
-            this.disconnect(new ChatMessage(s, new Object[0]));
+        if (s == null) {
+            // this.disconnect(new ChatMessage(s, new Object[0]));
+            // CraftBukkit end
         } else {
             this.g = LoginListener.EnumProtocolState.ACCEPTED;
             if (this.server.aG() >= 0 && !this.networkManager.isLocal()) {
@@ -92,7 +115,7 @@
                         LoginListener.this.networkManager.setCompressionLevel(LoginListener.this.server.aG());
                     }
 
-                    public void operationComplete(Future future) throws Exception {
+                    public void operationComplete(ChannelFuture future) throws Exception { // CraftBukkit - fix decompile error
                         this.a((ChannelFuture) future);
                     }
                 }, new GenericFutureListener[0]);
@@ -103,9 +126,9 @@
 
             if (entityplayer != null) {
                 this.g = LoginListener.EnumProtocolState.DELAY_ACCEPT;
-                this.l = this.server.getPlayerList().processLogin(this.i);
+                this.l = this.server.getPlayerList().processLogin(this.i, s); // CraftBukkit - add player reference
             } else {
-                this.server.getPlayerList().a(this.networkManager, this.server.getPlayerList().processLogin(this.i));
+                this.server.getPlayerList().a(this.networkManager, this.server.getPlayerList().processLogin(this.i, s)); // CraftBukkit - add player reference
             }
         }
 
@@ -150,6 +173,43 @@
 
                         LoginListener.this.i = LoginListener.this.server.az().hasJoinedServer(new GameProfile((UUID) null, gameprofile.getName()), s, this.a());
                         if (LoginListener.this.i != null) {
+                            // CraftBukkit start - fire PlayerPreLoginEvent
+                            if (!networkManager.isConnected()) {
+                                return;
+                            }
+
+                            String playerName = i.getName();
+                            java.net.InetAddress address = ((java.net.InetSocketAddress) networkManager.getSocketAddress()).getAddress();
+                            java.util.UUID uniqueId = i.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.c.info("UUID of player {} is {}", LoginListener.this.i.getName(), LoginListener.this.i.getId());
                             LoginListener.this.g = LoginListener.EnumProtocolState.READY_TO_ACCEPT;
                         } else if (LoginListener.this.server.R()) {
@@ -169,6 +229,11 @@
                             LoginListener.this.disconnect(new ChatMessage("multiplayer.disconnect.authservers_down", new Object[0]));
                             LoginListener.c.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
                     }
 
                 }