diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/BlockPlace.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/BlockPlace.java index a6a95d6..5999ea6 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Block/BlockPlace.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Block/BlockPlace.java @@ -9,7 +9,6 @@ import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.block.Chest; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -40,9 +39,9 @@ public class BlockPlace implements Listener { event.setCancelled(true); } - Chest neighbor = uBlock.findNeighbor(placed); + Block neighbor = uBlock.findNeighbor(placed); - if (neighbor != null && !Security.canAccess(event.getPlayer(), neighbor.getBlock())) { + if (neighbor != null && !Security.canAccess(event.getPlayer(), neighbor)) { event.getPlayer().sendMessage(Messages.prefix(Messages.ACCESS_DENIED)); event.setCancelled(true); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java index 151c35f..e37e886 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/ItemChecker.java @@ -8,6 +8,7 @@ import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.ChatColor; +import org.bukkit.block.Chest; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -39,8 +40,9 @@ public class ItemChecker implements Listener { if (item == null) { if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) { boolean foundItem = false; - if (uBlock.findConnectedChest(event.getSign()) != null) { - for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getInventory().getContents()) { + Chest chest = uBlock.findConnectedChest(event.getSign()); + if (chest != null) { + for (ItemStack stack : chest.getInventory().getContents()) { if (!MaterialUtil.isEmpty(stack)) { item = stack; itemCode = MaterialUtil.getSignName(stack); diff --git a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java index bfe376c..287e72b 100644 --- a/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java +++ b/src/main/java/com/Acrobot/ChestShop/Utils/uBlock.java @@ -6,6 +6,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; import org.bukkit.block.Sign; +import org.bukkit.material.Attachable; /** * @author Acrobot @@ -13,44 +14,70 @@ import org.bukkit.block.Sign; public class uBlock { public static final BlockFace[] CHEST_EXTENSION_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; public static final BlockFace[] SHOP_FACES = {BlockFace.SELF, BlockFace.DOWN, BlockFace.UP, BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; + @Deprecated public static final BlockFace[] NEIGHBOR_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; public static Sign getConnectedSign(Chest chest) { Sign sign = uBlock.findAnyNearbyShopSign(chest.getBlock()); - if (sign == null && getNeighbor(chest) != null) { - sign = uBlock.findAnyNearbyShopSign(getNeighbor(chest).getBlock()); + if (sign == null) { + Block neighbor = findNeighbor(chest.getBlock()); + if (neighbor != null) { + sign = uBlock.findAnyNearbyShopSign(neighbor); + } } return sign; } - - private static Chest getNeighbor(Chest chest) { - Block chestBlock = chest.getBlock(); - - for (BlockFace chestFace : NEIGHBOR_FACES) { - Block relative = chestBlock.getRelative(chestFace); - - if (relative.getType() == chestBlock.getType()) { - return (Chest) relative.getState(); + + public static Sign getConnectedSign(Block block) { + Sign sign = uBlock.findAnyNearbyShopSign(block); + + if (sign == null) { + Block neighbor = findNeighbor(block); + if (neighbor != null) { + sign = uBlock.findAnyNearbyShopSign(neighbor); } } - - return null; + + return sign; } public static Chest findConnectedChest(Sign sign) { - Block block = sign.getBlock(); - return findConnectedChest(block); + BlockFace signFace = null; + if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { + signFace = ((Attachable) sign.getData()).getAttachedFace(); + } + return findConnectedChest(sign.getBlock(), signFace); } public static Chest findConnectedChest(Block block) { - for (BlockFace bf : SHOP_FACES) { - Block faceBlock = block.getRelative(bf); + BlockFace signFace = null; + if (BlockUtil.isSign(block)) { + Sign sign = (Sign) block.getState(); + if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) { + signFace = ((Attachable) sign.getData()).getAttachedFace(); + } + } + return findConnectedChest(block, signFace); + } + + private static Chest findConnectedChest(Block block, BlockFace signFace) { + if (signFace != null ) { + Block faceBlock = block.getRelative(signFace); if (BlockUtil.isChest(faceBlock)) { return (Chest) faceBlock.getState(); } } + + for (BlockFace bf : SHOP_FACES) { + if (bf != signFace) { + Block faceBlock = block.getRelative(bf); + if (BlockUtil.isChest(faceBlock)) { + return (Chest) faceBlock.getState(); + } + } + } return null; } @@ -94,13 +121,18 @@ public class uBlock { } return null; } - - public static Chest findNeighbor(Block block) { + + public static Chest findNeighbor(Chest chest) { + Block neighbor = findNeighbor(chest.getBlock()); + return neighbor != null ? (Chest) neighbor.getState() : null; + } + + public static Block findNeighbor(Block block) { for (BlockFace blockFace : CHEST_EXTENSION_FACES) { Block neighborBlock = block.getRelative(blockFace); if (neighborBlock.getType() == block.getType()) { - return (Chest) neighborBlock.getState(); + return neighborBlock; } }