Skip to content

Commit

Permalink
Rework OverlayedFluidHandler to fix Fluid Parallel Limiting (#2423)
Browse files Browse the repository at this point in the history
  • Loading branch information
krossgg authored and screret committed Dec 27, 2024
1 parent 251e12c commit 99e7ae7
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gregtechceu.gtceu.api.capability.recipe;

import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.content.Content;
Expand All @@ -13,14 +14,13 @@
import com.gregtechceu.gtceu.integration.GTRecipeWidget;
import com.gregtechceu.gtceu.utils.FluidKey;
import com.gregtechceu.gtceu.utils.GTHashMaps;
import com.gregtechceu.gtceu.utils.OverlayedFluidHandler;
import com.gregtechceu.gtceu.utils.OverlayedTankHandler;
import com.gregtechceu.gtceu.utils.OverlayingFluidStorage;

import com.lowdragmc.lowdraglib.gui.texture.ProgressTexture;
import com.lowdragmc.lowdraglib.gui.widget.TankWidget;
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.jei.IngredientIO;
import com.lowdragmc.lowdraglib.misc.FluidTransferList;
import com.lowdragmc.lowdraglib.side.fluid.IFluidHandlerModifiable;
import com.lowdragmc.lowdraglib.utils.TagOrCycleFluidTransfer;

Expand All @@ -30,14 +30,11 @@
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.material.Fluid;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.crafting.*;

import com.mojang.datafixers.util.Either;
import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.Object2IntLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2LongLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -158,13 +155,14 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
int minMultiplier = 0;
int maxMultiplier = multiplier;

OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(new FluidTransferList(
Objects.requireNonNullElseGet(holder.getCapabilitiesProxy().get(IO.OUT, FluidRecipeCapability.CAP),
OverlayedTankHandler overlayedFluidHandler = new OverlayedTankHandler(
Objects.requireNonNullElseGet(
holder.getCapabilitiesProxy().get(IO.OUT, FluidRecipeCapability.CAP),
Collections::emptyList)
.stream()
.filter(IFluidHandler.class::isInstance)
.map(IFluidHandler.class::cast)
.toList()));
.filter(NotifiableFluidTank.class::isInstance)
.map(NotifiableFluidTank.class::cast)
.toList());

List<FluidStack> recipeOutputs = recipe.getOutputContents(FluidRecipeCapability.CAP)
.stream()
Expand All @@ -188,7 +186,7 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
} else {
amountToInsert = fluidStack.getAmount() * multiplier;
}
returnedAmount = amountToInsert - overlayedFluidHandler.insertFluid(fluidStack, amountToInsert);
returnedAmount = amountToInsert - overlayedFluidHandler.tryFill(fluidStack, amountToInsert);
if (returnedAmount > 0) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import net.minecraft.core.Direction;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.FluidType;
import net.neoforged.neoforge.fluids.crafting.SizedFluidIngredient;

import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -45,16 +45,18 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<SizedFluid
public final IO capabilityIO;
@Persisted(subPersisted = true)
@Getter
private final CustomFluidTank[] storages;
@Setter
protected final CustomFluidTank[] storages;
@Getter
protected boolean allowSameFluids; // Can different tanks be filled with the same fluid. It should be determined
// while creating tanks.
private Boolean isEmpty;

@Persisted
@DescSynced
@Getter
protected CustomFluidTank lockedFluid = new CustomFluidTank(FluidHelper.getBucket());
protected CustomFluidTank lockedFluid = new CustomFluidTank(FluidType.BUCKET_VOLUME);
@Getter
protected Predicate<FluidStack> filter = f -> true;

public NotifiableFluidTank(MetaMachine machine, int slots, int capacity, IO io, IO capabilityIO) {
super(machine);
Expand Down Expand Up @@ -209,7 +211,8 @@ public void setLocked(boolean locked, FluidStack fluidStack) {
}

public NotifiableFluidTank setFilter(Predicate<FluidStack> filter) {
for (CustomFluidTank storage : getStorages()) {
this.filter = filter;
for (CustomFluidTank storage : storages) {
storage.setValidator(filter);
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public InaccessibleInfiniteTank(MetaMachine holder) {
super(holder, List.of(new FluidStorageDelegate()), IO.OUT, IO.NONE);
internalBuffer.setOnContentsChanged(this::onContentsChanged);
storage = getStorages()[0];
allowSameFluids = true;
}

@Override
Expand Down
42 changes: 4 additions & 38 deletions src/main/java/com/gregtechceu/gtceu/utils/GTTransferUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.IFluidHandler.FluidAction;
import net.neoforged.neoforge.items.IItemHandler;
import net.neoforged.neoforge.items.IItemHandlerModifiable;

Expand Down Expand Up @@ -135,44 +136,9 @@ public static boolean addItemsToItemHandler(final IItemHandlerModifiable handler
return true;
}

/**
* Simulates the insertion of fluid into a target fluid handler, then optionally performs the insertion.
* <br />
* <br />
* Simulating will not modify any of the input parameters. Insertion will either succeed completely, or fail
* without modifying anything.
* This method should be called with {@code simulate} {@code true} first, then {@code simulate} {@code false},
* only if it returned {@code true}.
*
* @param fluidHandler the target inventory
* @param simulate whether to simulate ({@code true}) or actually perform the insertion ({@code false})
* @param fluidStacks the items to insert into {@code fluidHandler}.
* @return {@code true} if the insertion succeeded, {@code false} otherwise.
*/
public static boolean addFluidsToFluidHandler(FluidTransferList fluidHandler,
boolean simulate,
List<FluidStack> fluidStacks) {
if (simulate) {
OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(fluidHandler);
for (FluidStack fluidStack : fluidStacks) {
long inserted = overlayedFluidHandler.insertFluid(fluidStack, fluidStack.getAmount());
if (inserted != fluidStack.getAmount()) {
return false;
}
}
return true;
}

for (FluidStack fluidStack : fluidStacks) {
fillFluidAccountNotifiableList(fluidHandler, fluidStack, IFluidHandler.FluidAction.EXECUTE);
}
return true;
}

public static long fillFluidAccountNotifiableList(IFluidHandler handler, FluidStack stack,
IFluidHandler.FluidAction action) {
public static int fillFluidAccountNotifiableList(IFluidHandler fluidHandler, FluidStack stack, FluidAction action) {
if (stack.isEmpty()) return 0;
if (handler instanceof FluidTransferList transferList) {
if (fluidHandler instanceof FluidTransferList transferList) {
var copied = stack.copy();
for (var transfer : transferList.transfers) {
var candidate = copied.copy();
Expand All @@ -185,7 +151,7 @@ public static long fillFluidAccountNotifiableList(IFluidHandler handler, FluidSt
}
return stack.getAmount() - copied.getAmount();
}
return handler.fill(stack, action);
return fluidHandler.fill(stack, action);
}

public static FluidStack drainFluidAccountNotifiableList(IFluidHandler handler, FluidStack stack,
Expand Down
176 changes: 0 additions & 176 deletions src/main/java/com/gregtechceu/gtceu/utils/OverlayedFluidHandler.java

This file was deleted.

Loading

0 comments on commit 99e7ae7

Please sign in to comment.