2020-05-02 00:03:47 +02:00
|
|
|
From 1f72e6ba6fe22b8a0d93ce86effc67d43320e59f Mon Sep 17 00:00:00 2001
|
2018-03-23 04:19:59 +01:00
|
|
|
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
|
2020-04-02 23:07:06 +02:00
|
|
|
index 000000000..0cc5dd573
|
2018-03-23 04:19:59 +01:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/server/PaperServerListPingEvent.java
|
2019-03-20 01:28:15 +01:00
|
|
|
@@ -0,0 +1,323 @@
|
2018-03-23 04:19:59 +01:00
|
|
|
+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 org.bukkit.Bukkit;
|
|
|
|
+import org.bukkit.entity.Player;
|
|
|
|
+import org.bukkit.event.Cancellable;
|
|
|
|
+import org.bukkit.event.server.ServerListPingEvent;
|
|
|
|
+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;
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
|
+import org.jetbrains.annotations.Nullable;
|
2018-03-23 04:19:59 +01:00
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Extended version of {@link ServerListPingEvent} that allows full control
|
|
|
|
+ * of the response sent to the client.
|
|
|
|
+ */
|
|
|
|
+public class PaperServerListPingEvent extends ServerListPingEvent implements Cancellable {
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull private final StatusClient client;
|
2018-03-23 04:19:59 +01:00
|
|
|
+
|
|
|
|
+ private int numPlayers;
|
|
|
|
+ private boolean hidePlayers;
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull private final List<PlayerProfile> playerSample = new ArrayList<>();
|
2018-03-23 04:19:59 +01:00
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull private String version;
|
2018-03-23 04:19:59 +01:00
|
|
|
+ private int protocolVersion;
|
|
|
|
+
|
|
|
|
+ @Nullable private CachedServerIcon favicon;
|
|
|
|
+
|
|
|
|
+ private boolean cancelled;
|
|
|
|
+
|
|
|
|
+ private boolean originalPlayerCount = true;
|
|
|
|
+ private Object[] players;
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ public PaperServerListPingEvent(@NotNull StatusClient client, @NotNull String motd, int numPlayers, int maxPlayers,
|
|
|
|
+ @NotNull String version, int protocolVersion, @Nullable CachedServerIcon favicon) {
|
2018-03-23 04:19:59 +01:00
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ 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.</p>
|
|
|
|
+ *
|
|
|
|
+ * @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()}.</p>
|
|
|
|
+ */
|
|
|
|
+ @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.</p>
|
|
|
|
+ *
|
|
|
|
+ * @return {@code true} if the player count is hidden
|
|
|
|
+ */
|
|
|
|
+ public boolean shouldHidePlayers() {
|
|
|
|
+ return 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.</p>
|
|
|
|
+ *
|
|
|
|
+ * @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 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.</p>
|
|
|
|
+ *
|
|
|
|
+ * @return The mutable player sample list
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ public List<PlayerProfile> getPlayerSample() {
|
|
|
|
+ return this.playerSample;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the version that will be sent as server version on the client.
|
|
|
|
+ *
|
|
|
|
+ * @return The server version
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ public String getVersion() {
|
|
|
|
+ return version;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Sets the version that will be sent as server version to the client.
|
|
|
|
+ *
|
|
|
|
+ * @param version The server version
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ public void setVersion(@NotNull String version) {
|
2018-03-23 04:19:59 +01:00
|
|
|
+ this.version = requireNonNull(version, "version");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the protocol version that will be sent as the protocol version
|
|
|
|
+ * of the server to the client.
|
|
|
|
+ *
|
2019-02-06 20:15:46 +01:00
|
|
|
+ * @return The protocol version of the server, or {@code -1} if the server
|
|
|
|
+ * has not finished initialization yet
|
2018-03-23 04:19:59 +01:00
|
|
|
+ */
|
|
|
|
+ public int getProtocolVersion() {
|
|
|
|
+ return 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.</p>
|
|
|
|
+ */
|
|
|
|
+ @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.</p>
|
|
|
|
+ */
|
|
|
|
+ @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:</p>
|
|
|
|
+ *
|
|
|
|
+ * <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>
|
|
|
|
+ */
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ @Override
|
|
|
|
+ public Iterator<Player> iterator() {
|
|
|
|
+ if (this.players == null) {
|
|
|
|
+ this.players = getOnlinePlayers();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return new PlayerIterator();
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ protected Object[] getOnlinePlayers() {
|
|
|
|
+ return Bukkit.getOnlinePlayers().toArray();
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
|
|
|
+ protected Player getBukkitPlayer(@NotNull Object player) {
|
2018-03-23 04:19:59 +01:00
|
|
|
+ return (Player) player;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 01:28:15 +01:00
|
|
|
+ @NotNull
|
2018-03-23 04:19:59 +01:00
|
|
|
+ @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--;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
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
|
2020-04-02 23:07:06 +02:00
|
|
|
index 000000000..517d15238
|
2018-03-23 04:19:59 +01:00
|
|
|
--- /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/util/CachedServerIcon.java b/src/main/java/org/bukkit/util/CachedServerIcon.java
|
2020-04-02 23:07:06 +02:00
|
|
|
index 612958a33..bb4f7702c 100644
|
2018-03-23 04:19:59 +01:00
|
|
|
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java
|
|
|
|
+++ b/src/main/java/org/bukkit/util/CachedServerIcon.java
|
2019-03-20 01:28:15 +01:00
|
|
|
@@ -18,4 +18,9 @@ public interface CachedServerIcon {
|
|
|
|
@Nullable
|
|
|
|
public String getData(); // Paper
|
|
|
|
|
2018-03-23 04:19:59 +01:00
|
|
|
+ // Paper start
|
|
|
|
+ default boolean isEmpty() {
|
|
|
|
+ return getData() == null;
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
}
|
|
|
|
--
|
2020-05-02 00:03:47 +02:00
|
|
|
2.26.2
|
2018-03-23 04:19:59 +01:00
|
|
|
|