mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2025-01-09 16:47:36 +01:00
Properly stack partial stacks (Fixes #102)
This commit is contained in:
parent
650d02e401
commit
c5c9e96b13
@ -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<ItemStack> 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<ItemStack> 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()]);
|
||||
}
|
||||
}
|
||||
|
@ -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<ItemStack> toReturn = new LinkedList<ItemStack>();
|
||||
List<ItemStack> 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<ItemStack> stacks = new LinkedList<ItemStack>();
|
||||
LinkedList<ItemStack> 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<ItemStack> resultStock = new ArrayList<>();
|
||||
List<ItemStack> 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()]);
|
||||
|
Loading…
Reference in New Issue
Block a user