From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar 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 f0fe864026dc10cd809f06dc9d4e823c21494a8d..5b634f2385f6dbb62cf3884daabded3870e8b8cc 100644 --- a/src/main/java/net/minecraft/server/PlayerList.java +++ b/src/main/java/net/minecraft/server/PlayerList.java @@ -1031,11 +1031,30 @@ public abstract class PlayerList { world = (WorldServer) entityhuman.world; } - List players1 = world == null ? players : world.players; - for (int j = 0; j < players1.size(); ++j) { - EntityHuman entity = players1.get(j); - if (!(entity instanceof EntityPlayer)) continue; - EntityPlayer entityplayer = (EntityPlayer) entity; + // Paper start + if ((world == null || world.chunkProvider == null) && dimensionmanager != null) { + world = dimensionmanager.world; + } + if (world == null) { + LOGGER.error("Sending packet to invalid world" + entityhuman + " " + dimensionmanager + " - " + packet.getClass().getName(), new Throwable()); + return; // ??? shouldn't happen... + } + PlayerChunkMap chunkMap = world.chunkMap; + Object[] backingSet; + if (chunkMap == null) { + // Really shouldn't happen... + backingSet = world.players.toArray(); + } else { + com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet 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 diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java index aaf85a1497de98522e3a01d4f81a267c4b0cc087..2c9acffe5a62af43ff4f4ccdb6962929d645e226 100644 --- a/src/main/java/net/minecraft/server/WorldServer.java +++ b/src/main/java/net/minecraft/server/WorldServer.java @@ -70,6 +70,7 @@ public class WorldServer extends World { } } // Paper end + public final PlayerChunkMap chunkMap; // Paper private final MinecraftServer server; private final WorldNBTStorage dataManager; public boolean savingDisabled; @@ -208,6 +209,8 @@ public class WorldServer extends World { }, gameprofilerfiller, false, gen, env); this.pvpMode = minecraftserver.getPVP(); worlddata.world = this; + if (chunkProvider == null) { chunkMap = null; new Throwable("World created without a ChunkProvider!").printStackTrace(); } // Paper - figure out if something weird happened here + else chunkMap = ((ChunkProviderServer) chunkProvider).playerChunkMap; // CraftBukkit end if (com.destroystokyo.paper.PaperConfig.useOptimizedTickList) { this.nextTickListBlock = new com.destroystokyo.paper.server.ticklist.PaperTickList<>(this, (block) -> { // Paper - optimise TickListServer