@@ -67,6 +67,7 @@ public class TuffX extends JavaPlugin implements Listener, PluginMessageListener
6767 private final ThreadLocal <short []> threadLocalBlockArray = ThreadLocal .withInitial (() -> new short [4096 ]);
6868 private final ThreadLocal <byte []> threadLocalLightArray = ThreadLocal .withInitial (() -> new byte [4096 ]);
6969 private final ThreadLocal <ByteArrayOutputStream > threadLocalOutStream = ThreadLocal .withInitial (() -> new ByteArrayOutputStream (8256 ));
70+ private final Set <WorldChunkKey > pendingChunkLoads = ConcurrentHashMap .newKeySet ();
7071
7172 private void logDebug (String message ) {
7273 if (debug ) getLogger ().log (Level .INFO , "[TuffX-Debug] " + message );
@@ -253,42 +254,20 @@ public void run() {
253254 if (cachedData != null ) {
254255 sendPayloadsToPlayer (player , cachedData );
255256 checkIfInitialLoadComplete (player );
256- continue ;
257- }
258-
259- if (world .isChunkLoaded (key .x (), key .z ())) {
260- processAndSendChunk (player , world .getChunkAt (key .x (), key .z ()));
261257 } else {
262- queue .add (vec );
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 );
263+ }
263264 }
264265 }
265266 }
266267 }
267268 }.runTaskTimer (this , 0L , 1L );
268269 }
269270
270- @ EventHandler (priority = EventPriority .MONITOR )
271- public void onPlayerChangeWorld (PlayerChangedWorldEvent event ) {
272- Player player = event .getPlayer ();
273- UUID playerId = player .getUniqueId ();
274-
275- Queue <Vector > playerQueue = requestQueue .get (playerId );
276- if (playerQueue != null && !playerQueue .isEmpty ()) {
277- logDebug ("Player " + player .getName () + " changed worlds. Clearing " + playerQueue .size () + " pending chunk requests." );
278- playerQueue .clear ();
279- }
280-
281- if (initialChunksToProcess .remove (playerId ) != null ) {
282- logDebug ("Player " + player .getName () + " was in the middle of an initial chunk load. The process has been cancelled." );
283- awaitingInitialBatch .remove (playerId );
284- player .sendPluginMessage (this , CHANNEL , createLoadFinishedPayload ());
285- }
286-
287- player .sendPluginMessage (this , CHANNEL , createDimensionPayload ());
288-
289- player .sendPluginMessage (this , CHANNEL , createBelowY0StatusPayload (enabledWorlds .contains (player .getWorld ().getName ())));
290- }
291-
292271 private void processAndSendChunk (final Player player , final Chunk chunk ) {
293272 if (chunk == null || !player .isOnline () || chunkProcessorPool .isShutdown ()) {
294273 return ;
@@ -301,10 +280,9 @@ private void processAndSendChunk(final Player player, final Chunk chunk) {
301280 final ChunkSnapshot snapshot = chunk .getChunkSnapshot (true , false , false );
302281 final Map <BlockData , int []> conversionCache = threadLocalConversionCache .get ();
303282 conversionCache .clear ();
304-
305283 for (int sectionY = -4 ; sectionY < 0 ; sectionY ++) {
306284 if (!player .isOnline ()) {
307- return ;
285+ return ;
308286 }
309287 try {
310288 byte [] payload = createSectionPayload (snapshot , chunk .getX (), chunk .getZ (), sectionY , conversionCache );
@@ -322,16 +300,36 @@ private void processAndSendChunk(final Player player, final Chunk chunk) {
322300 @ Override
323301 public void run () {
324302 if (player .isOnline ()) {
325- for (byte [] payload : processedPayloads ) {
326- player .sendPluginMessage (TuffX .this , CHANNEL , payload );
327- }
303+ sendPayloadsToPlayer (player , processedPayloads );
328304 checkIfInitialLoadComplete (player );
329305 }
330306 }
331307 }.runTask (this );
332308 });
333309 }
334310
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+
335333 private void sendPayloadsToPlayer (Player player , List <byte []> payloads ) {
336334 new BukkitRunnable () {
337335 @ Override
0 commit comments