From 82026627c6fafbc0def10244f8c02ebd8c514f9a Mon Sep 17 00:00:00 2001 From: Martin Robertz Date: Sun, 14 Jan 2024 18:20:41 +0100 Subject: Feature/movefluid (#2459) * merge similar code into new utility function moveFluid * Spotless apply for branch feature/movefluid for #2455 (#2456) spotlessApply Co-authored-by: GitHub GTNH Actions <> * fix boolean logic * Spotless apply for branch feature/movefluid for #2459 (#2460) spotlessApply Co-authored-by: GitHub GTNH Actions <> * fix (cherry picked from commit dce33cb4bbc0b1405bcbee25b5f1f1e6384f0147) --------- Co-authored-by: Glease <4586901+Glease@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/main/java/gregtech/api/util/GT_Utility.java | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/main/java/gregtech/api/util') diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java index 75451d9fdf..d41e2a3b04 100644 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ b/src/main/java/gregtech/api/util/GT_Utility.java @@ -45,6 +45,7 @@ import java.util.Set; import java.util.UUID; import java.util.function.Function; import java.util.function.IntFunction; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Collectors; @@ -1682,6 +1683,46 @@ public class GT_Utility { true); } + /** + * Move up to maxAmount amount of fluid from source to dest, with optional filtering via allowMove. note that this + * filter cannot bypass filtering done by IFluidHandlers themselves. + * + * this overload will assume the fill side is the opposite of drainSide + * + * @param source tank to drain from. method become noop if this is null + * @param dest tank to fill to. method become noop if this is null + * @param drainSide side used during draining operation + * @param maxAmount max amount of fluid to transfer. method become noop if this is not a positive integer + * @param allowMove filter. can be null to signal all fluids are accepted + */ + public static void moveFluid(IFluidHandler source, IFluidHandler dest, ForgeDirection drainSide, int maxAmount, + @Nullable Predicate allowMove) { + moveFluid(source, dest, drainSide, drainSide.getOpposite(), maxAmount, allowMove); + } + + /** + * Move up to maxAmount amount of fluid from source to dest, with optional filtering via allowMove. note that this + * filter cannot bypass filtering done by IFluidHandlers themselves. + * + * @param source tank to drain from. method become noop if this is null + * @param dest tank to fill to. method become noop if this is null + * @param drainSide side used during draining operation + * @param fillSide side used during filling operation + * @param maxAmount max amount of fluid to transfer. method become noop if this is not a positive integer + * @param allowMove filter. can be null to signal all fluids are accepted + */ + public static void moveFluid(IFluidHandler source, IFluidHandler dest, ForgeDirection drainSide, + ForgeDirection fillSide, int maxAmount, @Nullable Predicate allowMove) { + if (source == null || dest == null || maxAmount <= 0) return; + FluidStack liquid = source.drain(drainSide, maxAmount, false); + if (liquid == null) return; + liquid = liquid.copy(); + liquid.amount = dest.fill(fillSide, liquid, false); + if (liquid.amount > 0 && (allowMove == null || allowMove.test(liquid))) { + dest.fill(fillSide, source.drain(drainSide, liquid.amount, true), true); + } + } + public static boolean listContainsItem(Collection aList, ItemStack aStack, boolean aTIfListEmpty, boolean aInvertFilter) { if (aStack == null || aStack.stackSize < 1) return false; -- cgit