From 16592c595b2b83507bee615e06a25caeac9f788b Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 14 Jul 2024 16:02:00 -0700 Subject: [PATCH] Added CANDLES and BELL_RINGING flags and protections --- .../protection/BlockInteractionListener.java | 2 ++ .../flags/protection/CandleListener.java | 33 +++++++++++++++++++ .../PhysicalInteractionListener.java | 26 +++++++++++---- .../world/bentobox/bentobox/lists/Flags.java | 18 ++++++++++ src/main/resources/locales/en-US.yml | 8 +++++ 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/listeners/flags/protection/CandleListener.java diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java index aecaf82c0..496ae5238 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BlockInteractionListener.java @@ -100,9 +100,11 @@ public class BlockInteractionListener extends FlagListener switch (type) { case BEACON -> this.checkIsland(e, player, loc, Flags.BEACON); + case BELL -> this.checkIsland(e, player, loc, Flags.BELL_RINGING); case BREWING_STAND -> this.checkIsland(e, player, loc, Flags.BREWING); case BEEHIVE, BEE_NEST -> this.checkIsland(e, player, loc, Flags.HIVE); case BARREL -> this.checkIsland(e, player, loc, Flags.BARREL); + case CANDLE -> this.checkIsland(e, player, loc, Flags.CANDLES); case CHEST, CHEST_MINECART -> this.checkIsland(e, player, loc, Flags.CHEST); case TRAPPED_CHEST -> this.checkIsland(e, player, loc, Flags.TRAPPED_CHEST); case FLOWER_POT -> this.checkIsland(e, player, loc, Flags.FLOWER_POT); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/CandleListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/CandleListener.java new file mode 100644 index 000000000..73fe13c3b --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/CandleListener.java @@ -0,0 +1,33 @@ +package world.bentobox.bentobox.listeners.flags.protection; + +import org.bukkit.Tag; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerInteractEvent; + +import world.bentobox.bentobox.api.flags.FlagListener; +import world.bentobox.bentobox.lists.Flags; + +/** + * Protects candles + * @author tastybento + * @since 2.4.2 + */ +public class CandleListener extends FlagListener { + + /** + * Prevent dying signs. + * @param e - event + */ + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onCandleInteract(final PlayerInteractEvent e) { + if (e.getClickedBlock() == null || e.getItem() == null) { + return; + } + + if (Tag.CANDLES.isTagged(e.getClickedBlock().getType()) + || Tag.CANDLE_CAKES.isTagged(e.getClickedBlock().getType())) { + this.checkIsland(e, e.getPlayer(), e.getClickedBlock().getLocation(), Flags.CANDLES); + } + } +} diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListener.java index 3110c2a7e..11c3dbf57 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListener.java @@ -67,16 +67,25 @@ public class PhysicalInteractionListener extends FlagListener } } - private static final Map, Flag> TAG_TO_FLAG = Map.of(Tag.WOODEN_BUTTONS, Flags.BUTTON, - Tag.PRESSURE_PLATES, Flags.PRESSURE_PLATE, Tag.FENCE_GATES, Flags.GATE, Tag.DOORS, Flags.DOOR); + private boolean checkBlocks(Event e, Player player, Block block) { + Map, Flag> TAG_TO_FLAG = Map.of(Tag.WOODEN_BUTTONS, Flags.BUTTON, Tag.PRESSURE_PLATES, + Flags.PRESSURE_PLATE, Tag.FENCE_GATES, Flags.GATE, Tag.DOORS, Flags.DOOR, Tag.CANDLE_CAKES, + Flags.CANDLES, Tag.CANDLES, Flags.CANDLES); + Map MAT_TO_FLAG = Map.of(Material.LEVER, Flags.LEVER, Material.TRIPWIRE, Flags.REDSTONE, + Material.TARGET, Flags.REDSTONE); + boolean result = TAG_TO_FLAG.entrySet().stream().filter(entry -> entry.getKey().isTagged(block.getType())) + .findFirst().map(entry -> this.checkIsland(e, player, block.getLocation(), entry.getValue())) + .orElse(true); - private void checkBlocks(Event e, Player player, Block block) { - TAG_TO_FLAG.entrySet().stream().filter(entry -> entry.getKey().isTagged(block.getType())).findFirst() - .ifPresent(entry -> this.checkIsland(e, player, block.getLocation(), entry.getValue())); + if (result && MAT_TO_FLAG.containsKey(block.getType())) { + result = this.checkIsland(e, player, block.getLocation(), MAT_TO_FLAG.get(block.getType())); + } + + return result; } /** - * Protects buttons and plates from being activated by projectiles that explode + * Protects buttons and plates, etc. from being activated by projectiles that explode * @param e - event */ @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) @@ -84,6 +93,11 @@ public class PhysicalInteractionListener extends FlagListener if (e.getEntity() instanceof Projectile p && p.getShooter() instanceof Player player) { for (Block b : e.blockList()) { this.checkBlocks(e, player, b); + /* + * TODO: + * Add protection for candles + * + */ } } } diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index d0f5c75b5..33f345d65 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -19,6 +19,7 @@ import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListen import world.bentobox.bentobox.listeners.flags.protection.BreakBlocksListener; import world.bentobox.bentobox.listeners.flags.protection.BreedingListener; import world.bentobox.bentobox.listeners.flags.protection.BucketListener; +import world.bentobox.bentobox.listeners.flags.protection.CandleListener; import world.bentobox.bentobox.listeners.flags.protection.DyeListener; import world.bentobox.bentobox.listeners.flags.protection.EggListener; import world.bentobox.bentobox.listeners.flags.protection.ElytraListener; @@ -687,6 +688,23 @@ public final class Flags { */ public static final Flag SIGN_EDITING = new Flag.Builder("SIGN_EDITING", Material.DARK_OAK_SIGN).mode(Flag.Mode.BASIC).type(Type.PROTECTION).build(); + /** + * Bell ringing protection + * Listeners are {@link BlockInteractionListener} and {@link PhysicalInteractionListener} + * @since 2.4.2 + */ + public static final Flag BELL_RINGING = new Flag.Builder("BELL_RINGING", Material.BELL).mode(Flag.Mode.EXPERT) + .type(Type.PROTECTION).build(); + + /** + * Candle protection + * Listener is {@link CandleListener} + * @since 2.4.2 + */ + public static final Flag CANDLES = new Flag.Builder("CANDLES", Material.CANDLE).mode(Flag.Mode.EXPERT) + .listener(new CandleListener()) + .type(Type.PROTECTION).build(); + /** * Provides a list of all the Flag instances contained in this class using reflection. * Deprecated Flags are ignored. diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 7989f41ed..47c5f27bc 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -912,6 +912,10 @@ protection: description: Toggle interaction name: Beacons hint: Beacon use disabled + BELL_RINGING: + description: Toggle interaction + name: Allow bell ringing + hint: Bell ringing disabled BED: description: Toggle interaction name: Beds @@ -960,6 +964,10 @@ protection: description: Toggle button use name: Buttons hint: Button use disabled + CANDLES: + description: Toggle candle interaction + name: Candles + hint: Candle interaction disabled CAKE: description: Toggle cake interaction name: Cakes