Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework OverlayedFluidHandler to fix Fluid Parallel Limiting #2423

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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