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;
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2011-05-21 19:55:55 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
Material itemMaterial = item.getType();
|
|
|
|
int maxStackSize = itemMaterial.getMaxStackSize();
|
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
if (amount <= maxStackSize) {
|
|
|
|
item.setAmount(amount);
|
|
|
|
inv.addItem(item);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<ItemStack> items = new ArrayList<ItemStack>();
|
|
|
|
for (int i = 0; i < Math.ceil(amount / maxStackSize); i++) {
|
|
|
|
if (amount <= maxStackSize) {
|
|
|
|
item.setAmount(amount);
|
|
|
|
items.add(item);
|
2011-05-21 19:55:55 +02:00
|
|
|
return 0;
|
2011-06-09 22:54:01 +02:00
|
|
|
} else {
|
|
|
|
item.setAmount(maxStackSize);
|
|
|
|
items.add(item);
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
2011-06-09 22:54:01 +02:00
|
|
|
Object[] iArray = items.toArray();
|
|
|
|
|
|
|
|
amount = 0;
|
|
|
|
for (Object o : iArray) {
|
|
|
|
ItemStack itemToAdd = (ItemStack) o;
|
|
|
|
amount += (!inv.addItem(itemToAdd).isEmpty() ? itemToAdd.getAmount() : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount;
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
|
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) {
|
2011-06-09 22:54:01 +02:00
|
|
|
if (i.getType() == item.getType() && (durability == -1 || i.getDurability() == durability)) {
|
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
|
|
|
|
2011-06-09 22:54:01 +02:00
|
|
|
if (currentAmount != maxStackSize && currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
|
|
|
|
amountLeft = ((currentAmount + amountLeft) <= maxStackSize ? 0 : amountLeft - (maxStackSize - currentAmount));
|
2011-05-21 19:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountLeft;
|
|
|
|
}
|
|
|
|
}
|