Skip to content

Commit 625f657

Browse files
committed
Dig adapter fixes
1 parent 786fa7f commit 625f657

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
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
}

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ 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))
46+
.findFirst()
47+
.ifPresent(view -> {
4648
// Get block data from view
4749
BlockData blockData = view.getBlock(position);
4850

@@ -58,24 +60,23 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
5860
// Block break functionality
5961
if (actionType == DiggingAction.FINISHED_DIGGING || canInstantBreak(player, blockData)) {
6062
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);
6463
// Call BlockifyBreakEvent
6564
BlockifyBreakEvent blockifyBreakEvent = new BlockifyBreakEvent(player, position, blockData, view, view.getStage());
6665
blockifyBreakEvent.callEvent();
66+
67+
// Set to air
68+
player.sendBlockChange(position.toLocation(player.getWorld()), Material.AIR.createBlockData());
69+
view.setBlock(position, Material.AIR.createBlockData());
70+
6771
// If block is not cancelled, break the block, otherwise, revert the block
6872
if (blockifyBreakEvent.isCancelled()) {
73+
System.out.println("block break event is cancelled");
6974
player.sendBlockChange(position.toLocation(player.getWorld()), blockData);
7075
view.setBlock(position, blockData);
7176
}
7277
});
7378
}
74-
75-
return;
76-
}
77-
}
78-
}
79+
});
7980
}
8081
}
8182

0 commit comments

Comments
 (0)