Skip to content

Commit

Permalink
Merge and replace nulls with virtual item output
Browse files Browse the repository at this point in the history
  • Loading branch information
Worive committed Dec 16, 2024
1 parent 96606eb commit ae309d7
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/main/java/gregtech/api/util/VoidProtectionHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package gregtech.api.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -414,8 +413,49 @@ private int calculateMaxItemParallels() {
busStacks = machine.getItemOutputSlots(itemOutputs);
}

if (virtualItemOutputsInBus != null && virtualItemOutputsInBus.length > 0) {
Collections.addAll(busStacks, virtualItemOutputsInBus);
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());
}
}
}
}

// A map to hold the items we will be 'inputting' into the output buses. These itemstacks are actually the
Expand Down

0 comments on commit ae309d7

Please sign in to comment.