Archiviert
13
0
Dieses Repository wurde am 2024-12-25 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
Paper-Old/patches/server/0175-Implement-extended-PaperServerListPingEvent.patch

242 Zeilen
11 KiB
Diff

2021-06-11 14:02:28 +02:00
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
2024-02-01 10:15:57 +01:00
index 0000000000000000000000000000000000000000..6ed2114f577ce12d2d493985e798609c7d83f15e
2021-06-11 14:02:28 +02:00
--- /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) {
2024-02-01 10:15:57 +01:00
+ super(client, server.motd(), server.getPlayerCount(), server.getMaxPlayers(),
2021-06-11 14:02:28 +02:00
+ 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
2024-06-14 15:16:29 +02:00
index 0000000000000000000000000000000000000000..69433be355f284030ca79cba509a351bb32ccd8b
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/network/StandardPaperServerListPingEventImpl.java
2023-03-15 00:10:18 +01:00
@@ -0,0 +1,116 @@
2021-06-11 14:02:28 +02:00
+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;
2023-03-15 00:10:18 +01:00
+import java.util.ArrayList;
+import java.util.Collections;
2021-06-11 14:02:28 +02:00
+import java.util.List;
2023-03-15 00:10:18 +01:00
+import java.util.Optional;
2021-06-11 14:02:28 +02:00
+import java.util.UUID;
+import javax.annotation.Nonnull;
+import net.minecraft.network.Connection;
2023-03-15 00:10:18 +01:00
+import net.minecraft.network.chat.Component;
2021-06-11 14:02:28 +02:00
+import net.minecraft.network.protocol.status.ClientboundStatusResponsePacket;
+import net.minecraft.network.protocol.status.ServerStatus;
+import net.minecraft.server.MinecraftServer;
2023-03-15 00:10:18 +01:00
+import org.bukkit.craftbukkit.util.CraftIconCache;
2021-06-11 14:02:28 +02:00
+
+public final class StandardPaperServerListPingEventImpl extends PaperServerListPingEventImpl {
+
2023-03-15 00:10:18 +01:00
+ // private static final GameProfile[] EMPTY_PROFILES = new GameProfile[0];
2021-06-11 14:02:28 +02:00
+ private static final UUID FAKE_UUID = new UUID(0, 0);
+
2023-03-15 00:10:18 +01:00
+ private List<GameProfile> originalSample;
2021-06-11 14:02:28 +02:00
+
+ private StandardPaperServerListPingEventImpl(MinecraftServer server, Connection networkManager, ServerStatus ping) {
2023-03-15 00:10:18 +01:00
+ 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
2021-06-11 14:02:28 +02:00
+ }
+
+ @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;
+ }
+
2023-03-15 00:10:18 +01:00
+ private List<GameProfile> getPlayerSampleHandle() {
2021-06-11 14:02:28 +02:00
+ if (this.originalSample != null) {
+ return this.originalSample;
+ }
+
+ List<PlayerProfile> entries = super.getPlayerSample();
+ if (entries.isEmpty()) {
2023-03-15 00:10:18 +01:00
+ return Collections.emptyList();
2021-06-11 14:02:28 +02:00
+ }
+
2023-03-15 00:10:18 +01:00
+ final List<GameProfile> profiles = new ArrayList<>();
+ for (PlayerProfile profile : entries) {
2021-06-11 14:02:28 +02:00
+ /*
+ * 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) {
2023-03-15 00:10:18 +01:00
+ profiles.add(CraftPlayerProfile.asAuthlib(profile));
2021-06-11 14:02:28 +02:00
+ } else {
2023-03-15 00:10:18 +01:00
+ profiles.add(new GameProfile(MoreObjects.firstNonNull(profile.getId(), FAKE_UUID), Strings.nullToEmpty(profile.getName())));
2021-06-11 14:02:28 +02:00
+ }
+ }
+ 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()) {
2024-06-14 15:16:29 +02:00
+ networkManager.disconnect((Component) null);
2021-06-11 14:02:28 +02:00
+ return;
+ }
+
+ // Setup response
+
+ // Description
2023-03-15 00:10:18 +01:00
+ final Component description = new AdventureComponent(event.motd());
2021-06-11 14:02:28 +02:00
+
+ // Players
2023-03-15 00:10:18 +01:00
+ final Optional<ServerStatus.Players> players;
2021-06-11 14:02:28 +02:00
+ if (!event.shouldHidePlayers()) {
2023-03-15 00:10:18 +01:00
+ players = Optional.of(new ServerStatus.Players(event.getMaxPlayers(), event.getNumPlayers(), event.getPlayerSampleHandle()));
+ } else {
+ players = Optional.empty();
2021-06-11 14:02:28 +02:00
+ }
+
+ // Version
2023-03-15 00:10:18 +01:00
+ final ServerStatus.Version version = new ServerStatus.Version(event.getVersion(), event.getProtocolVersion());
2021-06-11 14:02:28 +02:00
+
+ // Favicon
2023-03-15 00:10:18 +01:00
+ final Optional<ServerStatus.Favicon> favicon;
2021-06-11 14:02:28 +02:00
+ if (event.getServerIcon() != null) {
2023-03-15 00:10:18 +01:00
+ favicon = Optional.of(new ServerStatus.Favicon(((CraftIconCache) event.getServerIcon()).value));
+ } else {
+ favicon = Optional.empty();
2021-06-11 14:02:28 +02:00
+ }
2023-03-15 00:10:18 +01:00
+ final ServerStatus ping = new ServerStatus(description, players, Optional.of(version), favicon, server.enforceSecureProfile());
2021-06-11 14:02:28 +02:00
+
+ // 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
2024-06-13 20:09:28 +02:00
index 7335f9bb936eeb585ee077b0b9c461d7946d6134..1c024fb7f682c81c465f59f4ab5fbeac964d73e1 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2023-03-14 19:59:51 +01:00
@@ -3,6 +3,9 @@ package net.minecraft.server;
import com.google.common.base.Preconditions;
2021-06-11 14:02:28 +02:00
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;
2024-06-13 20:09:28 +02:00
@@ -1518,7 +1521,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2023-03-14 19:59:51 +01:00
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 - PaperServerListPingEvent
2023-03-14 19:59:51 +01:00
ObjectArrayList<GameProfile> objectarraylist = new ObjectArrayList(j);
int k = Mth.nextInt(this.random, 0, list.size() - j);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
2024-06-13 20:09:28 +02:00
index 71540687b4212702cdaaad5fd4815fb3eb97ddd6..385457c7151f7e636e1ea2e38ef983f4f532b9b5 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerStatusPacketListenerImpl.java
2024-06-13 20:09:28 +02:00
@@ -49,6 +49,8 @@ public class ServerStatusPacketListenerImpl implements ServerStatusPacketListene
2021-06-11 14:02:28 +02:00
this.connection.disconnect(ServerStatusPacketListenerImpl.DISCONNECT_REASON);
} else {
this.hasRequestedStatus = true;
+ // Paper start - Replace everything
+ /*
// CraftBukkit start
2023-03-14 19:59:51 +01:00
// this.connection.send(new PacketStatusOutServerInfo(this.status));
MinecraftServer server = MinecraftServer.getServer();
2024-06-13 20:09:28 +02:00
@@ -151,6 +153,9 @@ public class ServerStatusPacketListenerImpl implements ServerStatusPacketListene
2021-06-11 14:02:28 +02:00
2021-06-12 18:56:13 +02:00
this.connection.send(new ClientboundStatusResponsePacket(ping));
2021-11-23 15:03:50 +01:00
// CraftBukkit end
2021-06-11 14:02:28 +02:00
+ */
2023-03-15 00:10:18 +01:00
+ com.destroystokyo.paper.network.StandardPaperServerListPingEventImpl.processRequest(MinecraftServer.getServer(), this.connection);
2021-06-11 14:02:28 +02:00
+ // Paper end
}
}
2021-11-23 15:03:50 +01:00
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/spigotmc/SpigotConfig.java b/src/main/java/org/spigotmc/SpigotConfig.java
2024-06-13 20:09:28 +02:00
index ba621fdc82896245f6ce448e084847edc4d3fe08..d063d356388810fb6f0dddfbc8b5885b3e6442aa 100644
2021-06-11 14:02:28 +02:00
--- 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()
{
2021-06-12 18:56:13 +02:00
- SpigotConfig.playerSample = SpigotConfig.getInt( "settings.sample-count", 12 );
+ SpigotConfig.playerSample = Math.max( SpigotConfig.getInt( "settings.sample-count", 12 ), 0 ); // Paper - Avoid negative counts
2021-06-11 14:02:28 +02:00
Bukkit.getLogger().log( Level.INFO, "Server Ping Player Sample Count: {0}", playerSample ); // Paper - Use logger
}