aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0197-Implement-extended-PaperServerListPingEvent.patch
blob: 6ad25474c6d63ff47cc4397e82147a3135ff8a21 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Minecrell <minecrell@minecrell.net>
Date: Wed, 11 Oct 2017 15:56:26 +0200
Subject: [PATCH] Implement extended PaperServerListPingEvent


diff --git a/src/main/java/com/destroystokyo/paper/network/PaperServerListPingEventImpl.java b/src/main/java/com/destroystokyo/paper/network/PaperServerListPingEventImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..e7f1efd0466a5d7bb9584ffbd6fbac1ecc6153a5
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/PaperServerListPingEventImpl.java
@@ -0,0 +1,31 @@
+package com.destroystokyo.paper.network;
+
+import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.level.EntityPlayer;
+import org.bukkit.entity.Player;
+import org.bukkit.util.CachedServerIcon;
+
+import javax.annotation.Nullable;
+
+class PaperServerListPingEventImpl extends PaperServerListPingEvent {
+
+    private final MinecraftServer server;
+
+    PaperServerListPingEventImpl(MinecraftServer server, StatusClient client, int protocolVersion, @Nullable CachedServerIcon icon) {
+        super(client, server.getMotd(), server.getPlayerCount(), server.getMaxPlayers(),
+                server.getServerModName() + ' ' + server.getVersion(), protocolVersion, icon);
+        this.server = server;
+    }
+
+    @Override
+    protected final Object[] getOnlinePlayers() {
+        return this.server.getPlayerList().players.toArray();
+    }
+
+    @Override
+    protected final Player getBukkitPlayer(Object player) {
+        return ((EntityPlayer) player).getBukkitEntity();
+    }
+
+}
diff --git a/src/main/java/com/destroystokyo/paper/network/PaperStatusClient.java b/src/main/java/com/destroystokyo/paper/network/PaperStatusClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..46e84ac6ba5d32d030267fb0c991c281a673c716
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/PaperStatusClient.java
@@ -0,0 +1,11 @@
+package com.destroystokyo.paper.network;
+
+import net.minecraft.network.NetworkManager;
+
+class PaperStatusClient extends PaperNetworkClient implements StatusClient {
+
+    PaperStatusClient(NetworkManager networkManager) {
+        super(networkManager);
+    }
+
+}
diff --git a/src/main/java/com/destroystokyo/paper/network/StandardPaperServerListPingEventImpl.java b/src/main/java/com/destroystokyo/paper/network/StandardPaperServerListPingEventImpl.java
new file mode 100644
index 0000000000000000000000000000000000000000..174326871df9b61beec51ef6a1e5f26932404b6a
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/StandardPaperServerListPingEventImpl.java
@@ -0,0 +1,110 @@
+package com.destroystokyo.paper.network;
+
+import com.destroystokyo.paper.profile.CraftPlayerProfile;
+import com.destroystokyo.paper.profile.PlayerProfile;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Strings;
+import com.mojang.authlib.GameProfile;
+import io.papermc.paper.adventure.AdventureComponent;
+import java.util.List;
+import java.util.UUID;
+import javax.annotation.Nonnull;
+import net.minecraft.network.NetworkManager;
+import net.minecraft.network.protocol.status.PacketStatusOutServerInfo;
+import net.minecraft.network.protocol.status.ServerPing;
+import net.minecraft.server.MinecraftServer;
+
+public final class StandardPaperServerListPingEventImpl extends PaperServerListPingEventImpl {
+
+    private static final GameProfile[] EMPTY_PROFILES = new GameProfile[0];
+    private static final UUID FAKE_UUID = new UUID(0, 0);
+
+    private GameProfile[] originalSample;
+
+    private StandardPaperServerListPingEventImpl(MinecraftServer server, NetworkManager networkManager, ServerPing ping) {
+        super(server, new PaperStatusClient(networkManager), ping.getServerData() != null ? ping.getServerData().getProtocolVersion() : -1, server.server.getServerIcon());
+        this.originalSample = ping.getPlayers() == null ? null : ping.getPlayers().getSample(); // GH-1473 - pre-tick race condition NPE
+    }
+
+    @Nonnull
+    @Override
+    public List<PlayerProfile> getPlayerSample() {
+        List<PlayerProfile> sample = super.getPlayerSample();
+
+        if (this.originalSample != null) {
+            for (GameProfile profile : this.originalSample) {
+                sample.add(CraftPlayerProfile.asBukkitCopy(profile));
+            }
+            this.originalSample = null;
+        }
+
+        return sample;
+    }
+
+    private GameProfile[] getPlayerSampleHandle() {
+        if (this.originalSample != null) {
+            return this.originalSample;
+        }
+
+        List<PlayerProfile> entries = super.getPlayerSample();
+        if (entries.isEmpty()) {
+            return EMPTY_PROFILES;
+        }
+
+        GameProfile[] profiles = new GameProfile[entries.size()];
+        for (int i = 0; i < profiles.length; i++) {
+            /*
+             * Avoid null UUIDs/names since that will make the response invalid
+             * on the client.
+             * Instead, fall back to a fake/empty UUID and an empty string as name.
+             * This can be used to create custom lines in the player list that do not
+             * refer to a specific player.
+             */
+
+            PlayerProfile profile = entries.get(i);
+            if (profile.getId() != null && profile.getName() != null) {
+                profiles[i] = CraftPlayerProfile.asAuthlib(profile);
+            } else {
+                profiles[i] = new GameProfile(MoreObjects.firstNonNull(profile.getId(), FAKE_UUID), Strings.nullToEmpty(profile.getName()));
+            }
+        }
+
+        return profiles;
+    }
+
+    @SuppressWarnings("deprecation")
+    public static void processRequest(MinecraftServer server, NetworkManager networkManager) {
+        StandardPaperServerListPingEventImpl event = new StandardPaperServerListPingEventImpl(server, networkManager, server.getServerPing());
+        server.server.getPluginManager().callEvent(event);
+
+        // Close connection immediately if event is cancelled
+        if (event.isCancelled()) {
+            networkManager.close(null);
+            return;
+        }
+
+        // Setup response
+        ServerPing ping = new ServerPing();
+
+        // Description
+        ping.setMOTD(new AdventureComponent(event.motd()));
+
+        // Players
+        if (!event.shouldHidePlayers()) {
+            ping.setPlayerSample(new ServerPing.ServerPingPlayerSample(event.getMaxPlayers(), event.getNumPlayers()));
+            ping.getPlayers().setSample(event.getPlayerSampleHandle());
+        }
+
+        // Version
+        ping.setServerInfo(new ServerPing.ServerData(event.getVersion(), event.getProtocolVersion()));
+
+        // Favicon
+        if (event.getServerIcon() != null) {
+            ping.setFavicon(event.getServerIcon().getData());
+        }
+
+        // Send response
+        networkManager.sendPacket(new PacketStatusOutServerInfo(ping));
+    }
+
+}
diff --git a/src/main/java/net/minecraft/network/protocol/status/PacketStatusOutServerInfo.java b/src/main/java/net/minecraft/network/protocol/status/PacketStatusOutServerInfo.java
index 0ebeacaaeb265d202f52c758566a5160c42e8a55..cb37805c8cc5a064391f338c6359df518f6db39a 100644
--- a/src/main/java/net/minecraft/network/protocol/status/PacketStatusOutServerInfo.java
+++ b/src/main/java/net/minecraft/network/protocol/status/PacketStatusOutServerInfo.java
@@ -2,6 +2,7 @@ package net.minecraft.network.protocol.status;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
+import io.papermc.paper.adventure.AdventureComponent; // Paper
 import java.io.IOException;
 import net.minecraft.network.PacketDataSerializer;
 import net.minecraft.network.chat.ChatModifier;
@@ -12,7 +13,9 @@ import net.minecraft.util.ChatTypeAdapterFactory;
 
 public class PacketStatusOutServerInfo implements Packet<PacketStatusOutListener> {
 
-    private static final Gson a = (new GsonBuilder()).registerTypeAdapter(ServerPing.ServerData.class, new ServerPing.ServerData.Serializer()).registerTypeAdapter(ServerPing.ServerPingPlayerSample.class, new ServerPing.ServerPingPlayerSample.Serializer()).registerTypeAdapter(ServerPing.class, new ServerPing.Serializer()).registerTypeHierarchyAdapter(IChatBaseComponent.class, new IChatBaseComponent.ChatSerializer()).registerTypeHierarchyAdapter(ChatModifier.class, new ChatModifier.ChatModifierSerializer()).registerTypeAdapterFactory(new ChatTypeAdapterFactory()).create();
+    private static final Gson a = (new GsonBuilder()).registerTypeAdapter(ServerPing.ServerData.class, new ServerPing.ServerData.Serializer()).registerTypeAdapter(ServerPing.ServerPingPlayerSample.class, new ServerPing.ServerPingPlayerSample.Serializer()).registerTypeAdapter(ServerPing.class, new ServerPing.Serializer()).registerTypeHierarchyAdapter(IChatBaseComponent.class, new IChatBaseComponent.ChatSerializer()).registerTypeHierarchyAdapter(ChatModifier.class, new ChatModifier.ChatModifierSerializer()).registerTypeAdapterFactory(new ChatTypeAdapterFactory())
+        .registerTypeAdapter(AdventureComponent.class, new AdventureComponent.Serializer())
+        .create();
     private ServerPing b;
 
     public PacketStatusOutServerInfo() {}
diff --git a/src/main/java/net/minecraft/network/protocol/status/ServerPing.java b/src/main/java/net/minecraft/network/protocol/status/ServerPing.java
index 005ae7a75dfb19152abb606da29acad07c85e499..b9e36a83837913cd3e5abe598f695ba7a9ffc417 100644
--- a/src/main/java/net/minecraft/network/protocol/status/ServerPing.java
+++ b/src/main/java/net/minecraft/network/protocol/status/ServerPing.java
@@ -31,6 +31,7 @@ public class ServerPing {
         this.a = ichatbasecomponent;
     }
 
+    public ServerPingPlayerSample getPlayers() { return b(); } // Paper - OBFHELPER
     public ServerPing.ServerPingPlayerSample b() {
         return this.b;
     }
@@ -162,10 +163,12 @@ public class ServerPing {
             return this.b;
         }
 
+        public GameProfile[] getSample() { return c(); } // Paper - OBFHELPER
         public GameProfile[] c() {
             return this.c;
         }
 
+        public void setSample(GameProfile[] sample) { a(sample); } // Paper - OBFHELPER
         public void a(GameProfile[] agameprofile) {
             this.c = agameprofile;
         }
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 62ee4708a3196cfa395317a6312b3ac6c036793a..dbf5a849358158324e8a5c87f831236b71f7ec0d 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -2,6 +2,9 @@ package net.minecraft.server;
 
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
+import co.aikar.timings.Timings;
+import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
+import com.google.common.base.Stopwatch;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
@@ -1239,7 +1242,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
         if (i - this.T >= 5000000000L) {
             this.T = i;
             this.serverPing.setPlayerSample(new ServerPing.ServerPingPlayerSample(this.getMaxPlayers(), this.getPlayerCount()));
-            GameProfile[] agameprofile = new GameProfile[Math.min(this.getPlayerCount(), 12)];
+            GameProfile[] agameprofile = new GameProfile[Math.min(this.getPlayerCount(), org.spigotmc.SpigotConfig.playerSample)]; // Paper
             int j = MathHelper.nextInt(this.r, 0, this.getPlayerCount() - agameprofile.length);
 
             for (int k = 0; k < agameprofile.length; ++k) {
diff --git a/src/main/java/net/minecraft/server/network/PacketStatusListener.java b/src/main/java/net/minecraft/server/network/PacketStatusListener.java
index d219eda271a71f786808a6958b829fca40a1aaba..e1997563984540e6edf5d3b697d029dc5f3c40e1 100644
--- a/src/main/java/net/minecraft/server/network/PacketStatusListener.java
+++ b/src/main/java/net/minecraft/server/network/PacketStatusListener.java
@@ -48,6 +48,8 @@ public class PacketStatusListener implements PacketStatusInListener {
             this.networkManager.close(PacketStatusListener.a);
         } else {
             this.d = true;
+            // Paper start - Replace everything
+            /*
             // CraftBukkit start
             // this.networkManager.sendPacket(new PacketStatusOutServerInfo(this.minecraftServer.getServerPing()));
             final Object[] players = minecraftServer.getPlayerList().players.toArray();
@@ -143,6 +145,9 @@ public class PacketStatusListener implements PacketStatusInListener {
             ping.setServerInfo(new ServerPing.ServerData(minecraftServer.getServerModName() + " " + minecraftServer.getVersion(), version));
 
             this.networkManager.sendPacket(new PacketStatusOutServerInfo(ping));
+            */
+            com.destroystokyo.paper.network.StandardPaperServerListPingEventImpl.processRequest(this.minecraftServer, this.networkManager);
+            // Paper end
         }
         // CraftBukkit end
     }
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index e8e5e5b568ba53dd006f1461cb4f027ceeae5528..11f8a2e5bf43013bce8675ea310ff42eacf14754 100644
--- a/src/main/java/org/spigotmc/SpigotConfig.java
+++ b/src/main/java/org/spigotmc/SpigotConfig.java
@@ -289,7 +289,7 @@ public class SpigotConfig
     public static int playerSample;
     private static void playerSample()
     {
-        playerSample = getInt( "settings.sample-count", 12 );
+        playerSample = Math.max( getInt( "settings.sample-count", 12 ), 0 ); // Paper - Avoid negative counts
         Bukkit.getLogger().log( Level.INFO, "Server Ping Player Sample Count: {0}", playerSample ); // Paper - Use logger
     }