ChestShop-3/com/Acrobot/ChestShop/Utils/InventoryUtil.java

118 lines
3.7 KiB
Java
Raw Normal View History

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;
/**
* @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) {
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) {
return 0;
}
ItemStack currentItem = inv.getItem(slot);
2011-05-29 13:25:25 +02:00
if (currentItem == null || currentItem.getType() == Material.AIR) {
continue;
}
2011-05-29 13:25:25 +02:00
if (currentItem.getType() == itemMaterial && (durability == -1 || currentItem.getDurability() == durability)) {
int currentAmount = currentItem.getAmount();
2011-05-29 13:25:25 +02:00
if (amountLeft == currentAmount) {
currentItem = null;
amountLeft = 0;
2011-05-29 13:25:25 +02:00
} else if (amountLeft < currentAmount) {
currentItem.setAmount(currentAmount - amountLeft);
amountLeft = 0;
2011-05-29 13:25:25 +02:00
} else {
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) {
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);
return 0;
2011-06-09 22:54:01 +02:00
} else {
item.setAmount(maxStackSize);
items.add(item);
}
}
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-29 13:25:25 +02:00
public static int amount(Inventory inv, ItemStack item, short durability) {
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)) {
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) {
Material itemMaterial = item.getType();
int maxStackSize = itemMaterial.getMaxStackSize();
2011-05-29 13:25:25 +02:00
int amountLeft = amount;
2011-05-29 13:25:25 +02:00
for (ItemStack currentItem : inv.getContents()) {
if (amountLeft <= 0) {
return 0;
}
2011-05-29 13:25:25 +02:00
if (currentItem == null || currentItem.getType() == Material.AIR) {
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));
}
}
return amountLeft;
}
}