From 6371a59819d608d1add155e4fc59eeba93e14acc Mon Sep 17 00:00:00 2001 From: snowleo Date: Sun, 4 Dec 2011 22:45:47 +0100 Subject: [PATCH] Fix some rare cases, where adding an item to inventory could result in an infinite loop. --- .../earth2me/essentials/InventoryWorkaround.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java index 3ffda0ab3..5d020f08b 100644 --- a/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java +++ b/Essentials/src/com/earth2me/essentials/InventoryWorkaround.java @@ -43,6 +43,11 @@ public final class InventoryWorkaround } public static int firstPartial(final Inventory cinventory, final ItemStack item, final boolean forceDurability) + { + 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) { @@ -56,7 +61,7 @@ public final class InventoryWorkaround { continue; } - if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < cItem.getType().getMaxStackSize() && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) + if (item.getTypeId() == cItem.getTypeId() && cItem.getAmount() < maxAmount && (!forceDurability || cItem.getDurability() == item.getDurability()) && cItem.getEnchantments().equals(item.getEnchantments())) { return i; } @@ -129,7 +134,8 @@ public final class InventoryWorkaround while (true) { // Do we already have a stack of it? - final int firstPartial = firstPartial(cinventory, item, forceDurability); + final int maxAmount = oversizedStacks > item.getType().getMaxStackSize() ? oversizedStacks : item.getType().getMaxStackSize(); + final int firstPartial = firstPartial(cinventory, item, forceDurability, maxAmount); // Drat! no partial stack if (firstPartial == -1) @@ -145,7 +151,6 @@ public final class InventoryWorkaround } else { - final int maxAmount = oversizedStacks > 0 ? oversizedStacks : item.getType().getMaxStackSize(); // More than a single stack! if (item.getAmount() > maxAmount) { @@ -169,8 +174,7 @@ public final class InventoryWorkaround final int amount = item.getAmount(); final int partialAmount = partialItem.getAmount(); - final int maxAmount = oversizedStacks > 0 ? oversizedStacks : partialItem.getType().getMaxStackSize(); - + // Check if it fully fits if (amount + partialAmount <= maxAmount) {