@@ -241,25 +241,26 @@ public void run() {
241241 }
242242
243243 Queue <Vector > queue = requestQueue .get (playerUUID );
244- if (queue == null ) continue ;
245-
246- for (int i = 0 ; i < CHUNKS_PER_TICK && !queue .isEmpty (); i ++) {
247- Vector vec = queue .poll ();
248- if (vec == null ) continue ;
249-
250- World world = player .getWorld ();
251- WorldChunkKey key = new WorldChunkKey (world .getName (), vec .getBlockX (), vec .getBlockZ ());
252-
253- List <byte []> cachedData = chunkPayloadCache .getIfPresent (key );
254- if (cachedData != null ) {
255- sendPayloadsToPlayer (player , cachedData );
256- checkIfInitialLoadComplete (player );
257- } else {
258- if (world .isChunkLoaded (key .x (), key .z ())) {
259- processAndSendChunk (player , world .getChunkAt (key .x (), key .z ()));
260- } else {
261- world .loadChunk (key .x (), key .z (), true );
262- queue .add (vec );
244+ if (queue != null && !queue .isEmpty ()) {
245+ for (int i = 0 ; i < CHUNKS_PER_TICK && !queue .isEmpty (); i ++) {
246+ Vector vec = queue .poll ();
247+ if (vec != null ) {
248+ World world = player .getWorld ();
249+
250+ WorldChunkKey key = new WorldChunkKey (world .getName (), vec .getBlockX (), vec .getBlockZ ());
251+ List <byte []> cachedData = chunkPayloadCache .getIfPresent (key );
252+ if (cachedData != null ) {
253+ sendPayloadsToPlayer (player , cachedData );
254+ checkIfInitialLoadComplete (player );
255+ continue ;
256+ }
257+
258+ if (world .isChunkLoaded (vec .getBlockX (), vec .getBlockZ ())) {
259+ processAndSendChunk (player , world .getChunkAt (vec .getBlockX (), vec .getBlockZ ()));
260+ } else {
261+ world .loadChunk (vec .getBlockX (), vec .getBlockZ (), true );
262+ queue .add (vec );
263+ }
263264 }
264265 }
265266 }
@@ -268,6 +269,41 @@ public void run() {
268269 }.runTaskTimer (this , 0L , 1L );
269270 }
270271
272+ private void sendPayloadsToPlayer (Player player , List <byte []> payloads ) {
273+ new BukkitRunnable () {
274+ @ Override
275+ public void run () {
276+ if (player .isOnline ()) {
277+ for (byte [] payload : payloads ) {
278+ player .sendPluginMessage (TuffX .this , CHANNEL , payload );
279+ }
280+ }
281+ }
282+ }.runTask (this );
283+ }
284+
285+ @ EventHandler (priority = EventPriority .MONITOR )
286+ public void onPlayerChangeWorld (PlayerChangedWorldEvent event ) {
287+ Player player = event .getPlayer ();
288+ UUID playerId = player .getUniqueId ();
289+
290+ Queue <Vector > playerQueue = requestQueue .get (playerId );
291+ if (playerQueue != null && !playerQueue .isEmpty ()) {
292+ logDebug ("Player " + player .getName () + " changed worlds. Clearing " + playerQueue .size () + " pending chunk requests." );
293+ playerQueue .clear ();
294+ }
295+
296+ if (initialChunksToProcess .remove (playerId ) != null ) {
297+ logDebug ("Player " + player .getName () + " was in the middle of an initial chunk load. The process has been cancelled." );
298+ awaitingInitialBatch .remove (playerId );
299+ player .sendPluginMessage (this , CHANNEL , createLoadFinishedPayload ());
300+ }
301+
302+ player .sendPluginMessage (this , CHANNEL , createDimensionPayload ());
303+
304+ player .sendPluginMessage (this , CHANNEL , createBelowY0StatusPayload (enabledWorlds .contains (player .getWorld ().getName ())));
305+ }
306+
271307 private void processAndSendChunk (final Player player , final Chunk chunk ) {
272308 if (chunk == null || !player .isOnline () || chunkProcessorPool .isShutdown ()) {
273309 return ;
@@ -280,9 +316,10 @@ private void processAndSendChunk(final Player player, final Chunk chunk) {
280316 final ChunkSnapshot snapshot = chunk .getChunkSnapshot (true , false , false );
281317 final Map <BlockData , int []> conversionCache = threadLocalConversionCache .get ();
282318 conversionCache .clear ();
319+
283320 for (int sectionY = -4 ; sectionY < 0 ; sectionY ++) {
284321 if (!player .isOnline ()) {
285- return ;
322+ return ;
286323 }
287324 try {
288325 byte [] payload = createSectionPayload (snapshot , chunk .getX (), chunk .getZ (), sectionY , conversionCache );
@@ -300,55 +337,16 @@ private void processAndSendChunk(final Player player, final Chunk chunk) {
300337 @ Override
301338 public void run () {
302339 if (player .isOnline ()) {
303- sendPayloadsToPlayer (player , processedPayloads );
340+ for (byte [] payload : processedPayloads ) {
341+ player .sendPluginMessage (TuffX .this , CHANNEL , payload );
342+ }
304343 checkIfInitialLoadComplete (player );
305344 }
306345 }
307346 }.runTask (this );
308347 });
309348 }
310349
311- @ EventHandler (priority = EventPriority .MONITOR )
312- public void onPlayerChangeWorld (PlayerChangedWorldEvent event ) {
313- Player player = event .getPlayer ();
314- UUID playerId = player .getUniqueId ();
315-
316- Queue <Vector > playerQueue = requestQueue .get (playerId );
317- if (playerQueue != null && !playerQueue .isEmpty ()) {
318- logDebug ("Player " + player .getName () + " changed worlds. Clearing " + playerQueue .size () + " pending chunk requests." );
319- playerQueue .clear ();
320- }
321-
322- if (initialChunksToProcess .remove (playerId ) != null ) {
323- logDebug ("Player " + player .getName () + " was in the middle of an initial chunk load. The process has been cancelled." );
324- awaitingInitialBatch .remove (playerId );
325- player .sendPluginMessage (this , CHANNEL , createLoadFinishedPayload ());
326- }
327-
328- player .sendPluginMessage (this , CHANNEL , createDimensionPayload ());
329-
330- player .sendPluginMessage (this , CHANNEL , createBelowY0StatusPayload (enabledWorlds .contains (player .getWorld ().getName ())));
331- }
332-
333- private void sendPayloadsToPlayer (Player player , List <byte []> payloads ) {
334- new BukkitRunnable () {
335- @ Override
336- public void run () {
337- if (player .isOnline ()) {
338- for (byte [] payload : payloads ) {
339- player .sendPluginMessage (TuffX .this , CHANNEL , payload );
340- }
341- }
342- }
343- }.runTask (this );
344- }
345-
346- private void invalidateChunkCache (World world , int blockX , int blockZ ) {
347- WorldChunkKey key = new WorldChunkKey (world .getName (), blockX >> 4 , blockZ >> 4 );
348- chunkPayloadCache .invalidate (key );
349- logDebug ("Invalidated cache for chunk: " + key );
350- }
351-
352350 private void checkIfInitialLoadComplete (Player player ) {
353351 UUID playerId = player .getUniqueId ();
354352 AtomicInteger counter = initialChunksToProcess .get (playerId );
@@ -366,6 +364,12 @@ private void checkIfInitialLoadComplete(Player player) {
366364 }
367365 }
368366
367+ private void invalidateChunkCache (World world , int blockX , int blockZ ) {
368+ WorldChunkKey key = new WorldChunkKey (world .getName (), blockX >> 4 , blockZ >> 4 );
369+ chunkPayloadCache .invalidate (key );
370+ logDebug ("Invalidated cache for chunk: " + key );
371+ }
372+
369373 private byte [] createLoadFinishedPayload () {
370374 try (ByteArrayOutputStream bout = new ByteArrayOutputStream ();
371375 DataOutputStream out = new DataOutputStream (bout )) {
@@ -472,7 +476,8 @@ public void onBlockPlace(BlockPlaceEvent event) {
472476 public void onBlockPhysics (BlockPhysicsEvent event ) {
473477 Block block = event .getBlock ();
474478 if (block .getY () < 0 ) {
475- sendSingleBlockUpdate (block .getLocation (), block .getBlockData ());sendLightingUpdate (block .getLocation ());
479+ sendSingleBlockUpdate (block .getLocation (), block .getBlockData ());
480+ //sendLightingUpdate(block.getLocation());
476481 invalidateChunkCache (block .getWorld (), block .getX (), block .getZ ());
477482 }
478483 }
0 commit comments