2012-06-08 15:28:36 +02:00
|
|
|
package com.Acrobot.Breeze.Utils;
|
|
|
|
|
|
|
|
import org.bukkit.inventory.Inventory;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2012-11-23 21:01:29 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2012-06-08 15:28:36 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class InventoryUtil {
|
|
|
|
/**
|
|
|
|
* Returns the amount of the item inside the inventory
|
|
|
|
*
|
|
|
|
* @param item Item to check
|
|
|
|
* @param inventory inventory
|
|
|
|
* @return amount of the item
|
|
|
|
*/
|
|
|
|
public static int getAmount(ItemStack item, Inventory inventory) {
|
|
|
|
if (!inventory.contains(item.getType())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
if (inventory.getType() == null) {
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
|
|
|
|
int itemAmount = 0;
|
|
|
|
|
|
|
|
for (ItemStack iStack : items.values()) {
|
|
|
|
if (!MaterialUtil.equals(iStack, item)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemAmount += iStack.getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemAmount;
|
|
|
|
}
|
|
|
|
|
2012-10-16 17:03:45 +02:00
|
|
|
/**
|
|
|
|
* Tells if the inventory is empty
|
|
|
|
*
|
|
|
|
* @param inventory inventory
|
|
|
|
* @return Is the inventory empty?
|
|
|
|
*/
|
|
|
|
public static boolean isEmpty(Inventory inventory) {
|
|
|
|
for (ItemStack stack : inventory.getContents()) {
|
|
|
|
if (!MaterialUtil.isEmpty(stack)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the inventory has stock of this type
|
|
|
|
*
|
|
|
|
* @param items items
|
|
|
|
* @param inventory inventory
|
|
|
|
* @return Does the inventory contain stock of this type?
|
|
|
|
*/
|
|
|
|
public static boolean hasItems(ItemStack[] items, Inventory inventory) {
|
|
|
|
for (ItemStack item : items) {
|
2012-12-22 21:04:15 +01:00
|
|
|
if (!inventory.containsAtLeast(item, item.getAmount())) {
|
2012-10-16 17:03:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
/**
|
|
|
|
* Checks if the item fits the inventory
|
|
|
|
*
|
|
|
|
* @param item Item to check
|
|
|
|
* @param inventory inventory
|
|
|
|
* @return Does item fit inside inventory?
|
|
|
|
*/
|
|
|
|
public static boolean fits(ItemStack item, Inventory inventory) {
|
|
|
|
int left = item.getAmount();
|
|
|
|
|
2012-11-30 21:50:03 +01:00
|
|
|
if (inventory.getMaxStackSize() == Integer.MAX_VALUE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
for (ItemStack iStack : inventory.getContents()) {
|
|
|
|
if (left <= 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-16 17:03:45 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
if (MaterialUtil.isEmpty(iStack)) {
|
2012-11-30 21:50:03 +01:00
|
|
|
left -= item.getMaxStackSize();
|
2012-06-08 15:28:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MaterialUtil.equals(iStack, item)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= (iStack.getMaxStackSize() - iStack.getAmount());
|
|
|
|
}
|
|
|
|
|
|
|
|
return left <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an item to the inventory
|
2012-11-29 20:28:05 +01:00
|
|
|
* (it currently uses a custom method of adding items, because Bukkit hasn't fixed it for 6 months now)
|
2012-06-08 15:28:36 +02:00
|
|
|
*
|
|
|
|
* @param item Item to add
|
|
|
|
* @param inventory Inventory
|
|
|
|
* @return Number of leftover items
|
|
|
|
*/
|
|
|
|
public static int add(ItemStack item, Inventory inventory) {
|
2012-11-29 20:28:05 +01:00
|
|
|
if (item.getAmount() < 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-08 15:28:36 +02:00
|
|
|
|
2012-11-29 20:28:05 +01:00
|
|
|
int amountLeft = item.getAmount();
|
|
|
|
int maxStackSize = item.getMaxStackSize();
|
|
|
|
|
|
|
|
for (int currentSlot = 0; currentSlot < inventory.getSize() && amountLeft > 0; currentSlot++) {
|
|
|
|
ItemStack currentItem = inventory.getItem(currentSlot);
|
|
|
|
ItemStack duplicate = item.clone();
|
|
|
|
|
|
|
|
if (MaterialUtil.isEmpty(currentItem)) {
|
2012-12-26 21:42:11 +01:00
|
|
|
duplicate.setAmount(Math.min(amountLeft, maxStackSize));
|
2012-11-29 20:28:05 +01:00
|
|
|
duplicate.addEnchantments(item.getEnchantments());
|
|
|
|
|
|
|
|
amountLeft -= duplicate.getAmount();
|
|
|
|
|
|
|
|
inventory.setItem(currentSlot, duplicate);
|
|
|
|
} else if (currentItem.getAmount() < maxStackSize && MaterialUtil.equals(currentItem, item)) {
|
|
|
|
int currentAmount = currentItem.getAmount();
|
|
|
|
int neededToAdd = Math.min(maxStackSize - currentAmount, amountLeft);
|
|
|
|
|
|
|
|
duplicate.setAmount(currentAmount + neededToAdd);
|
|
|
|
duplicate.addEnchantments(item.getEnchantments());
|
|
|
|
|
2012-12-31 02:28:48 +01:00
|
|
|
amountLeft -= neededToAdd;
|
2012-11-29 20:28:05 +01:00
|
|
|
|
|
|
|
inventory.setItem(currentSlot, duplicate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return amountLeft;
|
2012-06-08 15:28:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes an item from the inventory
|
|
|
|
*
|
|
|
|
* @param item Item to remove
|
|
|
|
* @param inventory Inventory
|
|
|
|
* @return Number of items that couldn't be removed
|
|
|
|
*/
|
|
|
|
public static int remove(ItemStack item, Inventory inventory) {
|
|
|
|
Map<Integer, ItemStack> leftovers = inventory.removeItem(item);
|
|
|
|
|
|
|
|
return countItems(leftovers);
|
|
|
|
}
|
|
|
|
|
2012-11-23 21:01:29 +01:00
|
|
|
/**
|
|
|
|
* If items in arguments are similar, this function merges them into stacks of the same type
|
|
|
|
*
|
|
|
|
* @param items Items to merge
|
|
|
|
* @return Merged stack array
|
|
|
|
*/
|
|
|
|
public static ItemStack[] mergeSimilarStacks(ItemStack... items) {
|
|
|
|
List<ItemStack> itemList = new LinkedList<ItemStack>();
|
|
|
|
|
2012-11-29 20:28:05 +01:00
|
|
|
Iterating:
|
2012-11-23 21:01:29 +01:00
|
|
|
for (ItemStack item : items) {
|
|
|
|
for (ItemStack iStack : itemList) {
|
|
|
|
if (MaterialUtil.equals(item, iStack)) {
|
|
|
|
iStack.setAmount(iStack.getAmount() + item.getAmount());
|
2012-11-29 20:28:05 +01:00
|
|
|
continue Iterating;
|
2012-11-23 21:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 20:28:05 +01:00
|
|
|
itemList.add(item);
|
2012-11-23 21:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemList.toArray(new ItemStack[itemList.size()]);
|
|
|
|
}
|
|
|
|
|
2012-11-25 23:34:21 +01:00
|
|
|
/**
|
|
|
|
* Counts the amount of items in ItemStacks
|
|
|
|
*
|
|
|
|
* @param items ItemStacks of items to count
|
|
|
|
* @return How many items are there?
|
|
|
|
*/
|
|
|
|
public static int countItems(ItemStack... items) {
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (ItemStack item : items) {
|
|
|
|
count += item.getAmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
/**
|
|
|
|
* Counts leftovers from a map
|
|
|
|
*
|
|
|
|
* @param items Leftovers
|
|
|
|
* @return Number of leftovers
|
|
|
|
*/
|
2012-11-23 21:01:29 +01:00
|
|
|
public static int countItems(Map<Integer, ?> items) {
|
2012-06-08 15:28:36 +02:00
|
|
|
int totalLeft = 0;
|
|
|
|
|
|
|
|
for (int left : items.keySet()) {
|
|
|
|
totalLeft += left;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalLeft;
|
|
|
|
}
|
|
|
|
}
|