2011-12-06 16:42:55 +00:00
|
|
|
package com.earth2me.essentials.craftbukkit;
|
2011-03-19 22:39:51 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2011-06-06 12:01:56 +00:00
|
|
|
import java.util.Map;
|
2012-12-19 21:07:03 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2011-04-08 11:13:33 +00:00
|
|
|
import org.bukkit.inventory.Inventory;
|
2011-03-19 22:39:51 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
|
|
|
|
/*
|
2012-02-15 06:18:12 +00:00
|
|
|
* This class can be removed when https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit
|
2011-03-19 22:39:51 +00:00
|
|
|
*/
|
|
|
|
|
2011-06-01 10:40:12 +00:00
|
|
|
public final class InventoryWorkaround
|
2011-05-22 17:29:59 +00:00
|
|
|
{
|
2011-06-01 10:40:12 +00:00
|
|
|
private InventoryWorkaround()
|
|
|
|
{
|
|
|
|
}
|
2011-06-12 20:55:08 +00:00
|
|
|
|
2012-12-20 15:07:18 +00:00
|
|
|
private static int firstPartial(final Inventory inventory, final ItemStack item, final int maxAmount)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
|
|
|
if (item == null)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2012-12-19 21:07:03 +00:00
|
|
|
final ItemStack[] stacks = inventory.getContents();
|
|
|
|
for (int i = 0; i < stacks.length; i++)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
2012-12-19 21:07:03 +00:00
|
|
|
final ItemStack cItem = stacks[i];
|
|
|
|
if (cItem != null && cItem.getAmount() < maxAmount && cItem.isSimilar(item))
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-06 05:40:22 +00:00
|
|
|
// Returns what it couldnt store
|
|
|
|
// This will will abort if it couldn't store all items
|
|
|
|
public static Map<Integer, ItemStack> addAllItems(final Inventory inventory, final ItemStack... items)
|
2011-10-09 20:25:15 +00:00
|
|
|
{
|
2012-12-19 21:07:03 +00:00
|
|
|
final Inventory fakeInventory = Bukkit.getServer().createInventory(null, inventory.getType());
|
|
|
|
fakeInventory.setContents(inventory.getContents());
|
2013-05-06 05:40:22 +00:00
|
|
|
Map<Integer, ItemStack> overFlow = addItems(fakeInventory, items);
|
|
|
|
if (overFlow.isEmpty())
|
2011-10-09 20:25:15 +00:00
|
|
|
{
|
2012-12-20 15:07:18 +00:00
|
|
|
addItems(inventory, items);
|
2013-05-06 05:40:22 +00:00
|
|
|
return null;
|
2011-10-09 20:25:15 +00:00
|
|
|
}
|
2013-05-06 05:40:22 +00:00
|
|
|
return addItems(fakeInventory, items);
|
2011-10-09 20:25:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 05:40:22 +00:00
|
|
|
// Returns what it couldnt store
|
2012-12-19 21:07:03 +00:00
|
|
|
public static Map<Integer, ItemStack> addItems(final Inventory inventory, final ItemStack... items)
|
2011-11-27 09:59:28 +00:00
|
|
|
{
|
2012-12-19 21:07:03 +00:00
|
|
|
return addOversizedItems(inventory, 0, items);
|
2011-11-27 09:59:28 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 05:40:22 +00:00
|
|
|
// Returns what it couldnt store
|
|
|
|
// Set oversizedStack to below normal stack size to disable oversized stacks
|
2012-12-19 21:07:03 +00:00
|
|
|
public static Map<Integer, ItemStack> addOversizedItems(final Inventory inventory, final int oversizedStacks, final ItemStack... items)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
|
|
|
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
|
|
|
|
2012-02-15 06:18:12 +00:00
|
|
|
/*
|
|
|
|
* TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
|
|
|
|
* Cache firstEmpty result
|
2011-06-12 20:55:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// combine items
|
|
|
|
|
2012-12-19 21:07:03 +00:00
|
|
|
final ItemStack[] combined = new ItemStack[items.length];
|
2011-06-12 20:55:08 +00:00
|
|
|
for (int i = 0; i < items.length; i++)
|
|
|
|
{
|
2011-06-30 23:31:20 +00:00
|
|
|
if (items[i] == null || items[i].getAmount() < 1)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < combined.length; j++)
|
|
|
|
{
|
|
|
|
if (combined[j] == null)
|
|
|
|
{
|
2011-11-27 05:10:11 +00:00
|
|
|
combined[j] = items[i].clone();
|
2011-06-12 20:55:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-19 21:07:03 +00:00
|
|
|
if (combined[j].isSimilar(items[i]))
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
|
|
|
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-18 02:59:47 +00:00
|
|
|
for (int i = 0; i < combined.length; i++)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
2011-07-18 02:59:47 +00:00
|
|
|
final ItemStack item = combined[i];
|
2013-08-11 23:26:09 +00:00
|
|
|
if (item == null || item.getTypeId() == 0)
|
2011-07-18 20:39:01 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-09 20:25:15 +00:00
|
|
|
|
2011-06-12 20:55:08 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Do we already have a stack of it?
|
2011-12-04 21:45:47 +00:00
|
|
|
final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize();
|
2012-12-19 21:07:03 +00:00
|
|
|
final int firstPartial = firstPartial(inventory, item, maxAmount);
|
2011-06-12 20:55:08 +00:00
|
|
|
|
|
|
|
// Drat! no partial stack
|
|
|
|
if (firstPartial == -1)
|
|
|
|
{
|
|
|
|
// Find a free spot!
|
2012-12-19 21:07:03 +00:00
|
|
|
final int firstFree = inventory.firstEmpty();
|
2011-06-12 20:55:08 +00:00
|
|
|
|
|
|
|
if (firstFree == -1)
|
|
|
|
{
|
|
|
|
// No space at all!
|
|
|
|
leftover.put(i, item);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// More than a single stack!
|
2011-11-28 18:55:51 +00:00
|
|
|
if (item.getAmount() > maxAmount)
|
2011-06-12 20:55:08 +00:00
|
|
|
{
|
2011-11-28 18:55:51 +00:00
|
|
|
final ItemStack stack = item.clone();
|
|
|
|
stack.setAmount(maxAmount);
|
2012-12-19 21:07:03 +00:00
|
|
|
inventory.setItem(firstFree, stack);
|
2011-11-28 18:55:51 +00:00
|
|
|
item.setAmount(item.getAmount() - maxAmount);
|
2011-06-12 20:55:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Just store it
|
2012-12-19 21:07:03 +00:00
|
|
|
inventory.setItem(firstFree, item);
|
2011-06-12 20:55:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// So, apparently it might only partially fit, well lets do just that
|
2012-12-19 21:07:03 +00:00
|
|
|
final ItemStack partialItem = inventory.getItem(firstPartial);
|
2011-06-12 20:55:08 +00:00
|
|
|
|
|
|
|
final int amount = item.getAmount();
|
|
|
|
final int partialAmount = partialItem.getAmount();
|
2012-02-15 06:18:12 +00:00
|
|
|
|
2011-06-12 20:55:08 +00:00
|
|
|
// Check if it fully fits
|
|
|
|
if (amount + partialAmount <= maxAmount)
|
|
|
|
{
|
|
|
|
partialItem.setAmount(amount + partialAmount);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It fits partially
|
|
|
|
partialItem.setAmount(maxAmount);
|
|
|
|
item.setAmount(amount + partialAmount - maxAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return leftover;
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|