Paper/Spigot-Server-Patches/0499-Optimize-sending-packets-to-nearby-locations-sounds-.patch
Aikar b3f265d1b1
Updated Upstream (Bukkit/CraftBukkit/Spigot)
Upstream has released updates that appears to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
0ca45a21 #503: Add PlayerHarvestBlockEvent
dfa80a52 SPIGOT-5930: Add PlayerRespawnEvent#isAnchorSpawn

CraftBukkit Changes:
145921e2 #676: Add PlayerHarvestBlockEvent
47abffa2 SPIGOT-5929: Angered zombified piglins do not inherit killed_by_player status
7f6b4f58 SPIGOT-5930: Add PlayerRespawnEvent#isAnchorSpawn
94eff632 SPIGOT-5867, MC-193339: NPE during shutdown when rcon enabled with no password
068618eb SPIGOT-5927: Some items NBT data disappears

Spigot Changes:
beb7d47c Rebuild patches

Fixes #3738
2020-07-03 02:19:33 -04:00

64 lines
3.5 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sat, 23 May 2020 17:03:41 -0400
Subject: [PATCH] Optimize sending packets to nearby locations (sounds/effects)
Instead of using the entire world or player list, use the distance
maps to only iterate players who are even seeing the chunk the packet
is originating from.
This will drastically cut down on packet sending cost for worlds with
lots of players in them.
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
index e55e11d5c97d656db8fed42c1581375077129906..f17399a73e0de49d2e7325747a2637f447df78f2 100644
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
@@ -1059,16 +1059,40 @@ public abstract class PlayerList {
}
public void sendPacketNearby(@Nullable EntityHuman entityhuman, double d0, double d1, double d2, double d3, ResourceKey<World> resourcekey, Packet<?> packet) {
- for (int i = 0; i < this.players.size(); ++i) {
- EntityPlayer entityplayer = (EntityPlayer) this.players.get(i);
+ WorldServer world = null;
+ if (entityhuman != null && entityhuman.world instanceof WorldServer) {
+ world = (WorldServer) entityhuman.world;
+ }
- // CraftBukkit start - Test if player receiving packet can see the source of the packet
- if (entityhuman != null && entityhuman instanceof EntityPlayer && !entityplayer.getBukkitEntity().canSee(((EntityPlayer) entityhuman).getBukkitEntity())) {
- continue;
+ // Paper start
+ if (world == null) {
+ world = server.getWorldServer(resourcekey);
+ }
+ PlayerChunkMap chunkMap = world != null ? world.getChunkProvider().playerChunkMap : null;
+ Object[] backingSet;
+ if (chunkMap == null) {
+ // Really shouldn't happen...
+ backingSet = world != null ? world.players.toArray() : players.toArray();
+ } else {
+ com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<EntityPlayer> nearbyPlayers = chunkMap.playerViewDistanceBroadcastMap.getObjectsInRange(MCUtil.fastFloor(d0) >> 4, MCUtil.fastFloor(d2) >> 4);
+ if (nearbyPlayers == null) {
+ return;
}
+ backingSet = nearbyPlayers.getBackingSet();
+ }
+
+ for (Object object : backingSet) {
+ if (!(object instanceof EntityPlayer)) continue;
+ EntityPlayer entityplayer = (EntityPlayer) object;
+ // Paper end
+
+ // CraftBukkit start - Test if player receiving packet can see the source of the packet
+ //if (entityhuman != null && entityhuman instanceof EntityPlayer && !entityplayer.getBukkitEntity().canSee(((EntityPlayer) entityhuman).getBukkitEntity())) { // Paper
+ //continue; // Paper
+ //} // Paper
// CraftBukkit end
- if (entityplayer != entityhuman && entityplayer.world.getDimensionKey() == resourcekey) {
+ if (entityplayer != entityhuman && entityplayer.world.getDimensionKey() == resourcekey && (!(entityhuman instanceof EntityPlayer) || entityplayer.getBukkitEntity().canSee(((EntityPlayer) entityhuman).getBukkitEntity()))) { // Paper
double d4 = d0 - entityplayer.locX();
double d5 = d1 - entityplayer.locY();
double d6 = d2 - entityplayer.locZ();