diff options
author | snowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb> | 2011-05-22 17:29:59 +0000 |
---|---|---|
committer | snowleo <snowleo@e251c2fe-e539-e718-e476-b85c1f46cddb> | 2011-05-22 17:29:59 +0000 |
commit | 585d719bdcfd1ef96ee056c608e6e5e5ce722b31 (patch) | |
tree | f339a457beaee1c17af8f4d6cdc0874f60c2ae57 | |
parent | ae6b537296e514f9f9a73bdc92824565d2468b34 (diff) | |
download | Essentials-585d719bdcfd1ef96ee056c608e6e5e5ce722b31.tar Essentials-585d719bdcfd1ef96ee056c608e6e5e5ce722b31.tar.gz Essentials-585d719bdcfd1ef96ee056c608e6e5e5ce722b31.tar.lz Essentials-585d719bdcfd1ef96ee056c608e6e5e5ce722b31.tar.xz Essentials-585d719bdcfd1ef96ee056c608e6e5e5ce722b31.zip |
Fix that dropItem drops Stack that are over MaxStackSize.
git-svn-id: https://svn.java.net/svn/essentials~svn/trunk@1509 e251c2fe-e539-e718-e476-b85c1f46cddb
3 files changed, 85 insertions, 34 deletions
diff --git a/Essentials/src/com/earth2me/essentials/EssentialsEcoBlockListener.java b/Essentials/src/com/earth2me/essentials/EssentialsEcoBlockListener.java index 8317557bb..eae59aec1 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsEcoBlockListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsEcoBlockListener.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import javax.crypto.spec.IvParameterSpec; import org.bukkit.Material; import org.bukkit.block.Sign; import org.bukkit.craftbukkit.block.CraftSign; @@ -71,7 +72,7 @@ public class EssentialsEcoBlockListener extends BlockListener } else if (i1 != null) { - user.getWorld().dropItem(user.getLocation(), i1); + InventoryWorkaround.dropItem(user.getLocation(), i1); } if (m2) @@ -80,7 +81,7 @@ public class EssentialsEcoBlockListener extends BlockListener } else if (i2 != null) { - user.getWorld().dropItem(user.getLocation(), i2); + InventoryWorkaround.dropItem(user.getLocation(), i2); } sign.setType(Material.AIR); diff --git a/Essentials/src/com/earth2me/essentials/EssentialsEcoPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsEcoPlayerListener.java index 0a0f82207..fb7aab128 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsEcoPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsEcoPlayerListener.java @@ -53,7 +53,7 @@ public class EssentialsEcoPlayerListener extends PlayerListener Map<Integer, ItemStack> leftOver = user.getInventory().addItem(item); for (ItemStack itemStack : leftOver.values()) { - user.getWorld().dropItem(user.getLocation(), itemStack); + InventoryWorkaround.dropItem(user.getLocation(), itemStack); } user.updateInventory(); } @@ -121,7 +121,7 @@ public class EssentialsEcoPlayerListener extends PlayerListener Map<Integer, ItemStack> leftOver = user.getInventory().addItem(i1); for (ItemStack itemStack : leftOver.values()) { - user.getWorld().dropItem(user.getLocation(), itemStack); + InventoryWorkaround.dropItem(user.getLocation(), itemStack); } user.updateInventory(); } @@ -169,7 +169,7 @@ public class EssentialsEcoPlayerListener extends PlayerListener Map<Integer, ItemStack> leftOver = user.getInventory().addItem(qi2); for (ItemStack itemStack : leftOver.values()) { - user.getWorld().dropItem(user.getLocation(), itemStack); + InventoryWorkaround.dropItem(user.getLocation(), itemStack); } } diff --git a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java index d4250bc7c..ba356eb50 100644 --- a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java @@ -1,6 +1,9 @@ package com.earth2me.essentials; import java.util.HashMap; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.entity.Item; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -9,42 +12,53 @@ import org.bukkit.inventory.ItemStack; * https://github.com/Bukkit/CraftBukkit/pull/193 * is accepted to CraftBukkit */ -public class InventoryWorkaround { - public static int first(Inventory ci, ItemStack item, boolean forceDurability, boolean forceAmount) { +public class InventoryWorkaround +{ + public static int first(Inventory ci, ItemStack item, boolean forceDurability, boolean forceAmount) + { return next(ci, item, 0, forceDurability, forceAmount); } - public static int next(Inventory ci, ItemStack item, int start, boolean forceDurability, boolean forceAmount) { + public static int next(Inventory ci, ItemStack item, int start, boolean forceDurability, boolean forceAmount) + { ItemStack[] inventory = ci.getContents(); - for (int i = start; i < inventory.length; i++) { + for (int i = start; i < inventory.length; i++) + { ItemStack cItem = inventory[i]; - if (cItem == null) { + if (cItem == null) + { continue; } - if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability())) { + if (item.getTypeId() == cItem.getTypeId() && (!forceAmount || item.getAmount() == cItem.getAmount()) && (!forceDurability || cItem.getDurability() == item.getDurability())) + { return i; } } return -1; } - public static HashMap<Integer, ItemStack> removeItem(Inventory ci, boolean forceDurability, ItemStack... items) { + public static HashMap<Integer, ItemStack> removeItem(Inventory ci, boolean forceDurability, ItemStack... items) + { HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); // TODO: optimization - for (int i = 0; i < items.length; i++) { + for (int i = 0; i < items.length; i++) + { ItemStack item = items[i]; - if (item == null) { + if (item == null) + { continue; } int toDelete = item.getAmount(); - while (true) { + while (true) + { // Bail when done - if (toDelete <= 0) { + if (toDelete <= 0) + { break; } @@ -52,19 +66,25 @@ public class InventoryWorkaround { int first = first(ci, item, forceDurability, false); // Drat! we don't have this type in the inventory - if (first == -1) { + if (first == -1) + { item.setAmount(toDelete); leftover.put(i, item); break; - } else { + } + else + { ItemStack itemStack = ci.getItem(first); int amount = itemStack.getAmount(); - if (amount <= toDelete) { + if (amount <= toDelete) + { toDelete -= amount; // clear the slot, all used up ci.clear(first); - } else { + } + else + { // split the stack and store itemStack.setAmount(amount - toDelete); ci.setItem(first, itemStack); @@ -76,7 +96,8 @@ public class InventoryWorkaround { return leftover; } - public static boolean containsItem(Inventory ci, boolean forceDurability, ItemStack... items) { + public static boolean containsItem(Inventory ci, boolean forceDurability, ItemStack... items) + { HashMap<Integer, ItemStack> leftover = new HashMap<Integer, ItemStack>(); // TODO: optimization @@ -84,49 +105,64 @@ public class InventoryWorkaround { // combine items ItemStack[] combined = new ItemStack[items.length]; - for (int i = 0; i < items.length; i++) { - if (items[i] == null) { + for (int i = 0; i < items.length; i++) + { + if (items[i] == null) + { continue; } - for (int j = 0; j < combined.length; j++) { - if (combined[j] == null) { + for (int j = 0; j < combined.length; j++) + { + if (combined[j] == null) + { combined[j] = new ItemStack(items[i].getType(), items[i].getAmount(), items[i].getDurability()); break; } - if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability())) { + if (combined[j].getTypeId() == items[i].getTypeId() && (!forceDurability || combined[j].getDurability() == items[i].getDurability())) + { combined[j].setAmount(combined[j].getAmount() + items[i].getAmount()); break; } } } - for (int i = 0; i < combined.length; i++) { + for (int i = 0; i < combined.length; i++) + { ItemStack item = combined[i]; - if (item == null) { + if (item == null) + { continue; } int mustHave = item.getAmount(); int position = 0; - while (true) { + while (true) + { // Bail when done - if (mustHave <= 0) { + if (mustHave <= 0) + { break; } int slot = next(ci, item, position, forceDurability, false); // Drat! we don't have this type in the inventory - if (slot == -1) { + if (slot == -1) + { leftover.put(i, item); break; - } else { + } + else + { ItemStack itemStack = ci.getItem(slot); int amount = itemStack.getAmount(); - if (amount <= mustHave) { + if (amount <= mustHave) + { mustHave -= amount; - } else { + } + else + { mustHave = 0; } position = slot + 1; @@ -135,4 +171,18 @@ public class InventoryWorkaround { } return leftover.isEmpty(); } + + public static Item[] dropItem(Location loc, ItemStack itm) + { + int stacks = itm.getAmount() / itm.getMaxStackSize(); + int leftover = itm.getAmount() % itm.getMaxStackSize(); + Item[] itemStacks = new Item[stacks + (leftover > 0 ? 1 : 0)]; + for (int i = 0; i < stacks; i++) { + itemStacks[i] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), itm.getMaxStackSize(), itm.getDurability())); + } + if (leftover > 0) { + itemStacks[stacks] = loc.getWorld().dropItem(loc, new ItemStack(itm.getType(), leftover, itm.getDurability())); + } + return itemStacks; + } } |