2011-05-21 19:55:55 +02:00
|
|
|
package com.Acrobot.ChestShop.Utils;
|
|
|
|
|
|
|
|
import org.bukkit.Material;
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class InventoryUtil {
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int remove(Inventory inv, ItemStack item, int amount, short durability) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Material itemMaterial = item.getType();
|
|
|
|
int amountLeft = amount;
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
for (int slot = 0; slot < inv.getSize(); slot++) {
|
|
|
|
if (amountLeft <= 0) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemStack currentItem = inv.getItem(slot);
|
2011-05-29 13:25:25 +02:00
|
|
|
if (currentItem == null || currentItem.getType() == Material.AIR) {
|
2011-05-21 19:55:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
int currentAmount = currentItem.getAmount();
|
2011-05-29 13:25:25 +02:00
|
|
|
if (amountLeft == currentAmount) {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentItem = null;
|
|
|
|
amountLeft = 0;
|
2011-05-29 13:25:25 +02:00
|
|
|
} else if (amountLeft < currentAmount) {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentItem.setAmount(currentAmount - amountLeft);
|
|
|
|
amountLeft = 0;
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentItem = null;
|
|
|
|
amountLeft -= currentAmount;
|
|
|
|
}
|
|
|
|
inv.setItem(slot, currentItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountLeft;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int add(Inventory inv, ItemStack item, int amount) {
|
2011-05-21 19:55:55 +02:00
|
|
|
ItemStack[] contents = inv.getContents();
|
|
|
|
Material itemMaterial = item.getType();
|
|
|
|
short durability = item.getDurability();
|
|
|
|
|
|
|
|
int amountLeft = amount;
|
|
|
|
int maxStackSize = itemMaterial.getMaxStackSize();
|
|
|
|
ItemStack baseItem = item.clone();
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
for (int slot = 0; slot < contents.length; slot++) {
|
2011-05-21 19:55:55 +02:00
|
|
|
ItemStack itemStack = contents[slot];
|
2011-05-29 13:25:25 +02:00
|
|
|
if (amountLeft <= 0) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
if (itemStack != null && itemStack.getType() != Material.AIR) { //Our slot is not free
|
2011-05-21 19:55:55 +02:00
|
|
|
int currentAmount = itemStack.getAmount();
|
|
|
|
Material currentMaterial = itemStack.getType();
|
|
|
|
short currentDurability = itemStack.getDurability();
|
2011-05-29 13:25:25 +02:00
|
|
|
if (currentMaterial == itemMaterial && (currentDurability == durability)) {
|
|
|
|
if ((currentAmount + amountLeft) <= maxStackSize) {
|
2011-05-21 19:55:55 +02:00
|
|
|
baseItem.setAmount(currentAmount + amountLeft);
|
|
|
|
amountLeft = 0;
|
2011-05-29 13:25:25 +02:00
|
|
|
} else {
|
2011-05-21 19:55:55 +02:00
|
|
|
baseItem.setAmount(maxStackSize);
|
|
|
|
amountLeft -= (maxStackSize - currentAmount);
|
|
|
|
}
|
|
|
|
inv.setItem(slot, baseItem);
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
} else { //Free slot
|
|
|
|
if (amountLeft <= maxStackSize) { //There is less to add than whole stack
|
2011-05-21 19:55:55 +02:00
|
|
|
baseItem.setAmount(amountLeft);
|
|
|
|
inv.setItem(slot, baseItem);
|
|
|
|
amountLeft = 0;
|
2011-05-29 13:25:25 +02:00
|
|
|
} else { //We add whole stack
|
2011-05-21 19:55:55 +02:00
|
|
|
baseItem.setAmount(maxStackSize);
|
|
|
|
inv.setItem(slot, baseItem);
|
|
|
|
amountLeft -= maxStackSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return amountLeft;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int amount(Inventory inv, ItemStack item, short durability) {
|
2011-05-21 19:55:55 +02:00
|
|
|
int amount = 0;
|
|
|
|
ItemStack[] contents = inv.getContents();
|
2011-05-29 13:25:25 +02:00
|
|
|
for (ItemStack i : contents) {
|
|
|
|
if (i != null) {
|
|
|
|
if (i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability || (durability == 0 && i.getDurability() == -1))) {
|
2011-05-21 19:55:55 +02:00
|
|
|
amount += i.getAmount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
public static int fits(Inventory inv, ItemStack item, int amount, short durability) {
|
2011-05-21 19:55:55 +02:00
|
|
|
Material itemMaterial = item.getType();
|
|
|
|
int maxStackSize = itemMaterial.getMaxStackSize();
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
int amountLeft = amount;
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
for (ItemStack currentItem : inv.getContents()) {
|
|
|
|
if (amountLeft <= 0) {
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-29 13:25:25 +02:00
|
|
|
if (currentItem == null || currentItem.getType() == Material.AIR) {
|
2011-05-21 19:55:55 +02:00
|
|
|
amountLeft -= maxStackSize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int currentAmount = currentItem.getAmount();
|
2011-05-29 13:25:25 +02:00
|
|
|
if (currentAmount == itemMaterial.getMaxStackSize()) {
|
2011-05-21 19:55:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
|
2011-05-21 19:55:55 +02:00
|
|
|
currentAmount = currentAmount < 1 ? 1 : currentAmount;
|
|
|
|
amountLeft = (amountLeft <= currentAmount ? 0 : currentAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountLeft;
|
|
|
|
}
|
|
|
|
}
|