ChestShop-3/com/Acrobot/ChestShop/Utils/uBlock.java

111 lines
3.4 KiB
Java
Raw Normal View History

2011-05-15 19:33:03 +02:00
package com.Acrobot.ChestShop.Utils;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Signs.RestrictedSign;
2011-05-15 19:33:03 +02:00
import org.bukkit.Material;
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;
2011-05-15 19:33:03 +02:00
/**
* @author Acrobot
*/
public class uBlock {
private static final BlockFace[] CHEST_EXTENSION_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
private static final BlockFace[] SHOP_FACES = {BlockFace.SELF, BlockFace.DOWN, BlockFace.UP, BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
2011-05-15 19:33:03 +02:00
public static Chest findConnectedChest(Sign sign) {
2011-05-15 19:33:03 +02:00
Block block = sign.getBlock();
return findConnectedChest(block);
2011-05-15 19:33:03 +02:00
}
public static Chest findConnectedChest(Block block) {
for (BlockFace bf : SHOP_FACES) {
Block faceBlock = block.getRelative(bf);
if (faceBlock.getType() == Material.CHEST) {
return (Chest) faceBlock.getState();
}
2011-05-15 19:33:03 +02:00
}
return null;
}
public static Sign findValidShopSign(Block block, String originalName) {
Sign ownerShopSign = null;
2011-05-15 19:33:03 +02:00
for (BlockFace bf : SHOP_FACES) {
Block faceBlock = block.getRelative(bf);
if (!BlockUtil.isSign(faceBlock)) {
continue;
}
Sign sign = (Sign) faceBlock.getState();
if (ChestShopSign.isValid(sign) && signIsAttachedToBlock(sign, block)) {
if (!sign.getLine(0).equals(originalName)) {
return sign;
} else if (ownerShopSign == null) {
ownerShopSign = sign;
}
}
}
return ownerShopSign;
}
public static Sign findAnyNearbyShopSign(Block block) {
for (BlockFace bf : SHOP_FACES) {
Block faceBlock = block.getRelative(bf);
if (!BlockUtil.isSign(faceBlock)) {
continue;
}
Sign sign = (Sign) faceBlock.getState();
if (ChestShopSign.isValid(sign)) {
return sign;
2011-05-15 19:33:03 +02:00
}
}
return null;
}
2011-06-09 22:54:01 +02:00
public static Sign findRestrictedSign(Block block) {
for (BlockFace bf : SHOP_FACES) {
Block faceBlock = block.getRelative(bf);
if (!BlockUtil.isSign(faceBlock)) {
continue;
}
Sign sign = (Sign) faceBlock.getState();
if (RestrictedSign.isRestricted(sign) && signIsAttachedToBlock(sign, block)) {
return sign;
}
}
return null;
}
public static Chest findNeighbor(Block block) {
for (BlockFace blockFace : CHEST_EXTENSION_FACES) {
Block neighborBlock = block.getRelative(blockFace);
2011-05-29 13:25:25 +02:00
if (neighborBlock.getType() == Material.CHEST) {
return (Chest) neighborBlock.getState();
}
}
return null;
2011-05-29 13:25:25 +02:00
}
private static boolean signIsAttachedToBlock(Sign sign, Block block) {
return sign.getBlock().equals(block) || getAttachedFace(sign).equals(block);
}
public static Block getAttachedFace(Sign sign) {
return sign.getBlock().getRelative(((Attachable) sign.getData()).getAttachedFace());
}
2011-05-15 19:33:03 +02:00
}