Check the block that the sign is attached to first (Fixes #97)

Also fix some oddities and duplicate code/method execution
This commit is contained in:
Phoenix616 2018-01-11 18:28:45 +01:00
parent 39443a2ad3
commit 0176ea1dc5
3 changed files with 58 additions and 25 deletions

View File

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

View File

@ -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);

View File

@ -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;
}
}