Skip to content

Commit

Permalink
add an option to make loot blocks immune to explosions
Browse files Browse the repository at this point in the history
  • Loading branch information
YouHaveTrouble committed Jun 27, 2024
1 parent 66b0208 commit 11453cd
Showing 1 changed file with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

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;
import org.bukkit.event.EventHandler;
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;
Expand All @@ -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";

Expand All @@ -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()
Expand All @@ -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) {
Expand All @@ -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;
}

}

0 comments on commit 11453cd

Please sign in to comment.