Skip to content

Commit 8a9a61b

Browse files
committed
Added zIndex
1 parent 625f657 commit 8a9a61b

5 files changed

Lines changed: 30 additions & 10 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'codes.kooper'
7-
version = '1.0.0-alpha-3'
7+
version = '1.0.0-alpha-4'
88

99
repositories {
1010
mavenCentral()

src/main/java/codes/kooper/blockify/managers/BlockChangeManager.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/java/codes/kooper/blockify/models/View.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class View {
1515
private final ConcurrentHashMap<BlockifyChunk, ConcurrentHashMap<BlockifyPosition, BlockData>> blocks;
1616
private final Stage stage;
1717
private final String name;
18+
private int zIndex;
1819
private boolean breakable, placeable;
1920
private Pattern pattern;
2021

@@ -32,6 +33,7 @@ public View(String name, Stage stage, Pattern pattern, boolean breakable) {
3233
this.stage = stage;
3334
this.breakable = breakable;
3435
this.pattern = pattern;
36+
this.zIndex = 0;
3537
}
3638

3739
/**

src/main/java/codes/kooper/blockify/protocol/BlockDigAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
4242
// Find the block in any stage and view using streams
4343
stages.stream()
4444
.flatMap(stage -> stage.getViews().stream())
45-
.filter(view -> view.hasBlock(position))
46-
.findFirst()
45+
.filter(view -> view.hasBlock(position)).min((view1, view2) -> Integer.compare(view2.getZIndex(), view1.getZIndex()))
4746
.ifPresent(view -> {
4847
// Get block data from view
4948
BlockData blockData = view.getBlock(position);
@@ -70,7 +69,6 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
7069

7170
// If block is not cancelled, break the block, otherwise, revert the block
7271
if (blockifyBreakEvent.isCancelled()) {
73-
System.out.println("block break event is cancelled");
7472
player.sendBlockChange(position.toLocation(player.getWorld()), blockData);
7573
view.setBlock(position, blockData);
7674
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Blockify
2-
version: '1.0.0-alpha-3'
2+
version: '1.0.0-alpha-4'
33
main: codes.kooper.blockify.Blockify
44
api-version: '1.20'
55
depend:

0 commit comments

Comments
 (0)