mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2025-01-10 09:07:39 +01:00
Properly stack partial stacks (Fixes #102)
This commit is contained in:
parent
650d02e401
commit
c5c9e96b13
@ -341,28 +341,31 @@ public class InventoryUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of different item stacks that are properly stacked to their max stack size
|
* 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
|
* @return An array of item stacks which's amount is a maximum of the allowed stack size
|
||||||
*/
|
*/
|
||||||
public static ItemStack[] getItemsStacked(ItemStack item) {
|
public static ItemStack[] getItemsStacked(ItemStack... items) {
|
||||||
|
List<ItemStack> stackedItems = new LinkedList<>();
|
||||||
|
for (ItemStack item : items) {
|
||||||
int maxStackSize = getMaxStackSize(item);
|
int maxStackSize = getMaxStackSize(item);
|
||||||
if (maxStackSize == 0) {
|
if (maxStackSize == 0) {
|
||||||
return new ItemStack[]{};
|
continue;
|
||||||
}
|
}
|
||||||
if (item.getAmount() <= maxStackSize) {
|
if (item.getAmount() <= maxStackSize) {
|
||||||
return new ItemStack[]{item};
|
stackedItems.add(item.clone());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
List<ItemStack> items = new LinkedList<>();
|
|
||||||
for (int i = 0; i < Math.floor(item.getAmount() / maxStackSize); i++) {
|
for (int i = 0; i < Math.floor(item.getAmount() / maxStackSize); i++) {
|
||||||
ItemStack itemClone = item.clone();
|
ItemStack itemClone = item.clone();
|
||||||
itemClone.setAmount(maxStackSize);
|
itemClone.setAmount(maxStackSize);
|
||||||
items.add(itemClone);
|
stackedItems.add(itemClone);
|
||||||
}
|
}
|
||||||
if (item.getAmount() % maxStackSize != 0) {
|
if (item.getAmount() % maxStackSize != 0) {
|
||||||
ItemStack rest = item.clone();
|
ItemStack rest = item.clone();
|
||||||
rest.setAmount(item.getAmount() % maxStackSize);
|
rest.setAmount(item.getAmount() % maxStackSize);
|
||||||
items.add(rest);
|
stackedItems.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 org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.Collections;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@ -166,17 +166,13 @@ public class PartialTransactionModule implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static ItemStack[] getItems(ItemStack[] stock, Inventory inventory) {
|
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 : InventoryUtil.mergeSimilarStacks(stock)) {
|
||||||
|
|
||||||
for (ItemStack item : neededItems) {
|
|
||||||
int amount = InventoryUtil.getAmount(item, inventory);
|
int amount = InventoryUtil.getAmount(item, inventory);
|
||||||
|
|
||||||
ItemStack clone = item.clone();
|
Collections.addAll(toReturn, getCountedItemStack(new ItemStack[]{item},
|
||||||
clone.setAmount(amount > item.getAmount() ? item.getAmount() : amount);
|
amount > item.getAmount() ? item.getAmount() : amount));
|
||||||
|
|
||||||
toReturn.add(clone);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return toReturn.toArray(new ItemStack[toReturn.size()]);
|
return toReturn.toArray(new ItemStack[toReturn.size()]);
|
||||||
@ -184,7 +180,7 @@ public class PartialTransactionModule implements Listener {
|
|||||||
|
|
||||||
private static ItemStack[] getCountedItemStack(ItemStack[] stock, int numberOfItems) {
|
private static ItemStack[] getCountedItemStack(ItemStack[] stock, int numberOfItems) {
|
||||||
int left = numberOfItems;
|
int left = numberOfItems;
|
||||||
LinkedList<ItemStack> stacks = new LinkedList<ItemStack>();
|
LinkedList<ItemStack> stacks = new LinkedList<>();
|
||||||
|
|
||||||
for (ItemStack stack : stock) {
|
for (ItemStack stack : stock) {
|
||||||
int count = stack.getAmount();
|
int count = stack.getAmount();
|
||||||
@ -203,16 +199,24 @@ public class PartialTransactionModule implements Listener {
|
|||||||
|
|
||||||
boolean added = false;
|
boolean added = false;
|
||||||
|
|
||||||
|
int maxStackSize = InventoryUtil.getMaxStackSize(stack);
|
||||||
|
|
||||||
for (ItemStack iStack : stacks) {
|
for (ItemStack iStack : stacks) {
|
||||||
if (MaterialUtil.equals(toAdd, iStack)) {
|
if (iStack.getAmount() < maxStackSize && MaterialUtil.equals(toAdd, iStack)) {
|
||||||
iStack.setAmount(iStack.getAmount() + toAdd.getAmount());
|
int newAmount = iStack.getAmount() + toAdd.getAmount();
|
||||||
|
if (newAmount > maxStackSize) {
|
||||||
|
iStack.setAmount(maxStackSize);
|
||||||
|
toAdd.setAmount(newAmount - maxStackSize);
|
||||||
|
} else {
|
||||||
|
iStack.setAmount(newAmount);
|
||||||
added = true;
|
added = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!added) {
|
if (!added) {
|
||||||
stacks.add(toAdd);
|
Collections.addAll(stacks, InventoryUtil.getItemsStacked(toAdd));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (left <= 0) {
|
if (left <= 0) {
|
||||||
@ -231,7 +235,7 @@ public class PartialTransactionModule implements Listener {
|
|||||||
* @return Whether or not the items fit into the inventory
|
* @return Whether or not the items fit into the inventory
|
||||||
*/
|
*/
|
||||||
private static ItemStack[] getItemsThatFit(ItemStack[] stock, Inventory inventory) {
|
private static ItemStack[] getItemsThatFit(ItemStack[] stock, Inventory inventory) {
|
||||||
List<ItemStack> resultStock = new ArrayList<>();
|
List<ItemStack> resultStock = new LinkedList<>();
|
||||||
|
|
||||||
int emptySlots = InventoryUtil.countEmpty(inventory);
|
int emptySlots = InventoryUtil.countEmpty(inventory);
|
||||||
|
|
||||||
@ -261,7 +265,7 @@ public class PartialTransactionModule implements Listener {
|
|||||||
item.setAmount(free);
|
item.setAmount(free);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resultStock.add(item);
|
Collections.addAll(resultStock, InventoryUtil.getItemsStacked(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultStock.toArray(new ItemStack[resultStock.size()]);
|
return resultStock.toArray(new ItemStack[resultStock.size()]);
|
||||||
|
Loading…
Reference in New Issue
Block a user