From ba6c5663553ca1eec149ae9fe20b53302a164e71 Mon Sep 17 00:00:00 2001 From: PyvesB Date: Fri, 14 Jul 2017 12:07:08 +0200 Subject: [PATCH] Improved hopper protection --- .../quickshop/Listeners/LockListener.java | 27 ++++----------- .../org/maxgamer/quickshop/Util/Util.java | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Listeners/LockListener.java b/src/main/java/org/maxgamer/quickshop/Listeners/LockListener.java index 8aa170f0a..3138ac7df 100644 --- a/src/main/java/org/maxgamer/quickshop/Listeners/LockListener.java +++ b/src/main/java/org/maxgamer/quickshop/Listeners/LockListener.java @@ -64,29 +64,16 @@ public class LockListener implements Listener { } catch (NoSuchFieldError er) { return; // Your server doesn't have hoppers } - Block c = e.getBlockAgainst(); - if (Util.canBeShop(c) == false) - return; Player p = e.getPlayer(); - Shop shop = plugin.getShopManager().getShop(c.getLocation()); - if (shop == null) { - c = Util.getSecondHalf(c); - if (c == null) - return; // You didn't place a hopper on a shop. Meh. - else - shop = plugin.getShopManager().getShop(c.getLocation()); - if (shop == null) - return; - } - if (p.getUniqueId().equals(shop.getOwner()) == false) { - if (p.hasPermission("quickshop.other.open")) { - p.sendMessage(MsgUtil.getMessage("bypassing-lock")); - return; - } - p.sendMessage(MsgUtil.getMessage("that-is-locked")); - e.setCancelled(true); + if (Util.isOtherShopWithinHopperReach(b, p) == false) + return; + + if (p.hasPermission("quickshop.other.open")) { + p.sendMessage(MsgUtil.getMessage("bypassing-lock")); return; } + p.sendMessage(MsgUtil.getMessage("that-is-locked")); + e.setCancelled(true); } /** diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index 3bc6f820f..e7e931fe9 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -31,6 +31,7 @@ import org.bukkit.material.Sign; import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffect; import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.Shop.Shop; @SuppressWarnings("deprecation") public class Util { @@ -317,6 +318,38 @@ public class Util { } return null; } + + /** + * Checks whether someone else's shop is within reach of a hopper being placed by a player. + * + * @param b + * The block being placed. + * @param p + * The player performing the action. + * @return true if a nearby shop was found, false otherwise. + */ + public static boolean isOtherShopWithinHopperReach(Block b, Player p) { + // Check 5 relative positions that can be affected by a hopper: behind, in front of, to the right, + // to the left and underneath. + Block[] blocks = new Block[5]; + blocks[0] = b.getRelative(0, 0, -1); + blocks[1] = b.getRelative(0, 0, 1); + blocks[2] = b.getRelative(1, 0, 0); + blocks[3] = b.getRelative(-1, 0, 0); + blocks[4] = b.getRelative(0, 1, 0); + for (Block c : blocks) { + Shop firstShop = plugin.getShopManager().getShop(c.getLocation()); + // If firstShop is null but is container, it can be used to drain contents from a shop created + // on secondHalf. + Block secondHalf = getSecondHalf(c); + Shop secondShop = secondHalf == null ? null : plugin.getShopManager().getShop(secondHalf.getLocation()); + if (firstShop != null && !p.getUniqueId().equals(firstShop.getOwner()) + || secondShop != null && !p.getUniqueId().equals(secondShop.getOwner())) { + return true; + } + } + return false; + } /** * Converts a string into an item from the database.