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

92 lines
3.1 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;
import java.util.HashMap;
2011-06-09 22:54:01 +02:00
/**
* @author Acrobot
*/
public class uInventory {
2011-05-29 13:25:25 +02:00
public static int remove(Inventory inv, ItemStack item, int amount, short durability) {
amount = (amount > 0 ? amount : 1);
Material itemMaterial = item.getType();
int first = inv.first(itemMaterial);
if (first == -1) return amount;
for (int slot = first; slot < inv.getSize(); slot++) {
if (amount <= 0) return 0;
ItemStack currentItem = inv.getItem(slot);
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();
if (amount == currentAmount) {
currentItem = null;
amount = 0;
} else if (amount < currentAmount) {
currentItem.setAmount(currentAmount - amount);
amount = 0;
2011-05-29 13:25:25 +02:00
} else {
currentItem = null;
amount -= currentAmount;
}
inv.setItem(slot, currentItem);
}
}
return amount;
}
2011-05-29 13:25:25 +02:00
public static int add(Inventory inv, ItemStack item, int amount) {
amount = (amount > 0 ? amount : 1);
2011-07-02 20:34:14 +02:00
ItemStack itemToAdd = new ItemStack(item.getType(), amount, item.getDurability());
HashMap<Integer, ItemStack> items = inv.addItem(itemToAdd);
2011-06-09 22:54:01 +02:00
amount = 0;
for(ItemStack toAdd : items.values()){
amount += toAdd.getAmount();
}
2011-06-09 22:54:01 +02:00
return amount;
}
2011-05-29 13:25:25 +02:00
public static int amount(Inventory inv, ItemStack item, short durability) {
if (!inv.contains(item.getType())) return 0;
int amount = 0;
for (ItemStack i : inv.getContents()) {
if (i != null && 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();
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;
}
}