From c5a899b7c941c25ef1d7d2bad23576990205afde Mon Sep 17 00:00:00 2001 From: Emilia Kond Date: Wed, 7 Feb 2024 09:56:43 +0200 Subject: [PATCH] Don't allow dispensing items if redstone is disallowed Disallow removing items from dispensers and droppers by way of activating them and letting them shoot out their contents, if redstone isn't allowed for the dispenser or dropper. --- .../java/org/popcraft/bolt/BoltPlugin.java | 2 ++ .../bolt/listeners/BlockListener.java | 19 +++++++++++++++++++ .../adapter/BlockPreDispenseListener.java | 15 +++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 paper/src/main/java/org/popcraft/bolt/listeners/adapter/BlockPreDispenseListener.java diff --git a/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java b/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java index bb19b600..84085272 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java +++ b/bukkit/src/main/java/org/popcraft/bolt/BoltPlugin.java @@ -53,6 +53,7 @@ import org.popcraft.bolt.listeners.InventoryListener; import org.popcraft.bolt.listeners.PlayerListener; import org.popcraft.bolt.listeners.adapter.AnvilDamagedListener; +import org.popcraft.bolt.listeners.adapter.BlockPreDispenseListener; import org.popcraft.bolt.listeners.adapter.PlayerRecipeBookClickListener; import org.popcraft.bolt.matcher.Match; import org.popcraft.bolt.matcher.block.AmethystClusterMatcher; @@ -410,6 +411,7 @@ private void registerEvents() { pluginManager.registerEvents(blockListener, this); if (PaperUtil.isPaper()) { pluginManager.registerEvents(new PlayerRecipeBookClickListener(blockListener::onPlayerRecipeBookClick), this); + pluginManager.registerEvents(new BlockPreDispenseListener(blockListener::onBlockPreDispense), this); } pluginManager.registerEvents(new EntityListener(this), this); final InventoryListener inventoryListener = new InventoryListener(this); diff --git a/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java b/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java index 2316dc3e..c15dbb22 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java +++ b/bukkit/src/main/java/org/popcraft/bolt/listeners/BlockListener.java @@ -19,6 +19,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.block.BlockEvent; import org.bukkit.event.block.BlockExplodeEvent; import org.bukkit.event.block.BlockFadeEvent; import org.bukkit.event.block.BlockFormEvent; @@ -653,6 +654,10 @@ public void onBlockDispense(final BlockDispenseEvent e) { if (existingProtection == null) { return; } + if (!plugin.canAccess(existingProtection, REDSTONE_SOURCE_RESOLVER, Permission.REDSTONE)) { + e.setCancelled(true); + return; + } final BlockData blockData = block.getBlockData(); if (!(blockData instanceof final Directional directional)) { return; @@ -675,6 +680,20 @@ public void onBlockDispense(final BlockDispenseEvent e) { }); } + public void onBlockPreDispense(final BlockEvent e) { + if (!(e instanceof Cancellable cancellable)) { + return; + } + final Block block = e.getBlock(); + final Protection existingProtection = plugin.findProtection(block); + if (existingProtection == null) { + return; + } + if (!plugin.canAccess(existingProtection, REDSTONE_SOURCE_RESOLVER, Permission.REDSTONE)) { + cancellable.setCancelled(true); + } + } + @EventHandler public void onBlockReceiveGameEvent(final BlockReceiveGameEvent e) { if (!(e.getEntity() instanceof final Player player)) { diff --git a/paper/src/main/java/org/popcraft/bolt/listeners/adapter/BlockPreDispenseListener.java b/paper/src/main/java/org/popcraft/bolt/listeners/adapter/BlockPreDispenseListener.java new file mode 100644 index 00000000..f82c4b02 --- /dev/null +++ b/paper/src/main/java/org/popcraft/bolt/listeners/adapter/BlockPreDispenseListener.java @@ -0,0 +1,15 @@ +package org.popcraft.bolt.listeners.adapter; + +import io.papermc.paper.event.block.BlockPreDispenseEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockEvent; + +import java.util.function.Consumer; + +public record BlockPreDispenseListener(Consumer handler) implements Listener { + @EventHandler + public void onBlockPreDispense(final BlockPreDispenseEvent e) { + handler.accept(e); + } +}