PlayerManager
Chunk watch manager: streams S21 chunk data, S22 multi-block deltas and S26 bulk loads to watching players.
Updated 2026-09-12
Packets Referenced (3)
Related Vanilla Internals
Deep Analysis
Chunk-subscription manager mapping players to PlayerInstance watchers. Fans out block deltas only to watchers of that chunk. Uses playerViewRadius 3-32 for subscription square. Batches changes per tick into single, multi, or full-chunk packets.
Key Methods in Depth (8)
Flag single block coordinate for next tick flush.
This method maps a world BlockPos to its chunk watcher and queues a delta. It derives chunk X and Z by arithmetic right shift 4 on block X and Z. It looks up the PlayerInstance with getPlayerInstance without creation, so unloaded chunks get no queue. If the instance exists it calls flagChunkForUpdate with local coordinates using bitwise and 15 for X and Z plus full Y. That call records the packed short and sets Y section flags for later S21 use. No S23 S22 or S21 packet is built here. The eventual choice depends on accumulated count: one becomes S23, two to sixty three become S22, sixty four becomes S21. This defers networking to PlayerInstance.onUpdate on the next tick. Chunk-scoped fan-out replaces any global radius broadcast.
public void markBlockForUpdate(BlockPos pos)
{
int i = pos.getX() >> 4;
int j = pos.getZ() >> 4;
PlayerManager.PlayerInstance playermanager$playerinstance = this.getPlayerInstance(i, j, false);
if (playermanager$playerinstance != null)
{
playermanager$playerinstance.flagChunkForUpdate(pos.getX() & 15, pos.getY(), pos.getZ() & 15);
}
}
The signature takes BlockPos to flag. The shift lines convert block to chunk coordinates. The getPlayerInstance call uses false to avoid creating watchers. The null check ensures only watched chunks queue. The flagChunkForUpdate line passes local x y z for packing. Packet selection is deferred to onUpdate batching.
Flush accumulated block changes to chunk watchers.
This is the per-tick flush that converts accumulated block edits into minimal packets. If numBlocksToUpdate is zero it does nothing and returns without networking. If count equals one it decodes the single packed short into world x y z and sends S23PacketBlockChange to chunk watchers. It also sends the tile entity description packet if that block has a tile entity. If count equals sixty four it sends S21PacketChunkData with flagsYAreasToUpdate mask for full chunk resend plus tile entities per dirty section. Otherwise for two to sixty three it sends S22PacketMultiBlockChange with count, short array, and chunk reference plus per-block tile packets. After any branch it resets numBlocksToUpdate and flagsYAreasToUpdate to zero. This 1 versus 2 to 63 versus 64 batching rule minimizes bandwidth. Only players in playersWatchingChunk receive the chosen packet.
public void onUpdate()
{
if (this.numBlocksToUpdate != 0)
{
if (this.numBlocksToUpdate == 1)
{
int i = (this.locationOfBlockChange[0] >> 12 & 15) + this.chunkCoords.chunkXPos * 16;
int j = this.locationOfBlockChange[0] & 255;
int k = (this.locationOfBlockChange[0] >> 8 & 15) + this.chunkCoords.chunkZPos * 16;
BlockPos blockpos = new BlockPos(i, j, k);
this.sendToAllPlayersWatchingChunk(new S23PacketBlockChange(PlayerManager.this.theWorldServer, blockpos));
if (PlayerManager.this.theWorldServer.getBlockState(blockpos).getBlock().hasTileEntity())
{
this.sendTileToAllPlayersWatchingChunk(PlayerManager.this.theWorldServer.getTileEntity(blockpos));
}
}
else if (this.numBlocksToUpdate == 64)
{
int i1 = this.chunkCoords.chunkXPos * 16;
int k1 = this.chunkCoords.chunkZPos * 16;
this.sendToAllPlayersWatchingChunk(new S21PacketChunkData(PlayerManager.this.theWorldServer.getChunkFromChunkCoords(this.chunkCoords.chunkXPos, this.chunkCoords.chunkZPos), false, this.flagsYAreasToUpdate));
for (int i2 = 0; i2 < 16; ++i2)
{
if ((this.flagsYAreasToUpdate & 1 << i2) != 0)
{
int k2 = i2 << 4;
List<TileEntity> list = PlayerManager.this.theWorldServer.getTileEntitiesIn(i1, k2, k1, i1 + 16, k2 + 16, k1 + 16);
}
}
}
The method checks numBlocksToUpdate nonzero before branching. The equals one branch decodes packed shorts to world coords and builds S23PacketBlockChange. The tile check adds description packets for containers. The equals 64 branch builds S21PacketChunkData with Y mask for full resend. The loop over 16 sections gathers tile entities per dirty slice. The missing tail handles 2 to 63 via S22 multi change.
Chunk-scoped fan-out excluding fully loaded senders.
This helper implements chunk-scoped fan-out for all block delta packets. It iterates playersWatchingChunk by index for the owning PlayerInstance. For each EntityPlayerMP it checks whether loadedChunks contains the chunkCoords. If the player already fully loaded the chunk via the load queue, the packet is skipped to avoid duplicate S21 or delta spam. Otherwise it sends the supplied Packet directly via playerNetServerHandler.sendPacket. No distance math with 16.0D or 1024.0D is performed because subscription implies proximity. The same Packet instance is reused across recipients, whether S23 single, S22 multi, or S21 chunk. This exclusion of fully loaded senders prevents echo during initial chunk burst. All networking for onUpdate flows through this single choke point.
public void sendToAllPlayersWatchingChunk(Packet thePacket)
{
for (int i = 0; i < this.playersWatchingChunk.size(); ++i)
{
EntityPlayerMP entityplayermp = (EntityPlayerMP)this.playersWatchingChunk.get(i);
if (!entityplayermp.loadedChunks.contains(this.chunkCoords))
{
entityplayermp.playerNetServerHandler.sendPacket(thePacket);
}
}
}
The signature takes a generic Packet to share. The for loop walks playersWatchingChunk for this chunk. The loadedChunks contains check skips players still in initial burst. The sendPacket line delivers the same S23 S22 or S21 instance to qualifiers. No radius or dimension check appears. This is pure subscription-based fan-out.
Subscribe player to all chunk instances in view square.
This method subscribes a joining or teleporting player to all nearby chunk watchers. It computes central chunk XZ from posX and posZ with right shift 4. It stores managedPosX and managedPosZ as the subscription origin for later movement diffs. It then loops a square from center minus playerViewRadius to center plus radius in both axes. For each coordinate it calls getPlayerInstance with true to create the instance if absent, which also loads the chunk. It then calls addPlayer on that instance to add the player to playersWatchingChunk and loadedChunks queue. After the square it adds the player to the global players list. It finally calls filterChunkLoadQueue to order pending S21 chunk data sends in spiral priority. The radius is clamped 3 to 32 elsewhere.
public void addPlayer(EntityPlayerMP player)
{
int i = (int)player.posX >> 4;
int j = (int)player.posZ >> 4;
player.managedPosX = player.posX;
player.managedPosZ = player.posZ;
for (int k = i - this.playerViewRadius; k <= i + this.playerViewRadius; ++k)
{
for (int l = j - this.playerViewRadius; l <= j + this.playerViewRadius; ++l)
{
this.getPlayerInstance(k, l, true).addPlayer(player);
}
}
this.players.add(player);
this.filterChunkLoadQueue(player);
}
The signature takes the joining EntityPlayerMP. The shift lines derive central chunk XZ. The managedPos lines snapshot origin for movement checks. The nested loops cover the view radius square and force-create instances. The addPlayer call subscribes to each chunk watcher. The final lines register globally and reorder load queue for S21 sends.
Unsubscribe player and unload empty chunks.
This method unsubscribes a leaving player and unloads now-empty chunks. It derives the origin chunk from managedPosX and managedPosZ, not live pos, to cover the subscribed square. It loops the same view radius square around that managed origin. For each coordinate it fetches the PlayerInstance without creation. If non-null it calls removePlayer on the instance to drop the player from watchers and loadedChunks. When an instance becomes empty, that inner method sends S21PacketChunkData unload, drops the chunk, and removes the instance maps. After the loops it removes the player from the global players list. This ensures no ghost watchers remain after logout or dimension change. The S21 unload packet tells the client to discard chunk data.
public void removePlayer(EntityPlayerMP player)
{
int i = (int)player.managedPosX >> 4;
int j = (int)player.managedPosZ >> 4;
for (int k = i - this.playerViewRadius; k <= i + this.playerViewRadius; ++k)
{
for (int l = j - this.playerViewRadius; l <= j + this.playerViewRadius; ++l)
{
PlayerManager.PlayerInstance playermanager$playerinstance = this.getPlayerInstance(k, l, false);
if (playermanager$playerinstance != null)
{
playermanager$playerinstance.removePlayer(player);
}
}
}
this.players.remove(player);
}
The signature takes the departing EntityPlayerMP. The managedPos shift lines recover the subscribed center. The nested loops visit every watcher in the square without creating new ones. The null check plus removePlayer call handles unsubscribe and empty-chunk S21 unload. The final remove drops the global reference. This mirrors addPlayer subscription logic.
Tick dirty chunk instances each server tick.
This per-tick driver flushes dirty chunk instances for block deltas. It reads total world time and compares to previousTotalWorldTime. If more than 8000 ticks elapsed it does a full pass over playerInstanceList calling onUpdate plus processChunk for inhabited time. Otherwise it only iterates playerInstancesToUpdate, which holds instances flagged by markBlockForUpdate. After either path it clears playerInstancesToUpdate to reset dirty state. If the players list is empty and the provider cannot respawn here, it unloads all chunks to save memory. This method is called from WorldServer.tick chunkMap section. It is the indirect source of S23 for one change, S22 for two to sixty three, and S21 for sixty four. No radius check occurs because watchers define recipients.
public void updatePlayerInstances()
{
long i = this.theWorldServer.getTotalWorldTime();
if (i - this.previousTotalWorldTime > 8000L)
{
this.previousTotalWorldTime = i;
for (int j = 0; j < this.playerInstanceList.size(); ++j)
{
PlayerManager.PlayerInstance playermanager$playerinstance = (PlayerManager.PlayerInstance)this.playerInstanceList.get(j);
playermanager$playerinstance.onUpdate();
playermanager$playerinstance.processChunk();
}
}
else
{
for (int k = 0; k < this.playerInstancesToUpdate.size(); ++k)
{
PlayerManager.PlayerInstance playermanager$playerinstance1 = (PlayerManager.PlayerInstance)this.playerInstancesToUpdate.get(k);
playermanager$playerinstance1.onUpdate();
}
}
this.playerInstancesToUpdate.clear();
if (this.players.isEmpty())
{
WorldProvider worldprovider = this.theWorldServer.provider;
}
The signature takes no args and reads world time. The greater than 8000L branch does full onUpdate plus inhabited time. The else branch flushes only dirty instances queued for S23 S22 S21. The clear call resets the dirty list each tick. The trailing players empty check leads to unload when no viewers remain. This shows tick-driven batched flushing.
Update subscriptions after fast vehicle movement.
This method updates chunk subscriptions after fast vehicle movement like boats or minecarts. It computes current chunk XZ from live posX posZ and the delta from managedPosX managedPosZ. It calculates squared movement d2 and only proceeds if at least 64.0D, which equals 8 blocks. It then derives old managed chunk and view radius plus delta in chunks j1 k1. If no chunk changed it returns without networking. Otherwise it loops the new view square, adding the player to instances not overlapping the old square. It also loops to remove from old instances not overlapping the new square. It finally calls filterChunkLoadQueue and snaps managedPos to live pos. This diff avoids resubscribing the entire square on small moves. New chunks trigger S21 loads while stale ones trigger S21 unloads.
public void updateMountedMovingPlayer(EntityPlayerMP player)
{
int i = (int)player.posX >> 4;
int j = (int)player.posZ >> 4;
double d0 = player.managedPosX - player.posX;
double d1 = player.managedPosZ - player.posZ;
double d2 = d0 * d0 + d1 * d1;
if (d2 >= 64.0D)
{
int k = (int)player.managedPosX >> 4;
int l = (int)player.managedPosZ >> 4;
int i1 = this.playerViewRadius;
int j1 = i - k;
int k1 = j - l;
if (j1 != 0 || k1 != 0)
{
for (int l1 = i - i1; l1 <= i + i1; ++l1)
{
for (int i2 = j - i1; i2 <= j + i1; ++i2)
{
if (!this.overlaps(l1, i2, k, l, i1))
{
this.getPlayerInstance(l1, i2, true).addPlayer(player);
}
if (!this.overlaps(l1 - j1, i2 - k1, i, j, i1))
{
PlayerManager.PlayerInstance playermanager$playerinstance = this.getPlayerInstance(l1 - j1, i2 - k1, false);
if (playermanager$playerinstance != null)
{
playermanager$playerinstance.removePlayer(player);
}
}
}
}
}
}
The signature takes the mounted EntityPlayerMP. The chunk and delta lines compute movement squared. The greater than equals 64.0D gate equals 8 blocks before work. The j1 k1 check ensures a chunk boundary crossed. The nested loops add new watchers and remove stale ones via overlaps tests. Later lines reorder load queue and snap managedPos. This is differential S21 subscription update.
Reorder pending chunk loads in spiral around player.
This method reorders a player pending chunk load queue into spiral priority around the player. It copies loadedChunks to a temp list then clears the live queue. It computes view radius, central chunk XZ, and fetches the center PlayerInstance chunkCoords. If the old list contained the center it is added first for immediate S21 send. It then walks outward rings using xzDirectionsConst east south west north vectors with expanding step counts up to twice radius. Each visited coordinate is resolved to its PlayerInstance chunkCoords and re-added if previously queued. A final straight tail fills remaining edge chunks. This ordering makes closest chunks load first on login or teleport. The S21 chunk data sends later follow this queue order. No packets are built here, only queue order.
public void filterChunkLoadQueue(EntityPlayerMP player)
{
List<ChunkCoordIntPair> list = Lists.newArrayList(player.loadedChunks);
int i = 0;
int j = this.playerViewRadius;
int k = (int)player.posX >> 4;
int l = (int)player.posZ >> 4;
int i1 = 0;
int j1 = 0;
ChunkCoordIntPair chunkcoordintpair = this.getPlayerInstance(k, l, true).chunkCoords;
player.loadedChunks.clear();
if (list.contains(chunkcoordintpair))
{
player.loadedChunks.add(chunkcoordintpair);
}
for (int k1 = 1; k1 <= j * 2; ++k1)
{
for (int l1 = 0; l1 < 2; ++l1)
{
int[] aint = this.xzDirectionsConst[i++ % 4];
for (int i2 = 0; i2 < k1; ++i2)
{
i1 += aint[0];
j1 += aint[1];
chunkcoordintpair = this.getPlayerInstance(k + i1, l + j1, true).chunkCoords;
if (list.contains(chunkcoordintpair))
{
player.loadedChunks.add(chunkcoordintpair);
}
}
}
}
The signature takes the player whose queue is rebuilt. The copy plus clear lines snapshot and reset pending chunks. The center fetch plus contains check prioritizes the standing chunk. The nested k1 l1 loops walk the xzDirectionsConst spiral outward. Each step resolves chunkCoords and re-adds if previously pending. This controls S21 send order without sending directly.
Packet Usage
| Packet | Dir | Trigger |
|---|---|---|
| S23 | sent | onUpdate single block change to watchers |
| S22 | sent | onUpdate 2-63 block changes to watchers |
| S21 | sent | onUpdate 64 changes or chunk load/unload |
Internal State
Related Vanilla Internals
Full Source
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
Deobfuscated MCP 1.8.9 source of PlayerManager from Marcelektro/MavenMCP-1.8.9 (net/minecraft/server/management/PlayerManager.java), stored verbatim.