From c2ffe5e903e2c06f11ae242a2fb54f16e695e252 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 2 Mar 2022 14:05:20 +0100 Subject: [PATCH] Adds increased detection and optional refunding of signs broken by non-players --- README.md | 6 +- .../net/knarcraft/paidsigns/PaidSigns.java | 23 +++- .../listener/BlockBreakListener.java | 27 ----- .../paidsigns/listener/SignBreakListener.java | 103 ++++++++++++++++++ .../paidsigns/manager/TrackedSignManager.java | 5 +- src/main/resources/config.yml | 8 +- 6 files changed, 133 insertions(+), 39 deletions(-) delete mode 100644 src/main/java/net/knarcraft/paidsigns/listener/BlockBreakListener.java create mode 100644 src/main/java/net/knarcraft/paidsigns/listener/SignBreakListener.java diff --git a/README.md b/README.md index 99892b8..d5de68b 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ Removes a registered paid sign * ignoreColor - Whether to ignore any color or formatting applied to the text when trying to match a paid sign's text. The option can be set on a per-sign basis, but this value is used if not specified. The correct value depends on whether the plugin signs it should match allow coloring or not. -* enableRefunds - Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment +* refundsEnabled - Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment will always go to the original creator) -* refundPercentage - The percentage of the paid sign cost to refund (0-100) \ No newline at end of file +* refundPercentage - The percentage of the paid sign cost to refund (0-100) +* refundAlways - Whether to refund when signs that players have paid for are broken by anything. This includes tnt, + creepers, pistons and similar \ No newline at end of file diff --git a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java index 0b033ab..4616d5b 100644 --- a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java +++ b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java @@ -11,7 +11,7 @@ import net.knarcraft.paidsigns.command.RemoveConditionCommand; import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter; import net.knarcraft.paidsigns.command.RemoveTabCommand; import net.knarcraft.paidsigns.formatting.Translator; -import net.knarcraft.paidsigns.listener.BlockBreakListener; +import net.knarcraft.paidsigns.listener.SignBreakListener; import net.knarcraft.paidsigns.listener.SignListener; import net.knarcraft.paidsigns.manager.EconomyManager; import net.knarcraft.paidsigns.manager.PaidSignManager; @@ -37,8 +37,9 @@ public final class PaidSigns extends JavaPlugin { private String language; private boolean ignoreCase; private boolean ignoreColor; - private boolean enableRefunds; + private boolean refundsEnabled; private short refundPercentage; + private boolean refundAlways; /** * Instantiates a new paid signs object @@ -67,7 +68,7 @@ public final class PaidSigns extends JavaPlugin { PluginManager pluginManager = getServer().getPluginManager(); pluginManager.registerEvents(new SignListener(), this); - pluginManager.registerEvents(new BlockBreakListener(), this); + pluginManager.registerEvents(new SignBreakListener(), this); registerCommands(); } @@ -120,7 +121,7 @@ public final class PaidSigns extends JavaPlugin { * @return

Whether refunds are currently enabled

*/ public boolean areRefundsEnabled() { - return this.enableRefunds; + return this.refundsEnabled; } /** @@ -137,6 +138,15 @@ public final class PaidSigns extends JavaPlugin { return this.refundPercentage; } + /** + * Gets whether refunds should always happen, even if signs are not broken by players + * + * @return

True if refunds should always happen

+ */ + public boolean refundAlways() { + return this.refundAlways; + } + /** * Registers the commands used by this plugin */ @@ -175,11 +185,12 @@ public final class PaidSigns extends JavaPlugin { config.options().copyDefaults(true); this.saveDefaultConfig(); this.saveConfig(); + language = config.getString("language", "en"); ignoreCase = config.getBoolean("ignoreCase", true); ignoreColor = config.getBoolean("ignoreColor", false); - enableRefunds = config.getBoolean("enableRefunds", true); + refundsEnabled = config.getBoolean("refundsEnabled", true); refundPercentage = (short) config.getInt("refundPercentage", 100); - language = config.getString("language", "en"); + refundAlways = config.getBoolean("refundAlways", false); } /** diff --git a/src/main/java/net/knarcraft/paidsigns/listener/BlockBreakListener.java b/src/main/java/net/knarcraft/paidsigns/listener/BlockBreakListener.java deleted file mode 100644 index f761d21..0000000 --- a/src/main/java/net/knarcraft/paidsigns/listener/BlockBreakListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.knarcraft.paidsigns.listener; - -import net.knarcraft.paidsigns.manager.TrackedSignManager; -import org.bukkit.block.Sign; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockBreakEvent; - -import java.io.IOException; - -/** - * A listener that listens for any tracked signs being broken - */ -public class BlockBreakListener implements Listener { - - @EventHandler(priority = EventPriority.MONITOR) - public void onBlockBreak(BlockBreakEvent event) { - if (event.getBlock().getState() instanceof Sign) { - try { - TrackedSignManager.removeTrackedSign(event.getBlock().getLocation()); - } catch (IOException ignored) { - } - } - } - -} diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignBreakListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignBreakListener.java new file mode 100644 index 0000000..8e604e2 --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignBreakListener.java @@ -0,0 +1,103 @@ +package net.knarcraft.paidsigns.listener; + +import net.knarcraft.paidsigns.PaidSigns; +import net.knarcraft.paidsigns.manager.TrackedSignManager; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; +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.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityExplodeEvent; + +import java.io.IOException; +import java.util.List; + +/** + * A listener that listens for any tracked signs being broken + */ +public class SignBreakListener implements Listener { + + @EventHandler(priority = EventPriority.MONITOR) + public void onBlockBreak(BlockBreakEvent event) { + if (event.isCancelled()) { + return; + } + removeTrackedSign(event.getBlock(), true); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onExplosion(BlockExplodeEvent event) { + if (event.isCancelled()) { + return; + } + removeTrackedSigns(event.blockList()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onEntityExplosion(EntityExplodeEvent event) { + if (event.isCancelled()) { + return; + } + removeTrackedSigns(event.blockList()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPistonPush(BlockPistonExtendEvent event) { + if (event.isCancelled()) { + return; + } + removeTrackedSigns(event.getBlocks()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onPistonPull(BlockPistonRetractEvent event) { + if (event.isCancelled()) { + return; + } + removeTrackedSigns(event.getBlocks()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onEntityBlockChange(EntityChangeBlockEvent event) { + if (event.isCancelled()) { + return; + } + if (event.getEntity() instanceof Player) { + return; + } + removeTrackedSign(event.getBlock(), PaidSigns.getInstance().refundAlways()); + } + + /** + * Removes all tracked signs from the given blocks + * + * @param blocks

The blocks to search for tracked signs

+ */ + private void removeTrackedSigns(List blocks) { + for (Block block : blocks) { + removeTrackedSign(block, PaidSigns.getInstance().refundAlways()); + } + } + + /** + * Tries to remove any tracked sign at the given block + * + * @param block

The block that might be a sign

+ * @param refund

Whether to perform a refund after un-tracking the sign

+ */ + private void removeTrackedSign(Block block, boolean refund) { + if (block.getState() instanceof Sign) { + try { + TrackedSignManager.removeTrackedSign(block.getLocation(), refund); + } catch (IOException ignored) { + } + } + } + +} diff --git a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java index c697856..f9fc61a 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java @@ -49,16 +49,17 @@ public final class TrackedSignManager { * Removes a tracked sign from the manager * * @param signLocation

The location the sign was removed from

+ * @param refund

Whether to perform a refund after un-tracking the sign

* @throws IOException

If unable to save the tracked signs

*/ - public static void removeTrackedSign(Location signLocation) throws IOException { + public static void removeTrackedSign(Location signLocation, boolean refund) throws IOException { if (!trackedSigns.containsKey(signLocation)) { return; } TrackedSign trackedSign = trackedSigns.get(signLocation); trackedSigns.remove(signLocation); saveTrackedSigns(); - if (!PaidSigns.getInstance().areRefundsEnabled()) { + if (!PaidSigns.getInstance().areRefundsEnabled() || !refund) { return; } OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(trackedSign.getPlayerId()); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6e25191..5f2fd8f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -13,7 +13,11 @@ ignoreColor: false # Whether to enable refunds to the sign creator when a sign detected as a paid sign is broken (payment will always go # to the original creator) -enableRefunds: true +refundsEnabled: true # The percentage of the paid sign cost to refund (0-100) -refundPercentage: 100 \ No newline at end of file +refundPercentage: 100 + +# Whether to refund when signs that players have paid for are broken by anything. This includes tnt, creepers, pistons +# and similar +refundAlways: false \ No newline at end of file