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.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -40,9 +39,9 @@ public class BlockPlace implements Listener {
event.setCancelled(true); 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.getPlayer().sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
event.setCancelled(true); 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.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock; import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.block.Chest;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -39,8 +40,9 @@ public class ItemChecker implements Listener {
if (item == null) { if (item == null) {
if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) { if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) {
boolean foundItem = false; boolean foundItem = false;
if (uBlock.findConnectedChest(event.getSign()) != null) { Chest chest = uBlock.findConnectedChest(event.getSign());
for (ItemStack stack : uBlock.findConnectedChest(event.getSign()).getInventory().getContents()) { if (chest != null) {
for (ItemStack stack : chest.getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack)) { if (!MaterialUtil.isEmpty(stack)) {
item = stack; item = stack;
itemCode = MaterialUtil.getSignName(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.BlockFace;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.material.Attachable;
/** /**
* @author Acrobot * @author Acrobot
@ -13,44 +14,70 @@ import org.bukkit.block.Sign;
public class uBlock { public class uBlock {
public static final BlockFace[] CHEST_EXTENSION_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH}; 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}; 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 final BlockFace[] NEIGHBOR_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
public static Sign getConnectedSign(Chest chest) { public static Sign getConnectedSign(Chest chest) {
Sign sign = uBlock.findAnyNearbyShopSign(chest.getBlock()); Sign sign = uBlock.findAnyNearbyShopSign(chest.getBlock());
if (sign == null && getNeighbor(chest) != null) { if (sign == null) {
sign = uBlock.findAnyNearbyShopSign(getNeighbor(chest).getBlock()); Block neighbor = findNeighbor(chest.getBlock());
if (neighbor != null) {
sign = uBlock.findAnyNearbyShopSign(neighbor);
}
} }
return sign; return sign;
} }
private static Chest getNeighbor(Chest chest) { public static Sign getConnectedSign(Block block) {
Block chestBlock = chest.getBlock(); Sign sign = uBlock.findAnyNearbyShopSign(block);
for (BlockFace chestFace : NEIGHBOR_FACES) { if (sign == null) {
Block relative = chestBlock.getRelative(chestFace); Block neighbor = findNeighbor(block);
if (neighbor != null) {
if (relative.getType() == chestBlock.getType()) { sign = uBlock.findAnyNearbyShopSign(neighbor);
return (Chest) relative.getState();
} }
} }
return null; return sign;
} }
public static Chest findConnectedChest(Sign sign) { public static Chest findConnectedChest(Sign sign) {
Block block = sign.getBlock(); BlockFace signFace = null;
return findConnectedChest(block); if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
}
return findConnectedChest(sign.getBlock(), signFace);
} }
public static Chest findConnectedChest(Block block) { public static Chest findConnectedChest(Block block) {
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) { for (BlockFace bf : SHOP_FACES) {
if (bf != signFace) {
Block faceBlock = block.getRelative(bf); Block faceBlock = block.getRelative(bf);
if (BlockUtil.isChest(faceBlock)) { if (BlockUtil.isChest(faceBlock)) {
return (Chest) faceBlock.getState(); return (Chest) faceBlock.getState();
} }
} }
}
return null; return null;
} }
@ -95,12 +122,17 @@ public class uBlock {
return null; 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) { for (BlockFace blockFace : CHEST_EXTENSION_FACES) {
Block neighborBlock = block.getRelative(blockFace); Block neighborBlock = block.getRelative(blockFace);
if (neighborBlock.getType() == block.getType()) { if (neighborBlock.getType() == block.getType()) {
return (Chest) neighborBlock.getState(); return neighborBlock;
} }
} }