2011-05-15 19:33:03 +02:00
|
|
|
package com.Acrobot.ChestShop.Chests;
|
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;
|
2011-05-15 18:16:25 +02:00
|
|
|
import org.bukkit.block.Chest;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
2011-05-29 13:25:25 +02:00
|
|
|
public class MinecraftChest implements ChestObject {
|
2011-07-23 21:00:47 +02:00
|
|
|
private final Chest main;
|
|
|
|
private final Chest neighbor;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
public MinecraftChest(Chest chest) {
|
2011-05-15 18:16:25 +02:00
|
|
|
this.main = chest;
|
|
|
|
this.neighbor = getNeighbor();
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
public ItemStack[] getContents() {
|
|
|
|
ItemStack[] contents = new ItemStack[(neighbor != null ? 54 : 27)];
|
|
|
|
ItemStack[] chest1 = main.getInventory().getContents();
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
System.arraycopy(chest1, 0, contents, 0, chest1.length);
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (neighbor != null) {
|
2011-05-15 18:16:25 +02:00
|
|
|
ItemStack[] chest2 = neighbor.getInventory().getContents();
|
|
|
|
System.arraycopy(chest2, 0, contents, chest1.length, chest2.length);
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-15 18:16:25 +02:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSlot(int slot, ItemStack item) {
|
2011-05-29 13:25:25 +02:00
|
|
|
if (slot < main.getInventory().getSize()) {
|
2011-05-15 18:16:25 +02:00
|
|
|
main.getInventory().setItem(slot, item);
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-15 18:16:25 +02:00
|
|
|
neighbor.getInventory().setItem(slot - main.getInventory().getSize(), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearSlot(int slot) {
|
2011-05-29 13:25:25 +02:00
|
|
|
if (slot < main.getInventory().getSize()) {
|
2011-05-15 18:16:25 +02:00
|
|
|
main.getInventory().setItem(slot, null);
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-15 18:16:25 +02:00
|
|
|
neighbor.getInventory().setItem(slot - main.getInventory().getSize(), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 15:07:44 +02:00
|
|
|
public void addItem(ItemStack item, int amount) {
|
2011-05-29 13:25:25 +02:00
|
|
|
int left = addItem(item, amount, main);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (neighbor != null && left > 0) addItem(item, left, neighbor);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removeItem(ItemStack item, short durability, int amount) {
|
|
|
|
int left = removeItem(item, durability, amount, main);
|
2011-07-23 21:00:47 +02:00
|
|
|
if (neighbor != null && left > 0) removeItem(item, durability, left, neighbor);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int amount(ItemStack item, short durability) {
|
|
|
|
return amount(item, durability, main) + (neighbor != null ? amount(item, durability, neighbor) : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasEnough(ItemStack item, int amount, short durability) {
|
|
|
|
return amount(item, durability) >= amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean fits(ItemStack item, int amount, short durability) {
|
2011-05-21 19:55:55 +02:00
|
|
|
int firstChest = fits(item, amount, durability, main);
|
2011-06-09 22:54:01 +02:00
|
|
|
return (firstChest > 0 && neighbor != null ? fits(item, firstChest, durability, neighbor) <= 0 : firstChest <= 0);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getSize() {
|
|
|
|
return main.getInventory().getSize() + (neighbor != null ? neighbor.getInventory().getSize() : 0);
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private Chest getNeighbor() {
|
2011-07-05 19:08:55 +02:00
|
|
|
return uBlock.findNeighbor(main);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private static int amount(ItemStack item, short durability, Chest chest) {
|
2011-07-05 19:08:55 +02:00
|
|
|
return uInventory.amount(chest.getInventory(), item, durability);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private static int fits(ItemStack item, int amount, short durability, Chest chest) {
|
2011-07-23 21:00:47 +02:00
|
|
|
return uInventory.fits(chest.getInventory(), item, amount, durability);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private static int addItem(ItemStack item, int amount, Chest chest) {
|
2011-07-23 21:00:47 +02:00
|
|
|
return uInventory.add(chest.getInventory(), item, amount);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
private static int removeItem(ItemStack item, short durability, int amount, Chest chest) {
|
2011-07-23 21:00:47 +02:00
|
|
|
return uInventory.remove(chest.getInventory(), item, amount, durability);
|
2011-05-15 18:16:25 +02:00
|
|
|
}
|
|
|
|
}
|