|
28 | 28 | import net.minecraft.world.entity.player.Inventory; |
29 | 29 | import net.minecraft.world.item.ItemStack; |
30 | 30 | import net.neoforged.neoforge.capabilities.Capabilities; |
| 31 | +import net.neoforged.neoforge.fluids.FluidStack; |
| 32 | +import net.neoforged.neoforge.fluids.capability.IFluidHandler; |
31 | 33 | import net.neoforged.neoforge.items.IItemHandler; |
32 | 34 | import net.neoforged.neoforge.items.ItemHandlerHelper; |
33 | 35 | import net.neoforged.neoforge.items.wrapper.PlayerInvWrapper; |
@@ -128,4 +130,45 @@ public static ItemStack extractMatching(IItemHandler src, Predicate<ItemStack> p |
128 | 130 |
|
129 | 131 | return ret; |
130 | 132 | } |
| 133 | + |
| 134 | + /** |
| 135 | + * Fixed version of NeoForge's FluidUtil method. This one correctly handles multi-tank to multi-tank transfer. |
| 136 | + * |
| 137 | + * <p> |
| 138 | + * Fill a destination fluid handler from a source fluid handler with a max amount. |
| 139 | + * To transfer as much as possible, use {@link Integer#MAX_VALUE} for maxAmount. |
| 140 | + * |
| 141 | + * @param fluidDestination The fluid handler to be filled. |
| 142 | + * @param fluidSource The fluid handler to be drained. |
| 143 | + * @param maxAmount The largest amount of fluid that should be transferred. |
| 144 | + * @param doTransfer True if the transfer should actually be done, false if it should be simulated. |
| 145 | + * @return the fluidStack that was transferred from the source to the destination. null on failure. |
| 146 | + */ |
| 147 | + public static FluidStack tryFluidTransfer(IFluidHandler fluidDestination, IFluidHandler fluidSource, int maxAmount, boolean doTransfer) { |
| 148 | + int tanks = fluidSource.getTanks(); |
| 149 | + for (int i = 0; i < tanks; ++i) { |
| 150 | + FluidStack toTry = fluidSource.getFluidInTank(i).copy(); |
| 151 | + if (toTry.getAmount() > maxAmount) { |
| 152 | + toTry.setAmount(maxAmount); |
| 153 | + } |
| 154 | + FluidStack drainable = fluidSource.drain(toTry, IFluidHandler.FluidAction.SIMULATE); |
| 155 | + if (drainable.isEmpty()) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + int fillableAmount = fluidDestination.fill(drainable, IFluidHandler.FluidAction.SIMULATE); |
| 159 | + if (fillableAmount > 0) { |
| 160 | + drainable.setAmount(fillableAmount); |
| 161 | + if (doTransfer) { |
| 162 | + FluidStack drained = fluidSource.drain(drainable, IFluidHandler.FluidAction.EXECUTE); |
| 163 | + if (!drained.isEmpty()) { |
| 164 | + drained.setAmount(fluidDestination.fill(drained, IFluidHandler.FluidAction.EXECUTE)); |
| 165 | + return drained; |
| 166 | + } |
| 167 | + } else { |
| 168 | + return drainable; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + return FluidStack.EMPTY; |
| 173 | + } |
131 | 174 | } |
0 commit comments