Remove dropItems from the inventory work around

This commit is contained in:
md_5 2012-02-15 17:18:12 +11:00
parent f0c0ee1a8d
commit d05f2d7a32
2 changed files with 30 additions and 39 deletions

View file

@ -13,6 +13,7 @@ import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -81,7 +82,8 @@ public class Trade
} }
if (exp != null && exp > 0 if (exp != null && exp > 0
&& SetExpFix.getTotalExperience(user) < exp) { && SetExpFix.getTotalExperience(user) < exp)
{
throw new ChargeException(_("notEnoughExperience")); throw new ChargeException(_("notEnoughExperience"));
} }
} }
@ -103,9 +105,25 @@ public class Trade
if (dropItems) if (dropItems)
{ {
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
final Location loc = user.getLocation();
for (ItemStack itemStack : leftOver.values()) for (ItemStack itemStack : leftOver.values())
{ {
InventoryWorkaround.dropItem(user.getLocation(), itemStack); final int maxStackSize = itemStack.getType().getMaxStackSize();
final int stacks = itemStack.getAmount() / maxStackSize;
final int leftover = itemStack.getAmount() % maxStackSize;
final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
for (int i = 0; i < stacks; i++)
{
final ItemStack stack = itemStack.clone();
stack.setAmount(maxStackSize);
itemStacks[i] = loc.getWorld().dropItem(loc, stack);
}
if (leftover > 0)
{
final ItemStack stack = itemStack.clone();
stack.setAmount(leftover);
itemStacks[stacks] = loc.getWorld().dropItem(loc, stack);
}
} }
} }
else else

View file

@ -1,17 +1,12 @@
package com.earth2me.essentials.craftbukkit; package com.earth2me.essentials.craftbukkit;
import com.earth2me.essentials.craftbukkit.FakeInventory;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.Item;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
/* /*
* This class can be removed when * This class can be removed when https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit
* https://github.com/Bukkit/CraftBukkit/pull/193
* is accepted to CraftBukkit
*/ */
public final class InventoryWorkaround public final class InventoryWorkaround
@ -93,10 +88,9 @@ public final class InventoryWorkaround
{ {
final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); final Map<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>();
/* TODO: some optimization /*
* - Create a 'firstPartial' with a 'fromIndex' * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material -
* - Record the lastPartial per Material * Cache firstEmpty result
* - Cache firstEmpty result
*/ */
// combine items // combine items
@ -325,25 +319,4 @@ public final class InventoryWorkaround
} }
return leftover.isEmpty(); return leftover.isEmpty();
} }
public static Item[] dropItem(final Location loc, final ItemStack itm)
{
final int maxStackSize = itm.getType().getMaxStackSize();
final int stacks = itm.getAmount() / maxStackSize;
final int leftover = itm.getAmount() % maxStackSize;
final Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)];
for (int i = 0; i < stacks; i++)
{
final ItemStack stack = itm.clone();
stack.setAmount(maxStackSize);
itemStacks[i] = loc.getWorld().dropItem(loc, stack);
}
if (leftover > 0)
{
final ItemStack stack = itm.clone();
stack.setAmount(leftover);
itemStacks[stacks] = loc.getWorld().dropItem(loc, stack);
}
return itemStacks;
}
} }