Skip to content

Commit e92daf2

Browse files
authored
Merge pull request #12 from Kooperlol/1.0.0
1.0.0
2 parents c7ea638 + 8a9a61b commit e92daf2

5 files changed

Lines changed: 41 additions & 20 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: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void sendBlockChanges(Stage stage, Audience audience, Collection<Blockify
148148
if (blockCount < 3000) {
149149
Map<Position, BlockData> multiBlockChange = new HashMap<>();
150150
for (BlockifyChunk chunk : chunks) {
151-
if (!stage.getWorld().isChunkLoaded(chunk.x(), chunk.z())) continue;
151+
if (!stage.getWorld().isChunkLoaded(chunk.x(), chunk.z()) || !blockChanges.containsKey(chunk)) continue;
152152
for (Map.Entry<BlockifyPosition, BlockData> entry : blockChanges.get(chunk).entrySet()) {
153153
multiBlockChange.put(entry.getKey().toPosition(), entry.getValue());
154154
}
@@ -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: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
3939

4040
BlockifyPosition position = new BlockifyPosition(wrapper.getBlockPosition().getX(), wrapper.getBlockPosition().getY(), wrapper.getBlockPosition().getZ());
4141

42-
// Loop through all stages and views to find the block
43-
for (Stage stage : stages) {
44-
for (View view : stage.getViews()) {
45-
if (view.hasBlock(position)) {
42+
// Find the block in any stage and view using streams
43+
stages.stream()
44+
.flatMap(stage -> stage.getViews().stream())
45+
.filter(view -> view.hasBlock(position)).min((view1, view2) -> Integer.compare(view2.getZIndex(), view1.getZIndex()))
46+
.ifPresent(view -> {
4647
// Get block data from view
4748
BlockData blockData = view.getBlock(position);
4849

@@ -58,24 +59,22 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
5859
// Block break functionality
5960
if (actionType == DiggingAction.FINISHED_DIGGING || canInstantBreak(player, blockData)) {
6061
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> {
61-
// Set block to air
62-
view.setBlock(position, Material.AIR.createBlockData());
63-
Blockify.getInstance().getBlockChangeManager().sendBlockChange(view.getStage(), view.getStage().getAudience(), position);
6462
// Call BlockifyBreakEvent
6563
BlockifyBreakEvent blockifyBreakEvent = new BlockifyBreakEvent(player, position, blockData, view, view.getStage());
6664
blockifyBreakEvent.callEvent();
65+
66+
// Set to air
67+
player.sendBlockChange(position.toLocation(player.getWorld()), Material.AIR.createBlockData());
68+
view.setBlock(position, Material.AIR.createBlockData());
69+
6770
// If block is not cancelled, break the block, otherwise, revert the block
6871
if (blockifyBreakEvent.isCancelled()) {
6972
player.sendBlockChange(position.toLocation(player.getWorld()), blockData);
7073
view.setBlock(position, blockData);
7174
}
7275
});
7376
}
74-
75-
return;
76-
}
77-
}
78-
}
77+
});
7978
}
8079
}
8180

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)