Added a utility method to merge similar stacks

This commit is contained in:
Acrobot 2012-11-23 21:01:29 +01:00
parent aa35a0aa84
commit 9c492ac031

View File

@ -4,6 +4,8 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
@ -129,13 +131,41 @@ public class InventoryUtil {
return countItems(leftovers);
}
/**
* 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>();
for (ItemStack item : items) {
boolean added = false;
for (ItemStack iStack : itemList) {
if (MaterialUtil.equals(item, iStack)) {
iStack.setAmount(iStack.getAmount() + item.getAmount());
added = true;
break;
}
}
if (!added) {
itemList.add(item);
}
}
return itemList.toArray(new ItemStack[itemList.size()]);
}
/**
* Counts leftovers from a map
*
* @param items Leftovers
* @return Number of leftovers
*/
private static int countItems(Map<Integer, ?> items) {
public static int countItems(Map<Integer, ?> items) {
int totalLeft = 0;
for (int left : items.keySet()) {