From 1b602c00d5a055b2feaf2dabb8f899749dc7546f Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Wed, 31 Aug 2022 18:06:32 +0200 Subject: [PATCH] Curse of the simulation - Fixed fluid pipes occasionally losing fluid content when distributing to multiple containers --- .../contraptions/fluids/FluidNetwork.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidNetwork.java b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidNetwork.java index ee559d9ae..eeaf6000d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidNetwork.java +++ b/src/main/java/com/simibubi/create/content/contraptions/fluids/FluidNetwork.java @@ -4,6 +4,7 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.IdentityHashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -183,6 +184,8 @@ public class FluidNetwork { } int flowSpeed = transferSpeed; + Map accumulatedFill = new IdentityHashMap<>(); + for (boolean simulate : Iterate.trueAndFalse) { FluidAction action = simulate ? FluidAction.SIMULATE : FluidAction.EXECUTE; @@ -211,8 +214,9 @@ public class FluidNetwork { return; if (simulate) flowSpeed = transfer.getAmount(); - + List>> availableOutputs = new ArrayList<>(targets); + while (!availableOutputs.isEmpty() && transfer.getAmount() > 0) { int dividedTransfer = transfer.getAmount() / availableOutputs.size(); int remainder = transfer.getAmount() % availableOutputs.size(); @@ -234,12 +238,22 @@ public class FluidNetwork { iterator.remove(); continue; } - + + int simulatedTransfer = toTransfer; + if (simulate) + simulatedTransfer += accumulatedFill.getOrDefault(targetHandler, 0); + FluidStack divided = transfer.copy(); - divided.setAmount(toTransfer); + divided.setAmount(simulatedTransfer); int fill = targetHandler.fill(divided, action); + + if (simulate) { + accumulatedFill.put(targetHandler, Integer.valueOf(fill)); + fill -= simulatedTransfer - toTransfer; + } + transfer.setAmount(transfer.getAmount() - fill); - if (fill < toTransfer) + if (fill < simulatedTransfer) iterator.remove(); }