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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Minecrell <minecrell@minecrell.net>
Date: Wed, 11 Oct 2017 15:55:38 +0200
Subject: [PATCH] Add extended PaperServerListPingEvent
Add a new event that extends the original ServerListPingEvent
and allows full control of the response sent to the client.
diff --git a/src/main/java/com/destroystokyo/paper/event/server/PaperServerListPingEvent.java b/src/main/java/com/destroystokyo/paper/event/server/PaperServerListPingEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..acff2ff570f8419ffa4dfefe890795c63d75325d
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/server/PaperServerListPingEvent.java
@@ -0,0 +1,502 @@
+package com.destroystokyo.paper.event.server;
+
+import static java.util.Objects.requireNonNull;
+
+import com.destroystokyo.paper.network.StatusClient;
+import com.destroystokyo.paper.profile.PlayerProfile;
+import com.destroystokyo.paper.profile.ProfileProperty;
+import com.google.common.base.Preconditions;
+import io.papermc.paper.util.TransformingRandomAccessList;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.server.ServerListPingEvent;
+import org.bukkit.profile.PlayerTextures;
+import org.bukkit.util.CachedServerIcon;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.UUID;
+
+import org.jetbrains.annotations.ApiStatus;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Extended version of {@link ServerListPingEvent} that allows full control
+ * of the response sent to the client.
+ */
+public class PaperServerListPingEvent extends ServerListPingEvent implements Cancellable {
+
+ @NotNull private final StatusClient client;
+
+ private int numPlayers;
+ private boolean hidePlayers;
+ @NotNull private final List<ListedPlayerInfo> listedPlayers = new ArrayList<>();
+ @NotNull private final TransformingRandomAccessList<ListedPlayerInfo, PlayerProfile> playerSample = new TransformingRandomAccessList<>(
+ listedPlayers,
+ info -> new UncheckedPlayerProfile(info.name(), info.id()),
+ profile -> new ListedPlayerInfo(profile.getName(), profile.getId())
+ );
+
+ @NotNull private String version;
+ private int protocolVersion;
+
+ @Nullable private CachedServerIcon favicon;
+
+ private boolean cancelled;
+
+ private boolean originalPlayerCount = true;
+ private Object[] players;
+
+ @Deprecated
+ @ApiStatus.Internal
+ public PaperServerListPingEvent(@NotNull StatusClient client, @NotNull String motd, int numPlayers, int maxPlayers,
+ @NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon) {
+ super("", client.getAddress().getAddress(), motd, numPlayers, maxPlayers);
+ this.client = client;
+ this.numPlayers = numPlayers;
+ this.version = version;
+ this.protocolVersion = protocolVersion;
+ setServerIcon(favicon);
+ }
+
+ @ApiStatus.Internal
+ public PaperServerListPingEvent(@NotNull StatusClient client, @NotNull net.kyori.adventure.text.Component motd, int numPlayers, int maxPlayers,
+ @NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon) {
+ super("", client.getAddress().getAddress(), motd, numPlayers, maxPlayers);
+ this.client = client;
+ this.numPlayers = numPlayers;
+ this.version = version;
+ this.protocolVersion = protocolVersion;
+ setServerIcon(favicon);
+ }
+
+ /**
+ * Returns the {@link StatusClient} pinging the server.
+ *
+ * @return The client
+ */
+ @NotNull
+ public StatusClient getClient() {
+ return this.client;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * <p>Returns {@code -1} if players are hidden using
+ * {@link #shouldHidePlayers()}.</p>
+ */
+ @Override
+ public int getNumPlayers() {
+ if (this.hidePlayers) {
+ return -1;
+ }
+
+ return this.numPlayers;
+ }
+
+ /**
+ * Sets the number of players displayed in the server list.
+ * <p>
+ * Note that this won't have any effect if {@link #shouldHidePlayers()}
+ * is enabled.
+ *
+ * @param numPlayers The number of online players
+ */
+ public void setNumPlayers(int numPlayers) {
+ if (this.numPlayers != numPlayers) {
+ this.numPlayers = numPlayers;
+ this.originalPlayerCount = false;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Returns {@code -1} if players are hidden using
+ * {@link #shouldHidePlayers()}.
+ */
+ @Override
+ public int getMaxPlayers() {
+ if (this.hidePlayers) {
+ return -1;
+ }
+
+ return super.getMaxPlayers();
+ }
+
+ /**
+ * Returns whether all player related information is hidden in the server
+ * list. This will cause {@link #getNumPlayers()}, {@link #getMaxPlayers()}
+ * and {@link #getPlayerSample()} to be skipped in the response.
+ * <p>
+ * The Vanilla Minecraft client will display the player count as {@code ???}
+ * when this option is enabled.
+ *
+ * @return {@code true} if the player count is hidden
+ */
+ public boolean shouldHidePlayers() {
+ return this.hidePlayers;
+ }
+
+ /**
+ * Sets whether all player related information is hidden in the server
+ * list. This will cause {@link #getNumPlayers()}, {@link #getMaxPlayers()}
+ * and {@link #getPlayerSample()} to be skipped in the response.
+ * <p>
+ * The Vanilla Minecraft client will display the player count as {@code ???}
+ * when this option is enabled.
+ *
+ * @param hidePlayers {@code true} if the player count should be hidden
+ */
+ public void setHidePlayers(boolean hidePlayers) {
+ this.hidePlayers = hidePlayers;
+ }
+
+ /**
+ * Returns a mutable list of {@link ListedPlayerInfo} that will be displayed
+ * as online players on the client.
+ * <p>
+ * The Vanilla Minecraft client will display them when hovering the
+ * player count with the mouse.
+ *
+ * @return The mutable player sample list
+ */
+ @NotNull
+ public List<ListedPlayerInfo> getListedPlayers() {
+ return this.listedPlayers;
+ }
+
+ /**
+ * Returns a mutable list of {@link PlayerProfile} that will be displayed
+ * as online players on the client.
+ * <p>
+ * The Vanilla Minecraft client will display them when hovering the
+ * player count with the mouse.
+ *
+ * @return The mutable player sample list
+ * @deprecated Use {@link #getListedPlayers()}, as this does not contain real player profiles
+ */
+ @NotNull
+ @Deprecated(forRemoval = true, since = "1.20.6")
+ public List<PlayerProfile> getPlayerSample() {
+ return this.playerSample;
+ }
+
+ /**
+ * Returns the version that will be sent as server version on the client.
+ *
+ * @return The server version
+ */
+ @NotNull
+ public String getVersion() {
+ return this.version;
+ }
+
+ /**
+ * Sets the version that will be sent as server version to the client.
+ *
+ * @param version The server version
+ */
+ public void setVersion(@NotNull String version) {
+ this.version = requireNonNull(version, "version");
+ }
+
+ /**
+ * Returns the protocol version that will be sent as the protocol version
+ * of the server to the client.
+ *
+ * @return The protocol version of the server, or {@code -1} if the server
+ * has not finished initialization yet
+ */
+ public int getProtocolVersion() {
+ return this.protocolVersion;
+ }
+
+ /**
+ * Sets the protocol version that will be sent as the protocol version
+ * of the server to the client.
+ *
+ * @param protocolVersion The protocol version of the server
+ */
+ public void setProtocolVersion(int protocolVersion) {
+ this.protocolVersion = protocolVersion;
+ }
+
+ /**
+ * Gets the server icon sent to the client.
+ *
+ * @return The icon to send to the client, or {@code null} for none
+ */
+ @Nullable
+ public CachedServerIcon getServerIcon() {
+ return this.favicon;
+ }
+
+ /**
+ * Sets the server icon sent to the client.
+ *
+ * @param icon The icon to send to the client, or {@code null} for none
+ */
+ @Override
+ public void setServerIcon(@Nullable CachedServerIcon icon) {
+ if (icon != null && icon.isEmpty()) {
+ // Represent empty icons as null
+ icon = null;
+ }
+
+ this.favicon = icon;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Cancelling this event will cause the connection to be closed immediately,
+ * without sending a response to the client.
+ */
+ @Override
+ public boolean isCancelled() {
+ return this.cancelled;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Cancelling this event will cause the connection to be closed immediately,
+ * without sending a response to the client.
+ */
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <b>Note:</b> For compatibility reasons, this method will return all
+ * online players, not just the ones referenced in {@link #getPlayerSample()}.
+ * Removing a player will:
+ *
+ * <ul>
+ * <li>Decrement the online player count (if and only if) the player
+ * count wasn't changed by another plugin before.</li>
+ * <li>Remove all entries from {@link #getPlayerSample()} that refer to
+ * the removed player (based on their {@link UUID}).</li>
+ * </ul>
+ * @deprecated the Iterable interface will be removed at some point
+ */
+ @NotNull
+ @Override
+ @Deprecated(forRemoval = true, since = "1.20.6")
+ public Iterator<Player> iterator() {
+ if (this.players == null) {
+ this.players = getOnlinePlayers();
+ }
+
+ return new PlayerIterator();
+ }
+
+ @NotNull
+ protected Object[] getOnlinePlayers() {
+ return Bukkit.getOnlinePlayers().toArray();
+ }
+
+ @NotNull
+ protected Player getBukkitPlayer(@NotNull Object player) {
+ return (Player) player;
+ }
+
+ @ApiStatus.Internal
+ private final class PlayerIterator implements Iterator<Player> {
+
+ private int next;
+ private int current;
+ @Nullable private Player player;
+
+ @Override
+ public boolean hasNext() {
+ for (; this.next < players.length; this.next++) {
+ if (players[this.next] != null) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ @NotNull
+ @Override
+ public Player next() {
+ if (!hasNext()) {
+ this.player = null;
+ throw new NoSuchElementException();
+ }
+
+ this.current = this.next++;
+ return this.player = getBukkitPlayer(players[this.current]);
+ }
+
+ @Override
+ public void remove() {
+ if (this.player == null) {
+ throw new IllegalStateException();
+ }
+
+ UUID uniqueId = this.player.getUniqueId();
+ this.player = null;
+
+ // Remove player from iterator
+ players[this.current] = null;
+
+ // Remove player from sample
+ getPlayerSample().removeIf(p -> uniqueId.equals(p.getId()));
+
+ // Decrement player count
+ if (originalPlayerCount) {
+ numPlayers--;
+ }
+ }
+ }
+
+ /**
+ * Represents a player that will be displayed in the player sample of the server list.
+ *
+ * @param name name of the listed player
+ * @param id UUID of the listed player
+ */
+ public record ListedPlayerInfo(@NotNull String name, @NotNull UUID id) {
+ }
+
+ @ApiStatus.Internal
+ private static final class UncheckedPlayerProfile implements PlayerProfile {
+ private String name;
+ private UUID uuid;
+
+ public UncheckedPlayerProfile(final @NotNull String name, final @NotNull UUID uuid) {
+ Preconditions.checkNotNull(name, "name cannot be null");
+ Preconditions.checkNotNull(uuid, "uuid cannot be null");
+ this.name = name;
+ this.uuid = uuid;
+ }
+
+ @Override
+ public @Nullable UUID getUniqueId() {
+ return uuid;
+ }
+
+ @Override
+ public @Nullable String getName() {
+ return name;
+ }
+
+ @Override
+ public @NotNull String setName(@Nullable final String name) {
+ Preconditions.checkNotNull(name, "name cannot be null");
+ return this.name = name;
+ }
+
+ @Override
+ public @Nullable UUID getId() {
+ return uuid;
+ }
+
+ @Override
+ public @Nullable UUID setId(@Nullable final UUID uuid) {
+ Preconditions.checkNotNull(uuid, "uuid cannot be null");
+ return this.uuid = uuid;
+ }
+
+ @Override
+ public @NotNull PlayerTextures getTextures() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setTextures(@Nullable final PlayerTextures textures) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull Set<ProfileProperty> getProperties() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean hasProperty(@Nullable final String property) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setProperty(@NotNull final ProfileProperty property) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void setProperties(@NotNull final Collection<ProfileProperty> properties) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeProperty(@Nullable final String property) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void clearProperties() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean isComplete() {
+ return false;
+ }
+
+ @Override
+ public boolean completeFromCache() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean completeFromCache(final boolean onlineMode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean completeFromCache(final boolean lookupUUID, final boolean onlineMode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean complete(final boolean textures) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean complete(final boolean textures, final boolean onlineMode) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull CompletableFuture<PlayerProfile> update() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public org.bukkit.profile.@NotNull PlayerProfile clone() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public @NotNull Map<String, Object> serialize() {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
diff --git a/src/main/java/com/destroystokyo/paper/network/StatusClient.java b/src/main/java/com/destroystokyo/paper/network/StatusClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..517d15238ed117f38bbd39f570874014cecf7bb5
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/StatusClient.java
@@ -0,0 +1,13 @@
+package com.destroystokyo.paper.network;
+
+import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
+
+/**
+ * Represents a client requesting the current status from the server (e.g. from
+ * the server list).
+ *
+ * @see PaperServerListPingEvent
+ */
+public interface StatusClient extends NetworkClient {
+
+}
diff --git a/src/main/java/org/bukkit/event/server/ServerListPingEvent.java b/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
index 72ebc29db42d08d1d0361dba462fc8a573fbf918..d351f62971f8f1317b3fc92f8b1d446e48149c3a 100644
--- a/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
+++ b/src/main/java/org/bukkit/event/server/ServerListPingEvent.java
@@ -248,9 +248,11 @@ public class ServerListPingEvent extends ServerEvent implements Iterable<Player>
*
* @throws UnsupportedOperationException if the caller of this event does
* not support removing players
+ * @deprecated the Iterable interface will be removed at some point
*/
@NotNull
@Override
+ @Deprecated(forRemoval = true, since = "1.20.6")
public Iterator<Player> iterator() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
diff --git a/src/main/java/org/bukkit/util/CachedServerIcon.java b/src/main/java/org/bukkit/util/CachedServerIcon.java
index 9a7768d41270714d4a1c89b4dcb436cc66f57545..b74b21a1ac7798e847b6d34ff45026e1c9cfed14 100644
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java
+++ b/src/main/java/org/bukkit/util/CachedServerIcon.java
@@ -18,4 +18,9 @@ public interface CachedServerIcon {
@Nullable
public String getData(); // Paper
+ // Paper start
+ default boolean isEmpty() {
+ return getData() == null;
+ }
+ // Paper end
}
|