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 5, 2024
1 parent b1ac23f commit 84a62a1
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gregtechceu.gtceu.api.capability.recipe;

import com.gregtechceu.gtceu.api.gui.widget.TankWidget;
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 @@ -10,14 +11,13 @@
import com.gregtechceu.gtceu.api.recipe.lookup.ingredient.fluid.*;
import com.gregtechceu.gtceu.api.recipe.modifier.ParallelLogic;
import com.gregtechceu.gtceu.api.recipe.ui.GTRecipeTypeUI;
import com.gregtechceu.gtceu.api.transfer.fluid.FluidHandlerList;
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
import com.gregtechceu.gtceu.api.transfer.fluid.TagOrCycleFluidHandler;
import com.gregtechceu.gtceu.client.TooltipsHandler;
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;
Expand Down Expand Up @@ -160,13 +160,14 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
int minMultiplier = 0;
int maxMultiplier = multiplier;

OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(new FluidHandlerList(
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 @@ -190,7 +191,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 @@ -20,7 +20,6 @@
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 @@ -44,7 +43,7 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<SizedFluid
@Persisted(subPersisted = true)
@Getter
protected final CustomFluidTank[] storages;
@Setter
@Getter
protected boolean allowSameFluids; // Can different tanks be filled with the same fluid. It should be determined
// while creating tanks.
private Boolean isEmpty;
Expand All @@ -53,6 +52,8 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<SizedFluid
@DescSynced
@Getter
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 @@ -224,6 +225,7 @@ public void setLocked(boolean locked, FluidStack fluidStack) {
}

public NotifiableFluidTank setFilter(Predicate<FluidStack> filter) {
this.filter = filter;
for (CustomFluidTank storage : storages) {
storage.setValidator(filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ public void setFluidInTank(int tank, FluidStack stack) {
this.onContentsChanged();
}

public int fill(int tank, FluidStack resource, FluidAction action) {
return this.fill(resource, action);
}

public FluidStack drain(int tank, FluidStack resource, FluidAction action) {
return this.drain(resource, action);
}

@Override
public CompoundTag serializeNBT(HolderLookup.Provider provider) {
return writeToNBT(provider, new CompoundTag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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
35 changes: 1 addition & 34 deletions src/main/java/com/gregtechceu/gtceu/utils/GTTransferUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.neoforged.neoforge.capabilities.Capabilities;
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 @@ -184,40 +185,6 @@ 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(FluidHandlerList fluidHandler,
boolean simulate,
List<FluidStack> fluidStacks) {
if (simulate) {
OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(fluidHandler);
for (FluidStack fluidStack : fluidStacks) {
int inserted = overlayedFluidHandler.insertFluid(fluidStack, fluidStack.getAmount());
if (inserted != fluidStack.getAmount()) {
return false;
}
}
return true;
}

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

public static int fillFluidAccountNotifiableList(IFluidHandler fluidHandler, FluidStack stack, FluidAction action) {
if (stack.isEmpty()) return 0;
if (fluidHandler instanceof FluidHandlerList handlerList) {
Expand Down
177 changes: 0 additions & 177 deletions src/main/java/com/gregtechceu/gtceu/utils/OverlayedFluidHandler.java

This file was deleted.

Loading

0 comments on commit 84a62a1

Please sign in to comment.