2011-03-19 22:39:51 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2011-05-22 17:29:59 +00:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.entity.Item;
|
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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class can be removed when
|
|
|
|
* https://github.com/Bukkit/CraftBukkit/pull/193
|
|
|
|
* is accepted to CraftBukkit
|
|
|
|
*/
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
public class InventoryWorkaround
|
|
|
|
{
|
|
|
|
public static int first(Inventory ci, ItemStack item, boolean forceDurability, boolean forceAmount)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
return next(ci, item, 0, forceDurability, forceAmount);
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
public static int next(Inventory ci, ItemStack item, int start, boolean forceDurability, boolean forceAmount)
|
|
|
|
{
|
2011-04-08 08:16:28 +00:00
|
|
|
ItemStack[] inventory = ci.getContents();
|
2011-05-22 17:29:59 +00:00
|
|
|
for (int i = start; i < inventory.length; i++)
|
|
|
|
{
|
2011-04-08 08:16:28 +00:00
|
|
|
ItemStack cItem = inventory[i];
|
2011-05-22 17:29:59 +00:00
|
|
|
if (cItem == null)
|
|
|
|
{
|
2011-04-08 11:45:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-22 17:29:59 +00:00
|
|
|
if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability()))
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
public static HashMap<Integer, ItemStack> removeItem(Inventory ci, boolean forceDurability, ItemStack... items)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
|
|
|
|
|
|
|
// TODO: optimization
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
for (int i = 0; i < items.length; i++)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
ItemStack item = items[i];
|
2011-05-22 17:29:59 +00:00
|
|
|
if (item == null)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int toDelete = item.getAmount();
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
|
|
|
|
// Bail when done
|
2011-05-22 17:29:59 +00:00
|
|
|
if (toDelete <= 0)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get first Item, ignore the amount
|
|
|
|
int first = first(ci, item, forceDurability, false);
|
|
|
|
|
|
|
|
// Drat! we don't have this type in the inventory
|
2011-05-22 17:29:59 +00:00
|
|
|
if (first == -1)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
item.setAmount(toDelete);
|
|
|
|
leftover.put(i, item);
|
|
|
|
break;
|
2011-05-22 17:29:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-08 08:16:28 +00:00
|
|
|
ItemStack itemStack = ci.getItem(first);
|
2011-03-19 22:39:51 +00:00
|
|
|
int amount = itemStack.getAmount();
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
if (amount <= toDelete)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
toDelete -= amount;
|
|
|
|
// clear the slot, all used up
|
|
|
|
ci.clear(first);
|
2011-05-22 17:29:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
// split the stack and store
|
|
|
|
itemStack.setAmount(amount - toDelete);
|
|
|
|
ci.setItem(first, itemStack);
|
|
|
|
toDelete = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return leftover;
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
public static boolean containsItem(Inventory ci, boolean forceDurability, ItemStack... items)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
|
|
|
|
|
|
|
|
// TODO: optimization
|
|
|
|
|
|
|
|
// combine items
|
|
|
|
|
|
|
|
ItemStack[] combined = new ItemStack[items.length];
|
2011-05-22 17:29:59 +00:00
|
|
|
for (int i = 0; i < items.length; i++)
|
|
|
|
{
|
|
|
|
if (items[i] == null)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-22 17:29:59 +00:00
|
|
|
for (int j = 0; j < combined.length; j++)
|
|
|
|
{
|
|
|
|
if (combined[j] == null)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability());
|
|
|
|
break;
|
|
|
|
}
|
2011-05-22 17:29:59 +00:00
|
|
|
if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability()))
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
combined[j].setAmount(combined[j].getAmount() + items[i].getAmount());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
for (int i = 0; i < combined.length; i++)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
ItemStack item = combined[i];
|
2011-05-22 17:29:59 +00:00
|
|
|
if (item == null)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int mustHave = item.getAmount();
|
|
|
|
int position = 0;
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
// Bail when done
|
2011-05-22 17:29:59 +00:00
|
|
|
if (mustHave <= 0)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int slot = next(ci, item, position, forceDurability, false);
|
|
|
|
|
|
|
|
// Drat! we don't have this type in the inventory
|
2011-05-22 17:29:59 +00:00
|
|
|
if (slot == -1)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
leftover.put(i, item);
|
|
|
|
break;
|
2011-05-22 17:29:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-08 08:16:28 +00:00
|
|
|
ItemStack itemStack = ci.getItem(slot);
|
2011-03-19 22:39:51 +00:00
|
|
|
int amount = itemStack.getAmount();
|
|
|
|
|
2011-05-22 17:29:59 +00:00
|
|
|
if (amount <= mustHave)
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
mustHave -= amount;
|
2011-05-22 17:29:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-19 22:39:51 +00:00
|
|
|
mustHave = 0;
|
|
|
|
}
|
|
|
|
position = slot + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return leftover.isEmpty();
|
|
|
|
}
|
2011-05-22 17:29:59 +00:00
|
|
|
|
|
|
|
public static Item[] dropItem(Location loc, ItemStack itm)
|
|
|
|
{
|
2011-05-22 21:29:04 +00:00
|
|
|
int maxStackSize = itm.getType().getMaxStackSize();
|
|
|
|
int stacks = itm.getAmount() / maxStackSize;
|
|
|
|
int leftover = itm.getAmount() % maxStackSize;
|
2011-05-22 17:29:59 +00:00
|
|
|
Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
|
|
|
|
for (int i = 0; i < stacks; i++) {
|
2011-05-22 21:29:04 +00:00
|
|
|
itemStacks[i] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), maxStackSize, itm.getDurability()));
|
2011-05-22 17:29:59 +00:00
|
|
|
}
|
|
|
|
if (leftover > 0) {
|
|
|
|
itemStacks[stacks] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), leftover, itm.getDurability()));
|
|
|
|
}
|
|
|
|
return itemStacks;
|
|
|
|
}
|
2011-03-19 22:39:51 +00:00
|
|
|
}
|