diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 0ef7715..fb81871 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -338,31 +338,34 @@ public class InventoryUtil { public static int getMaxStackSize(ItemStack item) { return Properties.STACK_TO_64 ? 64 : item.getMaxStackSize(); } - + /** * Get an array of different item stacks that are properly stacked to their max stack size - * @param item The item to stack + * @param items The items to stack * @return An array of item stacks which's amount is a maximum of the allowed stack size */ - public static ItemStack[] getItemsStacked(ItemStack item) { - int maxStackSize = getMaxStackSize(item); - if (maxStackSize == 0) { - return new ItemStack[]{}; + public static ItemStack[] getItemsStacked(ItemStack... items) { + List stackedItems = new LinkedList<>(); + for (ItemStack item : items) { + int maxStackSize = getMaxStackSize(item); + if (maxStackSize == 0) { + continue; + } + if (item.getAmount() <= maxStackSize) { + stackedItems.add(item.clone()); + continue; + } + for (int i = 0; i < Math.floor(item.getAmount() / maxStackSize); i++) { + ItemStack itemClone = item.clone(); + itemClone.setAmount(maxStackSize); + stackedItems.add(itemClone); + } + if (item.getAmount() % maxStackSize != 0) { + ItemStack rest = item.clone(); + rest.setAmount(item.getAmount() % maxStackSize); + stackedItems.add(rest); + } } - if (item.getAmount() <= maxStackSize) { - return new ItemStack[]{item}; - } - List items = new LinkedList<>(); - for (int i = 0; i < Math.floor(item.getAmount() / maxStackSize); i++) { - ItemStack itemClone = item.clone(); - itemClone.setAmount(maxStackSize); - items.add(itemClone); - } - if (item.getAmount() % maxStackSize != 0) { - ItemStack rest = item.clone(); - rest.setAmount(item.getAmount() % maxStackSize); - items.add(rest); - } - return items.toArray(new ItemStack[items.size()]); + return stackedItems.toArray(new ItemStack[stackedItems.size()]); } } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java index 809bb43..5e53279 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreTransaction/PartialTransactionModule.java @@ -16,7 +16,7 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import java.math.BigDecimal; -import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -166,17 +166,13 @@ public class PartialTransactionModule implements Listener { } private static ItemStack[] getItems(ItemStack[] stock, Inventory inventory) { - List toReturn = new LinkedList(); + List toReturn = new LinkedList<>(); - ItemStack[] neededItems = InventoryUtil.mergeSimilarStacks(stock); - - for (ItemStack item : neededItems) { + for (ItemStack item : InventoryUtil.mergeSimilarStacks(stock)) { int amount = InventoryUtil.getAmount(item, inventory); - ItemStack clone = item.clone(); - clone.setAmount(amount > item.getAmount() ? item.getAmount() : amount); - - toReturn.add(clone); + Collections.addAll(toReturn, getCountedItemStack(new ItemStack[]{item}, + amount > item.getAmount() ? item.getAmount() : amount)); } return toReturn.toArray(new ItemStack[toReturn.size()]); @@ -184,7 +180,7 @@ public class PartialTransactionModule implements Listener { private static ItemStack[] getCountedItemStack(ItemStack[] stock, int numberOfItems) { int left = numberOfItems; - LinkedList stacks = new LinkedList(); + LinkedList stacks = new LinkedList<>(); for (ItemStack stack : stock) { int count = stack.getAmount(); @@ -203,16 +199,24 @@ public class PartialTransactionModule implements Listener { boolean added = false; + int maxStackSize = InventoryUtil.getMaxStackSize(stack); + for (ItemStack iStack : stacks) { - if (MaterialUtil.equals(toAdd, iStack)) { - iStack.setAmount(iStack.getAmount() + toAdd.getAmount()); - added = true; + if (iStack.getAmount() < maxStackSize && MaterialUtil.equals(toAdd, iStack)) { + int newAmount = iStack.getAmount() + toAdd.getAmount(); + if (newAmount > maxStackSize) { + iStack.setAmount(maxStackSize); + toAdd.setAmount(newAmount - maxStackSize); + } else { + iStack.setAmount(newAmount); + added = true; + } break; } } if (!added) { - stacks.add(toAdd); + Collections.addAll(stacks, InventoryUtil.getItemsStacked(toAdd)); } if (left <= 0) { @@ -231,7 +235,7 @@ public class PartialTransactionModule implements Listener { * @return Whether or not the items fit into the inventory */ private static ItemStack[] getItemsThatFit(ItemStack[] stock, Inventory inventory) { - List resultStock = new ArrayList<>(); + List resultStock = new LinkedList<>(); int emptySlots = InventoryUtil.countEmpty(inventory); @@ -261,7 +265,7 @@ public class PartialTransactionModule implements Listener { item.setAmount(free); } } - resultStock.add(item); + Collections.addAll(resultStock, InventoryUtil.getItemsStacked(item)); } return resultStock.toArray(new ItemStack[resultStock.size()]);