diff --git a/src/main/java/org/purpurmc/purpurextras/modules/LootBlocksProtectionModule.java b/src/main/java/org/purpurmc/purpurextras/modules/LootBlocksProtectionModule.java index 80db909..94d2318 100644 --- a/src/main/java/org/purpurmc/purpurextras/modules/LootBlocksProtectionModule.java +++ b/src/main/java/org/purpurmc/purpurextras/modules/LootBlocksProtectionModule.java @@ -2,6 +2,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.DecoratedPot; import org.bukkit.block.Vault; @@ -9,6 +10,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockExplodeEvent; +import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.loot.Lootable; import org.bukkit.permissions.PermissionDefault; import org.bukkit.util.permissions.DefaultPermissions; @@ -24,6 +27,7 @@ public class LootBlocksProtectionModule implements PurpurExtrasModule, Listener private final Component message; private MessageType messageType; private final boolean allowBreakingInSneak; + private final boolean blocksImmuneToExplosions; private final String permission = "purpurextras.lootblockprotectionbypass"; @@ -41,6 +45,7 @@ protected LootBlocksProtectionModule() { config.getString("settings.protect-blocks-with-loot.message", defaultMessage) ); allowBreakingInSneak = config.getBoolean("settings.protect-blocks-with-loot.allow-breaking-in-sneak", true); + blocksImmuneToExplosions = config.getBoolean("settings.protect-blocks-with-loot.immune-to-explosions", false); try { messageType = MessageType.valueOf( config.getString("settings.protect-blocks-with-loot.message-type", "CHAT").toUpperCase() @@ -64,17 +69,21 @@ public boolean shouldEnable() { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onDestroyBlockWithLoot(BlockBreakEvent event) { // decorated pots are only lootable by breaking them - if (event.getBlock().getState() instanceof DecoratedPot) return; - if (event.getBlock().getState() instanceof Lootable lootable) { - if (!lootable.hasLootTable()) return; + if (isProtected(event.getBlock().getState(true))) { handleLootBlockDestruction(event); - return; - } - if (event.getBlock().getState() instanceof Vault) { - handleLootBlockDestruction(event); - return; } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onExplodingBlockWithLoot(EntityExplodeEvent event) { + if (!blocksImmuneToExplosions) return; + event.blockList().removeIf(block -> isProtected(block.getState(true))); + } + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onExplodingBlockWithLoot(BlockExplodeEvent event) { + if (!blocksImmuneToExplosions) return; + event.blockList().removeIf(block -> isProtected(block.getState(true))); } private void handleLootBlockDestruction(BlockBreakEvent event) { @@ -87,4 +96,13 @@ private void handleLootBlockDestruction(BlockBreakEvent event) { } } + private boolean isProtected(BlockState blockState) { + if (blockState instanceof DecoratedPot) return false; + if (blockState instanceof Lootable lootable) { + return lootable.hasLootTable(); + } + if (blockState instanceof Vault) return true; + return false; + } + }