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 Nov 25, 2024
1 parent b8f83e9 commit 098bedb
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 225 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 @@ -12,14 +13,13 @@
import com.gregtechceu.gtceu.api.recipe.lookup.MapFluidTagIngredient;
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 All @@ -32,7 +32,6 @@
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;

import com.mojang.datafixers.util.Either;
import com.mojang.datafixers.util.Pair;
Expand Down Expand Up @@ -152,13 +151,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 @@ -182,7 +182,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 @@ -19,7 +19,6 @@
import net.minecraftforge.fluids.FluidType;

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

Expand All @@ -43,7 +42,7 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<FluidIngre
@Persisted
@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 @@ -52,6 +51,8 @@ public class NotifiableFluidTank extends NotifiableRecipeHandlerTrait<FluidIngre
@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 @@ -222,6 +223,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 @@ -50,14 +50,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() {
return writeToNBT(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
34 changes: 0 additions & 34 deletions src/main/java/com/gregtechceu/gtceu/utils/GTTransferUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,40 +195,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
172 changes: 0 additions & 172 deletions src/main/java/com/gregtechceu/gtceu/utils/OverlayedFluidHandler.java

This file was deleted.

Loading

0 comments on commit 098bedb

Please sign in to comment.