Improved hopper protection

This commit is contained in:
PyvesB committed 2017-07-14 12:07:08 +02:00
1 parent 555466bc43
commit ba6c566355
2 files changed
+40 -20

No files matched your search

@@ -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);
}
/**
@@ -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.