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;
@ -38,7 +39,7 @@ public class Trade
{ {
this(null, null, items, null, ess); this(null, null, items, null, ess);
} }
public Trade(final int exp, final IEssentials ess) public Trade(final int exp, final IEssentials ess)
{ {
this(null, null, null, exp, ess); this(null, null, null, exp, ess);
@ -79,9 +80,10 @@ public class Trade
{ {
throw new ChargeException(_("notEnoughMoney")); throw new ChargeException(_("notEnoughMoney"));
} }
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
@ -173,7 +191,7 @@ public class Trade
{ {
return itemStack; return itemStack;
} }
public Integer getExperience() public Integer getExperience()
{ {
return exp; return exp;

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
@ -47,7 +42,7 @@ public final class InventoryWorkaround
{ {
return firstPartial(cinventory, item, forceDurability, item.getType().getMaxStackSize()); return firstPartial(cinventory, item, forceDurability, item.getType().getMaxStackSize());
} }
public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability, final int maxAmount) public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability, final int maxAmount)
{ {
if (item == null) if (item == null)
@ -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
@ -175,7 +169,7 @@ public final class InventoryWorkaround
final int amount = item.getAmount(); final int amount = item.getAmount();
final int partialAmount = partialItem.getAmount(); final int partialAmount = partialItem.getAmount();
// Check if it fully fits // Check if it fully fits
if (amount + partialAmount <= maxAmount) if (amount + partialAmount <= maxAmount)
{ {
@ -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;
}
} }