aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0180-Implement-extended-PaperServerListPingEvent.patch
blob: c91d2d9333b306ffd757da424c6a2cda2be92726 (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
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..5d7110a33957a597592cacb864c947eb053e8563
--- /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.ServerPlayer;
+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.server.motd(), server.getPlayerCount(), server.getMaxPlayers(),
+                server.getServerModName() + ' ' + server.getServerVersion(), protocolVersion, icon);
+        this.server = server;
+    }
+
+    @Override
+    protected final Object[] getOnlinePlayers() {
+        return this.server.getPlayerList().players.toArray();
+    }
+
+    @Override
+    protected final Player getBukkitPlayer(Object player) {
+        return ((ServerPlayer) 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..d926ad804355ee2fdc5910b2505e8671602acdab
--- /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.Connection;
+
+class PaperStatusClient extends PaperNetworkClient implements StatusClient {
+
+    PaperStatusClient(Connection 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..6b0bdc266109cdfb874f08bf74323603921d2260
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/StandardPaperServerListPingEventImpl.java
@@ -0,0 +1,116 @@
+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.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+import javax.annotation.Nonnull;
+import net.minecraft.network.Connection;
+import net.minecraft.network.chat.Component;
+import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket;
+import net.minecraft.network.protocol.status.ServerStatus;
+import net.minecraft.server.MinecraftServer;
+import org.bukkit.craftbukkit.util.CraftIconCache;
+
+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 List<GameProfile> originalSample;
+
+    private StandardPaperServerListPingEventImpl(MinecraftServer server, Connection networkManager, ServerStatus ping) {
+        super(server, new PaperStatusClient(networkManager), ping.version().map(ServerStatus.Version::protocol).orElse(-1), server.server.getServerIcon());
+        this.originalSample = ping.players().map(ServerStatus.Players::sample).orElse(null); // 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 List<GameProfile> getPlayerSampleHandle() {
+        if (this.originalSample != null) {
+            return this.originalSample;
+        }
+
+        List<PlayerProfile> entries = super.getPlayerSample();
+        if (entries.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        final List<GameProfile> profiles = new ArrayList<>();
+        for (PlayerProfile profile : entries) {
+            /*
+             * 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.
+             */
+            if (profile.getId() != null && profile.getName() != null) {
+                profiles.add(CraftPlayerProfile.asAuthlib(profile));
+            } else {
+                profiles.add(new GameProfile(MoreObjects.firstNonNull(profile.getId(), FAKE_UUID), Strings.nullToEmpty(profile.getName())));
+            }
+        }
+        return profiles;
+    }
+
+    public static void processRequest(MinecraftServer server, Connection networkManager) {
+        StandardPaperServerListPingEventImpl event = new StandardPaperServerListPingEventImpl(server, networkManager, server.getStatus());
+        server.server.getPluginManager().callEvent(event);
+
+        // Close connection immediately if event is cancelled
+        if (event.isCancelled()) {
+            networkManager.disconnect(null);
+            return;
+        }
+
+        // Setup response
+
+        // Description
+        final Component description = new AdventureComponent(event.motd());
+
+        // Players
+        final Optional<ServerStatus.Players> players;
+        if (!event.shouldHidePlayers()) {
+            players = Optional.of(new ServerStatus.Players(event.getMaxPlayers(), event.getNumPlayers(), event.getPlayerSampleHandle()));
+        } else {
+            players = Optional.empty();
+        }
+
+        // Version
+        final ServerStatus.Version version = new ServerStatus.Version(event.getVersion(), event.getProtocolVersion());
+
+        // Favicon
+        final Optional<ServerStatus.Favicon> favicon;
+        if (event.getServerIcon() != null) {
+            favicon = Optional.of(new ServerStatus.Favicon(((CraftIconCache) event.getServerIcon()).value));
+        } else {
+            favicon = Optional.empty();
+        }
+        final ServerStatus ping = new ServerStatus(description, players, Optional.of(version), favicon, server.enforceSecureProfile());
+
+        // Send response
+        networkManager.send(new ClientboundStatusResponsePacket(ping));
+    }
+
+}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index ee27251b9decc00a0094611cf79644bd83bce5c7..52f5c91d71a8fc86d14c3c4c9e258b9cf70ad57b 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -3,6 +3,9 @@ package net.minecraft.server;
 import com.google.common.base.Preconditions;
 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;
@@ -1328,7 +1331,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
         if (this.hidesOnlinePlayers()) {
             return new ServerStatus.Players(i, list.size(), List.of());
         } else {
-            int j = Math.min(list.size(), 12);
+            int j = Math.min(list.size(), org.spigotmc.SpigotConfig.playerSample); // Paper
             ObjectArrayList<GameProfile> objectarraylist = new ObjectArrayList(j);
             int k = Mth.nextInt(this.random, 0, list.size() - j);
 
diff --git a/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
index d99b2235038eb1aba8cda474c4aa51e207149ef4..2c13147bc063a09bb7907d6f90c3a1e811a09eb1 100644
--- a/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
@@ -48,6 +48,8 @@ public class ServerStatusPacketListenerImpl implements ServerStatusPacketListene
             this.connection.disconnect(ServerStatusPacketListenerImpl.DISCONNECT_REASON);
         } else {
             this.hasRequestedStatus = true;
+            // Paper start - Replace everything
+            /*
             // CraftBukkit start
             // this.connection.send(new PacketStatusOutServerInfo(this.status));
             MinecraftServer server = MinecraftServer.getServer();
@@ -150,6 +152,9 @@ public class ServerStatusPacketListenerImpl implements ServerStatusPacketListene
 
             this.connection.send(new ClientboundStatusResponsePacket(ping));
             // CraftBukkit end
+            */
+            com.destroystokyo.paper.network.StandardPaperServerListPingEventImpl.processRequest(MinecraftServer.getServer(), this.connection);
+            // Paper end
         }
     }
 
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
index 489ce6f439778b26eb33ede9432681d4bf9e0116..ddc5f2d9aa7dda6aff132392927e3d7e3674dbff 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()
     {
-        SpigotConfig.playerSample = SpigotConfig.getInt( "settings.sample-count", 12 );
+        SpigotConfig.playerSample = Math.max( SpigotConfig.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
     }