9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2026-01-03 22:16:41 +00:00
Files
Gale/patches/server/0095-Spread-out-sending-all-player-info.patch
2022-12-22 00:58:30 +01:00

187 lines
11 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: AGPL-3.0 (https://www.gnu.org/licenses/agpl-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/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
index c0226f0445e2554000f2fc0a3906f9b3ed404097..5d21b033ac5ee10efe77ae1fbff0585414bd2d74 100644
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -15,6 +15,8 @@ import java.net.SocketAddress;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.time.Instant;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
@@ -136,10 +138,11 @@ public abstract class PlayerList {
public static final File WHITELIST_FILE = new File("whitelist.json");
public static final Component CHAT_FILTERED_FULL = Component.translatable("chat.filtered_full");
private static final Logger LOGGER = LogUtils.getLogger();
- private static final int SEND_PLAYER_INFO_INTERVAL = 600;
+ public static final int SEND_PLAYER_INFO_INTERVAL = 600; // Gale - Purpur - spread out sending all player info - private -> public
private static final SimpleDateFormat BAN_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
private final MinecraftServer server;
public final List<ServerPlayer> players = new java.util.concurrent.CopyOnWriteArrayList(); // CraftBukkit - ArrayList -> CopyOnWriteArrayList: Iterator safety
+ private final ServerPlayer[][] sendAllPlayerInfoBuckets = new ServerPlayer[SEND_PLAYER_INFO_INTERVAL][]; // Gale - Purpur - spread out sending all player info
private final Map<UUID, ServerPlayer> playersByUUID = Maps.newHashMap();
private final UserBanList bans;
private final IpBanList ipBans;
@@ -363,6 +366,7 @@ public abstract class PlayerList {
player.sendServerStatus(this.server.getStatus());
player.connection.send(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(this.players));
this.players.add(player);
+ this.addToSendAllPlayerInfoBuckets(player); // Gale - Purpur - spread out sending all player info
this.playersByName.put(player.getScoreboardName().toLowerCase(java.util.Locale.ROOT), player); // Spigot
this.playersByUUID.put(player.getUUID(), player);
// this.broadcastAll(ClientboundPlayerInfoUpdatePacket.createPlayerInitializing(List.of(entityplayer))); // CraftBukkit - replaced with loop below
@@ -683,6 +687,7 @@ public abstract class PlayerList {
worldserver.removePlayerImmediately(entityplayer, Entity.RemovalReason.UNLOADED_WITH_PLAYER);
entityplayer.getAdvancements().stopListening();
this.players.remove(entityplayer);
+ this.removeFromSendAllPlayerInfoBuckets(entityplayer); // Gale - Purpur - spread out sending all player info
this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
this.server.getCustomBossEvents().onPlayerDisconnect(entityplayer);
UUID uuid = entityplayer.getUUID();
@@ -850,6 +855,7 @@ public abstract class PlayerList {
// Paper end
entityplayer.stopRiding(); // CraftBukkit
this.players.remove(entityplayer);
+ this.removeFromSendAllPlayerInfoBuckets(entityplayer); // Gale - Purpur - spread out sending all player info
this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
entityplayer.getLevel().removePlayerImmediately(entityplayer, Entity.RemovalReason.DISCARDED);
BlockPos blockposition = entityplayer.getRespawnPosition();
@@ -987,6 +993,7 @@ public abstract class PlayerList {
if (!entityplayer.connection.isDisconnected()) {
worldserver1.addRespawnedPlayer(entityplayer1);
this.players.add(entityplayer1);
+ this.addToSendAllPlayerInfoBuckets(entityplayer1); // Gale - Purpur - spread out sending all player info
this.playersByName.put(entityplayer1.getScoreboardName().toLowerCase(java.util.Locale.ROOT), entityplayer1); // Spigot
this.playersByUUID.put(entityplayer1.getUUID(), entityplayer1);
}
@@ -1037,20 +1044,55 @@ public abstract class PlayerList {
this.sendPlayerPermissionLevel(player, i);
}
- public void tick() {
- if (++this.sendAllPlayerInfoIn > 600) {
- // CraftBukkit start
- for (int i = 0; i < this.players.size(); ++i) {
- final ServerPlayer target = (ServerPlayer) this.players.get(i);
+ // 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 = 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] = Arrays.copyOf(sendAllPlayerInfoBucket, sendAllPlayerInfoBucket.length - 1);
+ }
+ }
+ }
+ }
+ // Gale end - Purpur - spread out sending all player info
+
+ public void tick() {
+ // 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), this.players.stream().filter(new Predicate<ServerPlayer>() {
@Override
public boolean test(ServerPlayer input) {
return target.getBukkitEntity().canSee(input.getBukkitEntity());
}
}).collect(Collectors.toList())));
+ // Gale start - Purpur - spread out sending all player info
}
- // CraftBukkit end
+ }
+ if (++this.sendAllPlayerInfoIn >= SEND_PLAYER_INFO_INTERVAL) {
+ // Gale end - Purpur - spread out sending all player info
this.sendAllPlayerInfoIn = 0;
}
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
index 2e6557a19523d18aecff709de30cb4466b46a9fa..6f071601266dff833270bbde9863cc0c4dcfa97d 100644
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
@@ -40,6 +40,7 @@ import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.server.players.PlayerList;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
@@ -193,12 +194,15 @@ public abstract class Player extends LivingEntity {
}
// CraftBukkit end
+ public final int sendAllPlayerInfoBucketIndex; // Gale - Purpur - spread out sending all player info
+
public Player(Level world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(EntityType.PLAYER, world);
this.lastItemInMainHand = ItemStack.EMPTY;
this.cooldowns = this.createItemCooldowns();
this.lastDeathLocation = Optional.empty();
this.setUUID(UUIDUtil.getOrCreatePlayerUUID(gameProfile));
+ this.sendAllPlayerInfoBucketIndex = Math.floorMod(this.uuid.hashCode(), PlayerList.SEND_PLAYER_INFO_INTERVAL); // Gale - Purpur - spread out sending all player info
this.gameProfile = gameProfile;
this.inventoryMenu = new InventoryMenu(this.inventory, !world.isClientSide, this);
this.containerMenu = this.inventoryMenu;