9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-24 01:19:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0071-Spread-out-sending-all-player-info.patch
2025-06-15 08:13:47 +08:00

164 lines
9.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Tue, 29 Nov 2022 23:18:57 +0100
Subject: [PATCH] Spread out sending all player info
License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following patch:
"Spread out and optimise player list ticksSpread out and optimise player list ticks"
By: James Lyne <jim+github@not-null.co.uk>
As part of: Purpur (https://github.com/PurpurMC/Purpur)
Licensed under: MIT (https://opensource.org/licenses/MIT)
* Purpur copyright *
MIT License
Copyright (c) 2019-2022 PurpurMC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
index 186e0a6e565d29572f5d4771a68e4a9cb2bd45de..91d4a5f7685296e397c1c341813542fd13eaf168 100644
--- a/net/minecraft/server/players/PlayerList.java
+++ b/net/minecraft/server/players/PlayerList.java
@@ -113,6 +113,7 @@ public abstract class PlayerList {
private final MinecraftServer server;
public final List<ServerPlayer> players = new java.util.concurrent.CopyOnWriteArrayList(); // CraftBukkit - ArrayList -> CopyOnWriteArrayList: Iterator safety
private final Map<UUID, ServerPlayer> playersByUUID = Maps.newHashMap();
+ private final ServerPlayer[][] sendAllPlayerInfoBuckets = new ServerPlayer[SEND_PLAYER_INFO_INTERVAL][]; // Gale - Purpur - spread out sending all player info
private final UserBanList bans = new UserBanList(USERBANLIST_FILE);
private final IpBanList ipBans = new IpBanList(IPBANLIST_FILE);
private final ServerOpList ops = new ServerOpList(OPLIST_FILE);
@@ -312,6 +313,7 @@ public abstract class PlayerList {
this.players.add(player);
this.playersByName.put(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT), player); // Spigot
this.playersByUUID.put(player.getUUID(), player);
+ this.addToSendAllPlayerInfoBuckets(player); // Gale - Purpur - spread out sending all player info
// this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(player))); // CraftBukkit - replaced with loop below
// Paper start - Fire PlayerJoinEvent when Player is actually ready; correctly register player BEFORE PlayerJoinEvent, so the entity is valid and doesn't require tick delay hacks
player.supressTrackerForLogin = true;
@@ -585,6 +587,7 @@ public abstract class PlayerList {
player.getAdvancements().stopListening();
this.players.remove(player);
this.playersByName.remove(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
+ this.removeFromSendAllPlayerInfoBuckets(player); // Gale - Purpur - spread out sending all player info
this.server.getCustomBossEvents().onPlayerDisconnect(player);
UUID uuid = player.getUUID();
ServerPlayer serverPlayer = this.playersByUUID.get(uuid);
@@ -720,6 +723,7 @@ public abstract class PlayerList {
player.stopRiding(); // CraftBukkit
this.players.remove(player);
this.playersByName.remove(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
+ this.removeFromSendAllPlayerInfoBuckets(player); // Gale - Purpur - spread out sending all player info
player.level().removePlayerImmediately(player, reason);
// TeleportTransition teleportTransition = player.findRespawnPositionAndUseSpawnBlock(!keepInventory, TeleportTransition.DO_NOTHING);
// ServerLevel level = teleportTransition.newLevel();
@@ -798,6 +802,7 @@ public abstract class PlayerList {
this.players.add(serverPlayer);
this.playersByName.put(serverPlayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT), serverPlayer); // Spigot
this.playersByUUID.put(serverPlayer.getUUID(), serverPlayer);
+ this.addToSendAllPlayerInfoBuckets(serverPlayer); // Gale - Purpur - spread out sending all player info
}
// serverPlayer.initInventoryMenu();
serverPlayer.setHealth(serverPlayer.getHealth());
@@ -900,18 +905,58 @@ public abstract class PlayerList {
}
public void tick() {
- if (++this.sendAllPlayerInfoIn > 600) {
- // CraftBukkit start
- for (int i = 0; i < this.players.size(); ++i) {
- final ServerPlayer target = this.players.get(i);
+ // Gale start - Purpur - spread out sending all player info
+ ServerPlayer[] sendAllPlayerInfoBucket = this.sendAllPlayerInfoBuckets[this.sendAllPlayerInfoIn];
+
+ if (sendAllPlayerInfoBucket != null) {
+ for (ServerPlayer target : sendAllPlayerInfoBucket) {
+ // Gale end - Purpur - spread out sending all player info
target.connection.send(new ClientboundPlayerInfoUpdatePacket(EnumSet.of(ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY), com.google.common.collect.Collections2.filter(this.players, t -> target.getBukkitEntity().canSee(t.getBukkitEntity()))));
}
- // CraftBukkit end
+ // Gale start - Purpur - spread out sending all player info
+ }
+
+ if (++this.sendAllPlayerInfoIn >= SEND_PLAYER_INFO_INTERVAL) {
+ // Gale end - Purpur - spread out sending all player info
this.sendAllPlayerInfoIn = 0;
}
}
+ // Gale start - Purpur - spread out sending all player info
+ private void addToSendAllPlayerInfoBuckets(ServerPlayer player) {
+ ServerPlayer[] sendAllPlayerInfoBucket = this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex];
+
+ if (sendAllPlayerInfoBucket == null) {
+ this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex] = new ServerPlayer[]{player};
+ } else {
+ this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex] = sendAllPlayerInfoBucket = java.util.Arrays.copyOf(sendAllPlayerInfoBucket, sendAllPlayerInfoBucket.length + 1);
+ sendAllPlayerInfoBucket[sendAllPlayerInfoBucket.length - 1] = player;
+ }
+ }
+
+ private void removeFromSendAllPlayerInfoBuckets(ServerPlayer player) {
+ ServerPlayer[] sendAllPlayerInfoBucket = this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex];
+
+ if (sendAllPlayerInfoBucket != null) {
+ if (sendAllPlayerInfoBucket.length == 1) {
+ if (sendAllPlayerInfoBucket[0] == player) {
+ this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex] = null;
+ }
+
+ return;
+ }
+
+ for (int i = 0; i < sendAllPlayerInfoBucket.length; i++) {
+ if (sendAllPlayerInfoBucket[i] == player) {
+ sendAllPlayerInfoBucket[i] = sendAllPlayerInfoBucket[sendAllPlayerInfoBucket.length - 1];
+ this.sendAllPlayerInfoBuckets[player.sendAllPlayerInfoBucketIndex] = java.util.Arrays.copyOf(sendAllPlayerInfoBucket, sendAllPlayerInfoBucket.length - 1);
+ }
+ }
+ }
+ }
+ // Gale end - Purpur - spread out sending all player info
+
// CraftBukkit start - add a world/entity limited version
public void broadcastAll(Packet packet, net.minecraft.world.entity.player.Player entityhuman) {
for (ServerPlayer entityplayer : this.players) { // Paper - replace for i with for each for thread safety
diff --git a/net/minecraft/world/entity/player/Player.java b/net/minecraft/world/entity/player/Player.java
index abb19f08c736151fa5c6986e31eb4cf545073586..5e01ae7c5d11777868d72589019ad11305c9acdd 100644
--- a/net/minecraft/world/entity/player/Player.java
+++ b/net/minecraft/world/entity/player/Player.java
@@ -230,9 +230,12 @@ public abstract class Player extends LivingEntity {
}
// CraftBukkit end
+ public final int sendAllPlayerInfoBucketIndex; // Gale - Purpur - spread out sending all player info
+
public Player(Level level, GameProfile gameProfile) {
super(EntityType.PLAYER, level);
this.setUUID(gameProfile.getId());
+ this.sendAllPlayerInfoBucketIndex = Math.floorMod(this.uuid.hashCode(), net.minecraft.server.players.PlayerList.SEND_PLAYER_INFO_INTERVAL); // Gale - Purpur - spread out sending all player info
this.gameProfile = gameProfile;
this.inventory = new Inventory(this, this.equipment);
this.inventoryMenu = new InventoryMenu(this.inventory, !level.isClientSide, this);