Skip to content

Commit 3b8e12a

Browse files
committed
fix: coalesce recovered factory shipment stacks
1 parent 477ee1c commit 3b8e12a

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

‎src/main/java/com/innygr/icreateordernetworking/delivery/FactorySourcedDeliveryService.java‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,36 @@ private static boolean reserveAllocations(ServerLevel level, DeliveryRequest req
137137
return false;
138138
}
139139
TrainShipmentMarker.mark(stack, request);
140-
int target = -1;
141-
for (int slot : emptySlots) {
142-
if (logger.getInventory().getStackInSlot(slot).isEmpty()) { target = slot; break; }
140+
// Coalesce compatible transaction stacks first so the number of
141+
// source slots does not artificially exceed the logger's capacity.
142+
for (int slot = 0; slot < logger.getInventory().getSlots() && !stack.isEmpty(); slot++) {
143+
ItemStack existing = logger.getInventory().getStackInSlot(slot);
144+
if (existing.isEmpty() || !TrainShipmentMarker.matches(existing, request.transactionId())
145+
|| !ItemStack.isSameItemSameComponents(existing, stack)) continue;
146+
int room = existing.getMaxStackSize() - existing.getCount();
147+
if (room <= 0) continue;
148+
int moved = Math.min(room, stack.getCount());
149+
ItemStack merged = existing.copy();
150+
merged.grow(moved);
151+
logger.getInventory().setStackInSlot(slot, merged);
152+
stack.shrink(moved);
143153
}
144-
if (target < 0) {
145-
ItemStack remainder = slice.inventory().insertItem(slice.slot(), stack, false);
146-
if (!remainder.isEmpty()) level.addFreshEntity(new net.minecraft.world.entity.item.ItemEntity(
147-
level, slice.position().getX() + 0.5, slice.position().getY() + 0.5,
148-
slice.position().getZ() + 0.5, remainder));
149-
if (outputSlot > 0) throw new IllegalStateException(
150-
"Factory logger filled during a partial durable reservation");
151-
return false;
154+
if (!stack.isEmpty()) {
155+
int target = -1;
156+
for (int slot : emptySlots) {
157+
if (logger.getInventory().getStackInSlot(slot).isEmpty()) { target = slot; break; }
158+
}
159+
if (target < 0) {
160+
ItemStack remainder = slice.inventory().insertItem(slice.slot(), stack, false);
161+
if (!remainder.isEmpty()) level.addFreshEntity(new net.minecraft.world.entity.item.ItemEntity(
162+
level, slice.position().getX() + 0.5, slice.position().getY() + 0.5,
163+
slice.position().getZ() + 0.5, remainder));
164+
if (outputSlot > 0) throw new IllegalStateException(
165+
"Factory logger filled during a partial durable reservation");
166+
return false;
167+
}
168+
logger.getInventory().setStackInSlot(target, stack);
152169
}
153-
logger.getInventory().setStackInSlot(target, stack);
154170
logger.setChanged();
155171
outputSlot++;
156172
}

0 commit comments

Comments
 (0)