Fix issues with partial item transfer due to full inventory

This commit is contained in:
Phoenix616 2018-01-08 23:56:58 +01:00
parent 3ad0849c3d
commit eaeeb80fd3
2 changed files with 8 additions and 11 deletions

View File

@ -59,7 +59,7 @@ public class MaterialUtil {
*/
public static boolean equals(ItemStack one, ItemStack two) {
if (one == null || two == null) {
return one != two;
return one == two;
}
if (one.isSimilar(two)) {
return true;

View File

@ -234,15 +234,13 @@ public class PartialTransactionModule implements Listener {
List<ItemStack> resultStock = new ArrayList<>();
int emptySlots = InventoryUtil.countEmpty(inventory);
ItemStack[] itemsInInventory = getItems(stock, inventory);
for (ItemStack item : stock) {
for (ItemStack item : InventoryUtil.mergeSimilarStacks(stock)) {
int maxStackSize = InventoryUtil.getMaxStackSize(item);
int free = 0;
for (ItemStack itemInInventory : itemsInInventory) {
for (ItemStack itemInInventory : inventory.getContents()) {
if (MaterialUtil.equals(item, itemInInventory)) {
free = (maxStackSize - itemInInventory.getAmount()) % maxStackSize;
break;
free += (maxStackSize - itemInInventory.getAmount()) % maxStackSize;
}
}
@ -250,21 +248,20 @@ public class PartialTransactionModule implements Listener {
continue;
}
ItemStack clone = item.clone();
if (item.getAmount() > free) {
if (emptySlots > 0) {
int requiredSlots = (int) Math.ceil((item.getAmount() - free) / maxStackSize);
int requiredSlots = (int) Math.ceil(((double) item.getAmount() - free) / maxStackSize);
if (requiredSlots <= emptySlots) {
emptySlots = emptySlots - requiredSlots;
} else {
item.setAmount(free + maxStackSize * emptySlots);
emptySlots = 0;
clone.setAmount(free + maxStackSize * emptySlots);
}
} else {
clone.setAmount(free);
item.setAmount(free);
}
}
resultStock.add(clone);
resultStock.add(item);
}
return resultStock.toArray(new ItemStack[resultStock.size()]);