2012-04-19 17:12:49 +02:00
|
|
|
package com.Acrobot.ChestShop.Containers;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.block.Block;
|
|
|
|
import org.bukkit.block.BlockFace;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Chest;
|
2012-05-10 16:32:25 +02:00
|
|
|
import org.bukkit.block.Sign;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2012-06-08 15:28:36 +02:00
|
|
|
public class ShopChest implements Container {
|
2012-03-17 15:00:25 +01:00
|
|
|
private final Chest chest;
|
2012-06-08 15:28:36 +02:00
|
|
|
private static final BlockFace[] NEIGHBOR_FACES = new BlockFace[]{BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public ShopChest(Chest chest) {
|
2012-03-17 15:00:25 +01:00
|
|
|
this.chest = chest;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public boolean isEmpty() {
|
|
|
|
for (ItemStack item : chest.getInventory()) {
|
|
|
|
if (item != null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
return true;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public void addItem(ItemStack item) {
|
|
|
|
InventoryUtil.add(item, chest.getInventory());
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public void removeItem(ItemStack item) {
|
|
|
|
InventoryUtil.remove(item, chest.getInventory());
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public int amount(ItemStack item) {
|
|
|
|
return InventoryUtil.getAmount(item, chest.getInventory());
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public boolean hasEnough(ItemStack item) {
|
|
|
|
return amount(item) >= item.getAmount();
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public boolean fits(ItemStack item) {
|
|
|
|
return InventoryUtil.fits(item, chest.getInventory());
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
public Sign findShopSign() {
|
|
|
|
Sign sign = uBlock.findAnyNearbyShopSign(chest.getBlock());
|
|
|
|
|
|
|
|
if (sign == null && getNeighbor() != null) {
|
|
|
|
sign = uBlock.findAnyNearbyShopSign(getNeighbor().getBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
return sign;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 16:32:25 +02:00
|
|
|
private Chest getNeighbor() {
|
|
|
|
Block chestBlock = chest.getBlock();
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
for (BlockFace chestFace : NEIGHBOR_FACES) {
|
2012-05-10 16:32:25 +02:00
|
|
|
Block relative = chestBlock.getRelative(chestFace);
|
|
|
|
|
|
|
|
if (relative.getState() instanceof Chest) {
|
|
|
|
return (Chest) relative.getState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
2012-03-17 15:00:25 +01:00
|
|
|
}
|