From d05f2d7a32058ea0b877786fffb5dff4c00e3879 Mon Sep 17 00:00:00 2001 From: md_5 Date: Wed, 15 Feb 2012 17:18:12 +1100 Subject: [PATCH] Remove dropItems from the inventory work around --- .../src/com/earth2me/essentials/Trade.java | 30 +++++++++++--- .../craftbukkit/InventoryWorkaround.java | 39 +++---------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Trade.java b/Essentials/src/com/earth2me/essentials/Trade.java index 6cd9ce93a..933b54b3f 100644 --- a/Essentials/src/com/earth2me/essentials/Trade.java +++ b/Essentials/src/com/earth2me/essentials/Trade.java @@ -13,6 +13,7 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Location; +import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; @@ -38,7 +39,7 @@ public class Trade { this(null, null, items, null, ess); } - + public Trade(final int exp, final IEssentials ess) { this(null, null, null, exp, ess); @@ -79,9 +80,10 @@ public class Trade { throw new ChargeException(_("notEnoughMoney")); } - - if (exp != null && exp > 0 - && SetExpFix.getTotalExperience(user) < exp) { + + if (exp != null && exp > 0 + && SetExpFix.getTotalExperience(user) < exp) + { throw new ChargeException(_("notEnoughExperience")); } } @@ -103,9 +105,25 @@ public class Trade if (dropItems) { final Map leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack()); + final Location loc = user.getLocation(); 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 @@ -173,7 +191,7 @@ public class Trade { return itemStack; } - + public Integer getExperience() { return exp; diff --git a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java index 308568452..a6d5d4fbc 100644 --- a/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -1,17 +1,12 @@ package com.earth2me.essentials.craftbukkit; -import com.earth2me.essentials.craftbukkit.FakeInventory; import java.util.HashMap; import java.util.Map; -import org.bukkit.Location; -import org.bukkit.entity.Item; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; /* - * This class can be removed when - * https://github.com/Bukkit/CraftBukkit/pull/193 - * is accepted to CraftBukkit + * This class can be removed when https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit */ public final class InventoryWorkaround @@ -47,7 +42,7 @@ public final class InventoryWorkaround { return firstPartial(cinventory, item, forceDurability, item.getType().getMaxStackSize()); } - + public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability, final int maxAmount) { if (item == null) @@ -93,10 +88,9 @@ public final class InventoryWorkaround { final Map leftover = new HashMap(); - /* TODO: some optimization - * - Create a 'firstPartial' with a 'fromIndex' - * - Record the lastPartial per Material - * - Cache firstEmpty result + /* + * TODO: some optimization - Create a 'firstPartial' with a 'fromIndex' - Record the lastPartial per Material - + * Cache firstEmpty result */ // combine items @@ -175,7 +169,7 @@ public final class InventoryWorkaround final int amount = item.getAmount(); final int partialAmount = partialItem.getAmount(); - + // Check if it fully fits if (amount + partialAmount <= maxAmount) { @@ -325,25 +319,4 @@ public final class InventoryWorkaround } 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; - } }