Skip to content

Commit

Permalink
Split above maxStackSize item stacks in bus stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Worive committed Dec 16, 2024
1 parent ae309d7 commit 9a81708
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions src/main/java/gregtech/api/util/VoidProtectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,50 +413,7 @@ private int calculateMaxItemParallels() {
busStacks = machine.getItemOutputSlots(itemOutputs);
}

if (virtualItemOutputsInBus != null) {
for (ItemStack virtualStack : virtualItemOutputsInBus) {
if (virtualStack == null || virtualStack.stackSize <= 0) continue;

boolean merged = false;

// Attempt to merge with an existing stack in busStacks
for (ItemStack busStack : busStacks) {
if (busStack == null) continue;

if (!busStack.isItemEqual(virtualStack)
|| ItemStack.areItemStackTagsEqual(busStack, virtualStack)) {
continue;
}

int spaceLeft = busStack.getMaxStackSize() - busStack.stackSize;

int transferAmount = Math.min(spaceLeft, virtualStack.stackSize);
busStack.stackSize += transferAmount;
virtualStack.stackSize -= transferAmount;

if (virtualStack.stackSize <= 0) {
merged = true;
break;
}
}

if (!merged) {
boolean replacedNull = false;

for (int i = 0; i < busStacks.size(); i++) {
if (busStacks.get(i) == null) {
busStacks.set(i, virtualStack.copy());
replacedNull = true;
break;
}
}

if (!replacedNull) {
busStacks.add(virtualStack.copy());
}
}
}
}
mergeVirtualItemOutputsInto(busStacks);

// A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the
// recipe outputs.
Expand Down Expand Up @@ -531,6 +488,64 @@ private int calculateMaxItemParallels() {
return 0;
}

private void mergeVirtualItemOutputsInto(List<ItemStack> itemStacks) {
if (virtualItemOutputsInBus == null) return;

for (ItemStack virtualStack : virtualItemOutputsInBus) {
if (virtualStack == null || virtualStack.stackSize <= 0) continue;

int maxStackSize = virtualStack.getMaxStackSize();
int remainingItems = virtualStack.stackSize;

while (remainingItems > 0) {
boolean merged = false;

for (ItemStack busStack : itemStacks) {
if (busStack == null) continue;

if (busStack.isItemEqual(virtualStack) && ItemStack.areItemStackTagsEqual(busStack, virtualStack)) {
int spaceLeft = busStack.getMaxStackSize() - busStack.stackSize;
if (spaceLeft > 0) {
int transferAmount = Math.min(spaceLeft, remainingItems);
busStack.stackSize += transferAmount;
remainingItems -= transferAmount;

if (remainingItems == 0) {
merged = true;
break;
}
}
}
}

if (!merged) {
boolean replacedNull = false;

for (int i = 0; i < itemStacks.size(); i++) {
if (itemStacks.get(i) == null) {
int newStackSize = Math.min(remainingItems, maxStackSize);
ItemStack newStack = ItemStack.copyItemStack(virtualStack);
newStack.stackSize = newStackSize;
itemStacks.set(i, newStack);
remainingItems -= newStackSize;
replacedNull = true;
break;
}
}

// If no null slot was found, add a new stack to the list
if (!replacedNull) {
int newStackSize = Math.min(remainingItems, maxStackSize);
ItemStack newStack = ItemStack.copyItemStack(virtualStack);
newStack.stackSize = newStackSize;
itemStacks.add(newStack);
remainingItems -= newStackSize;
}
}
}
}
}

private static class ParallelData {

private int batch;
Expand Down

0 comments on commit 9a81708

Please sign in to comment.