2012-04-19 17:12:49 +02:00
|
|
|
package com.Acrobot.ChestShop.Containers;
|
2011-05-15 18:16:25 +02:00
|
|
|
|
2011-07-05 19:08:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Utils.uBlock;
|
|
|
|
import com.Acrobot.ChestShop.Utils.uInventory;
|
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-04-19 17:12:49 +02:00
|
|
|
public class MinecraftChest implements Container {
|
2012-03-17 15:00:25 +01:00
|
|
|
private final Chest chest;
|
2012-05-10 16:32:25 +02:00
|
|
|
private final BlockFace[] neighborFaces = new BlockFace[]{BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
public MinecraftChest(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
|
|
|
}
|
|
|
|
|
2011-06-14 15:07:44 +02:00
|
|
|
public void addItem(ItemStack item, int amount) {
|
2012-03-17 15:00:25 +01:00
|
|
|
uInventory.add(chest.getInventory(), item, amount);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeItem(ItemStack item, short durability, int amount) {
|
2012-03-17 15:00:25 +01:00
|
|
|
uInventory.remove(chest.getInventory(), item, amount, durability);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int amount(ItemStack item, short durability) {
|
2012-03-17 15:00:25 +01:00
|
|
|
return uInventory.amount(chest.getInventory(), item, durability);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasEnough(ItemStack item, int amount, short durability) {
|
|
|
|
return amount(item, durability) >= amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean fits(ItemStack item, int amount, short durability) {
|
2012-03-17 15:00:25 +01:00
|
|
|
return uInventory.fits(chest.getInventory(), item, amount, durability) <= 0;
|
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();
|
|
|
|
|
|
|
|
for (BlockFace chestFace : neighborFaces) {
|
|
|
|
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
|
|
|
}
|