Skip to content

Commit

Permalink
Added StructureGrowEvent listener to prevent block growth destroying …
Browse files Browse the repository at this point in the history
…post boxes
  • Loading branch information
shantek committed Sep 21, 2024
1 parent 6139db0 commit 7f00b4d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/io/shantek/listeners/BarrelProtection.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ProjectileHitEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.inventory.InventoryHolder;

import java.util.ArrayList;
import java.util.List;

public class BarrelProtection implements Listener {

private final PostOffice postOffice;
Expand Down Expand Up @@ -174,4 +178,30 @@ public void onProjectileHit(ProjectileHitEvent event) {
}
}
}

@EventHandler
public void onStructureGrow(StructureGrowEvent event) {

if (!postOffice.postBoxProtection) {
return;
}

// Create a list to store blocks to be removed
List<BlockState> blocksToRemove = new ArrayList<>();

// Iterate through the blocks the structure is going to replace
for (BlockState blockState : event.getBlocks()) {
Block block = blockState.getBlock();

// Check if the block is a protected post box (e.g., a sign attached to a barrel)
if (postOffice.helpers.isProtectedPostBox(block)) {
// Add the block to the list for removal
blocksToRemove.add(blockState);
}
}

// Remove the protected blocks from the event after iteration
event.getBlocks().removeAll(blocksToRemove);
}

}

0 comments on commit 7f00b4d

Please sign in to comment.