@@ -306,13 +306,33 @@ public void sendChunkPacket(Stage stage, Player player, BlockifyChunk chunk, boo
306306
307307 private ConcurrentHashMap <BlockifyChunk , ConcurrentHashMap <BlockifyPosition , BlockData >> getBlockChanges (Stage stage , Collection <BlockifyChunk > chunks ) {
308308 ConcurrentHashMap <BlockifyChunk , ConcurrentHashMap <BlockifyPosition , BlockData >> blockChanges = new ConcurrentHashMap <>();
309+ ConcurrentHashMap <BlockifyChunk , ConcurrentHashMap <BlockifyPosition , Integer >> highestZIndexes = new ConcurrentHashMap <>();
310+
309311 for (View view : stage .getViews ()) {
312+ int zIndex = view .getZIndex ();
310313 for (Map .Entry <BlockifyChunk , ConcurrentHashMap <BlockifyPosition , BlockData >> entry : view .getBlocks ().entrySet ()) {
311- if (!chunks .contains (entry .getKey ())) continue ;
312- if (blockChanges .containsKey (entry .getKey ())) {
313- blockChanges .get (entry .getKey ()).putAll (entry .getValue ());
314- } else {
315- blockChanges .put (entry .getKey (), new ConcurrentHashMap <>(entry .getValue ()));
314+ BlockifyChunk chunk = entry .getKey ();
315+ if (!chunks .contains (chunk )) continue ;
316+
317+ highestZIndexes .computeIfAbsent (chunk , k -> new ConcurrentHashMap <>());
318+
319+ for (Map .Entry <BlockifyPosition , BlockData > positionEntry : entry .getValue ().entrySet ()) {
320+ BlockifyPosition position = positionEntry .getKey ();
321+ BlockData blockData = positionEntry .getValue ();
322+
323+ highestZIndexes .get (chunk ).compute (position , (key , currentMaxZIndex ) -> {
324+ if (currentMaxZIndex == null || zIndex > currentMaxZIndex ) {
325+ // This view has a higher Z-index, so update the block data
326+ blockChanges .computeIfAbsent (chunk , k -> new ConcurrentHashMap <>()).put (position , blockData );
327+ return zIndex ;
328+ } else if (zIndex == currentMaxZIndex ) {
329+ // Z-index is the same, merge the blocks
330+ blockChanges .get (chunk ).put (position , blockData );
331+ return currentMaxZIndex ;
332+ }
333+ // This view has a lower Z-index, do nothing
334+ return currentMaxZIndex ;
335+ });
316336 }
317337 }
318338 }
0 commit comments