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

Move from LDLib's FluidStack implementation to Forge's FluidStack #1975

Merged
merged 23 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Expand Up @@ -18,12 +18,10 @@
import com.gregtechceu.gtceu.common.machine.owner.IMachineOwner;
import com.gregtechceu.gtceu.common.pipelike.fluidpipe.longdistance.LDFluidEndpointMachine;
import com.gregtechceu.gtceu.common.pipelike.item.longdistance.LDItemEndpointMachine;
import com.gregtechceu.gtceu.utils.GTTransferUtils;

import com.lowdragmc.lowdraglib.client.renderer.IRenderer;
import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
import com.lowdragmc.lowdraglib.side.fluid.FluidTransferHelper;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.fluid.forge.FluidTransferHelperImpl;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.side.item.forge.ItemTransferHelperImpl;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
Expand Down Expand Up @@ -224,17 +222,16 @@ public static <T> LazyOptional<T> getCapability(MetaMachine machine, @NotNull Ca
ILDEndpoint endpoint = fluidEndpointMachine.getLink();
if (endpoint == null) return null;
Direction outputFacing = fluidEndpointMachine.getOutputFacing();
IFluidTransfer transfer = FluidTransferHelper.getFluidTransfer(machine.getLevel(),
endpoint.getPos().relative(outputFacing), outputFacing.getOpposite());
if (transfer != null) {
return ForgeCapabilities.FLUID_HANDLER.orEmpty(cap, LazyOptional.of(() -> FluidTransferHelperImpl
.toFluidHandler(new LDFluidEndpointMachine.FluidHandlerWrapper(transfer))));
var h = GTTransferUtils.getAdjacentFluidHandler(machine.getLevel(), endpoint.getPos(), outputFacing)
.map(LDFluidEndpointMachine.FluidHandlerWrapper::new);
if (h.isPresent()) {
return ForgeCapabilities.FLUID_HANDLER.orEmpty(cap, LazyOptional.of(h::get));
}
}
var transfer = machine.getFluidTransferCap(side, true);
if (transfer != null) {
var handler = machine.getFluidHandlerCap(side, true);
if (handler != null) {
return ForgeCapabilities.FLUID_HANDLER.orEmpty(cap,
LazyOptional.of(() -> FluidTransferHelperImpl.toFluidHandler(transfer)));
LazyOptional.of(() -> handler));
}
} else if (cap == ForgeCapabilities.ENERGY) {
if (machine instanceof IEnergyStorage energyStorage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.gregtechceu.gtceu.api.blockentity.ITickSubscription;
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
import com.gregtechceu.gtceu.api.cover.CoverDefinition;
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
import com.gregtechceu.gtceu.utils.GTUtil;

import com.lowdragmc.lowdraglib.LDLib;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;

import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -61,7 +61,7 @@ public interface ICoverable extends ITickSubscription, IAppearance {

IItemTransfer getItemTransferCap(@Nullable Direction side, boolean useCoverCapability);

IFluidTransfer getFluidTransferCap(@Nullable Direction side, boolean useCoverCapability);
IFluidHandlerModifiable getFluidHandlerCap(@Nullable Direction side, boolean useCoverCapability);

/**
* Its an internal method, you should never call it yourself.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import com.gregtechceu.gtceu.utils.FormattingUtil;
import com.gregtechceu.gtceu.utils.GTUtil;

import com.lowdragmc.lowdraglib.side.fluid.FluidHelper;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;

import net.minecraft.network.chat.Component;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnmodifiableView;
Expand All @@ -26,7 +25,8 @@ public interface IPropertyFluidFilter extends Predicate<FluidStack> {
@Override
default boolean test(@NotNull FluidStack stack) {
Fluid fluid = stack.getFluid();
if (FluidHelper.getTemperature(stack) < CRYOGENIC_FLUID_THRESHOLD && !isCryoProof()) return false;
FluidType fluidType = fluid.getFluidType();
if (fluidType.getTemperature() < CRYOGENIC_FLUID_THRESHOLD && !isCryoProof()) return false;

if (fluid instanceof IAttributedFluid attributedFluid) {
FluidState state = attributedFluid.getState();
Expand All @@ -41,15 +41,15 @@ default boolean test(@NotNull FluidStack stack) {
// plasma ignores temperature requirements
if (state == FluidState.PLASMA) return true;
} else {
if (FluidHelper.isLighterThanAir(stack) && !canContain(FluidState.GAS)) {
if (fluidType.isLighterThanAir() && !canContain(FluidState.GAS)) {
return false;
}
if (!canContain(FluidState.LIQUID)) {
return false;
}
}

return FluidHelper.getTemperature(stack) <= getMaxFluidTemperature();
return fluidType.getTemperature() <= getMaxFluidTemperature();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import com.gregtechceu.gtceu.api.fluids.attribute.FluidAttributes;
import com.gregtechceu.gtceu.api.fluids.attribute.IAttributedFluid;

import com.lowdragmc.lowdraglib.side.fluid.FluidHelper;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;

import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidType;

/**
* Interface for FluidHandlerItemStacks which handle GT's unique fluid mechanics
Expand All @@ -26,12 +24,12 @@ public interface IThermalFluidHandlerItemStack {
default boolean canFillFluidType(FluidStack stack) {
if (stack == null || stack.getFluid() == null) return false;

Fluid fluid = stack.getFluid();
var temp = FluidHelper.getTemperature(stack);
FluidType fluidType = stack.getFluid().getFluidType();
var temp = fluidType.getTemperature();
if (temp > getMaxFluidTemperature()) return false;
// fluids less than 120K are cryogenic
if (temp < 120 && !isCryoProof()) return false;
if (FluidHelper.isLighterThanAir(stack) && !isGasProof()) return false;
if (fluidType.isLighterThanAir() && !isGasProof()) return false;
krossgg marked this conversation as resolved.
Show resolved Hide resolved

// TODO custom fluid
// for (RegistryEntry<Fluid> entry : GTRegistries.REGISTRATE.getAll(Registry.FLUID_REGISTRY)) {
Expand Down
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.gui.widget.TankWidget;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.api.recipe.content.Content;
Expand All @@ -11,6 +12,9 @@
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;
Expand All @@ -19,24 +23,21 @@
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.FluidStack;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.utils.TagOrCycleFluidTransfer;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.TagKey;
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;
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 @@ -82,7 +83,7 @@ public List<AbstractMapIngredient> convertToMapIngredient(Object obj) {
Collection<Fluid> fluids = value.getFluids();
for (Fluid fluid : fluids) {
ingredients.add(new MapFluidIngredient(
FluidStack.create(fluid, ingredient.getAmount(), ingredient.getNbt())));
new FluidStack(fluid, ingredient.getAmount(), ingredient.getNbt())));
}
}
}
Expand Down Expand Up @@ -149,12 +150,12 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
int minMultiplier = 0;
int maxMultiplier = multiplier;

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

List<FluidStack> recipeOutputs = recipe.getOutputContents(FluidRecipeCapability.CAP)
Expand All @@ -167,8 +168,8 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
while (minMultiplier != maxMultiplier) {
overlayedFluidHandler.reset();

long returnedAmount = 0;
long amountToInsert = 0;
int returnedAmount = 0;
int amountToInsert = 0;

for (FluidStack fluidStack : recipeOutputs) {
if (fluidStack.getAmount() <= 0) continue;
Expand Down Expand Up @@ -197,24 +198,24 @@ public int limitParallel(GTRecipe recipe, IRecipeCapabilityHolder holder, int mu
@Override
public int getMaxParallelRatio(IRecipeCapabilityHolder holder, GTRecipe recipe, int parallelAmount) {
// Find all the fluids in the combined Fluid Input inventories and create oversized FluidStacks
Map<FluidKey, Long> fluidStacks = Objects
Map<FluidKey, Integer> fluidStacks = Objects
.requireNonNullElseGet(holder.getCapabilitiesProxy().get(IO.IN, FluidRecipeCapability.CAP),
Collections::<IRecipeHandler<?>>emptyList)
.stream()
.map(container -> container.getContents().stream().filter(FluidStack.class::isInstance)
.map(FluidStack.class::cast).toList())
.flatMap(container -> GTHashMaps.fromFluidCollection(container).entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Long::sum,
Object2LongLinkedOpenHashMap::new));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::sum,
Object2IntLinkedOpenHashMap::new));

int minMultiplier = Integer.MAX_VALUE;
// map the recipe input fluids to account for duplicated fluids,
// so their sum is counted against the total of fluids available in the input
Map<FluidIngredient, Long> fluidCountMap = new HashMap<>();
Map<FluidIngredient, Long> notConsumableMap = new HashMap<>();
Map<FluidIngredient, Integer> fluidCountMap = new HashMap<>();
Map<FluidIngredient, Integer> notConsumableMap = new HashMap<>();
for (Content content : recipe.getInputContents(FluidRecipeCapability.CAP)) {
FluidIngredient fluidInput = FluidRecipeCapability.CAP.of(content.content);
long fluidAmount = fluidInput.getAmount();
int fluidAmount = fluidInput.getAmount();
if (content.chance == 0) {
notConsumableMap.computeIfPresent(fluidInput,
(k, v) -> v + fluidAmount);
Expand All @@ -227,23 +228,23 @@ public int getMaxParallelRatio(IRecipeCapabilityHolder holder, GTRecipe recipe,
}

// Iterate through the recipe inputs, excluding the not consumable fluids from the fluid inventory map
for (Map.Entry<FluidIngredient, Long> notConsumableFluid : notConsumableMap.entrySet()) {
long needed = notConsumableFluid.getValue();
long available = 0;
for (Map.Entry<FluidIngredient, Integer> notConsumableFluid : notConsumableMap.entrySet()) {
int needed = notConsumableFluid.getValue();
int available = 0;
// For every fluid gathered from the fluid inputs.
for (Map.Entry<FluidKey, Long> inputFluid : fluidStacks.entrySet()) {
for (Map.Entry<FluidKey, Integer> inputFluid : fluidStacks.entrySet()) {
// Strip the Non-consumable tags here, as FluidKey compares the tags, which causes finding matching
// fluids
// in the input tanks to fail, because there is nothing in those hatches with a non-consumable tag
if (notConsumableFluid.getKey().test(
FluidStack.create(inputFluid.getKey().fluid, inputFluid.getValue(), inputFluid.getKey().tag))) {
new FluidStack(inputFluid.getKey().fluid, inputFluid.getValue(), inputFluid.getKey().tag))) {
available = inputFluid.getValue();
if (available > needed) {
inputFluid.setValue(available - needed);
needed -= available;
break;
} else {
inputFluid.setValue(0L);
inputFluid.setValue(0);
notConsumableFluid.setValue(needed - available);
needed -= available;
}
Expand All @@ -268,13 +269,13 @@ public int getMaxParallelRatio(IRecipeCapabilityHolder holder, GTRecipe recipe,
}

// Iterate through the fluid inputs in the recipe
for (Map.Entry<FluidIngredient, Long> fs : fluidCountMap.entrySet()) {
long needed = fs.getValue();
long available = 0;
for (Map.Entry<FluidIngredient, Integer> fs : fluidCountMap.entrySet()) {
int needed = fs.getValue();
int available = 0;
// For every fluid gathered from the fluid inputs.
for (Map.Entry<FluidKey, Long> inputFluid : fluidStacks.entrySet()) {
for (Map.Entry<FluidKey, Integer> inputFluid : fluidStacks.entrySet()) {
if (fs.getKey().test(
FluidStack.create(inputFluid.getKey().fluid, inputFluid.getValue(), inputFluid.getKey().tag))) {
new FluidStack(inputFluid.getKey().fluid, inputFluid.getValue(), inputFluid.getKey().tag))) {
available += inputFluid.getValue();
}
}
Expand All @@ -301,7 +302,8 @@ public int getMaxParallelRatio(IRecipeCapabilityHolder holder, GTRecipe recipe,
public Object createXEIContainer(List<?> contents) {
// cast is safe if you don't pass the wrong thing.
// noinspection unchecked
return new TagOrCycleFluidTransfer((List<Either<List<Pair<TagKey<Fluid>, Long>>, List<FluidStack>>>) contents);
return new TagOrCycleFluidHandler(
(List<Either<List<Pair<TagKey<Fluid>, Integer>>, List<FluidStack>>>) contents);
}

@NotNull
Expand Down Expand Up @@ -330,10 +332,10 @@ public void applyWidgetInfo(@NotNull Widget widget,
@Nullable Content content,
@Nullable Object storage) {
if (widget instanceof TankWidget tank) {
if (storage instanceof TagOrCycleFluidTransfer fluidTransfer) {
tank.setFluidTank(fluidTransfer, index);
} else if (storage instanceof IFluidTransfer fluidTransfer) {
tank.setFluidTank(new OverlayingFluidStorage(fluidTransfer, index));
if (storage instanceof TagOrCycleFluidHandler fluidHandler) {
tank.setFluidTank(fluidHandler, index);
} else if (storage instanceof IFluidHandlerModifiable fluidHandler) {
tank.setFluidTank(new OverlayingFluidStorage(fluidHandler, index));
}
tank.setIngredientIO(io == IO.IN ? IngredientIO.INPUT : IngredientIO.OUTPUT);
tank.setAllowClickFilled(!isXEI);
Expand Down Expand Up @@ -361,17 +363,17 @@ public void applyWidgetInfo(@NotNull Widget widget,
}

// Maps fluids to Either<(tag with count), FluidStack>s
public static Either<List<Pair<TagKey<Fluid>, Long>>, List<FluidStack>> mapFluid(FluidIngredient ingredient) {
long amount = ingredient.getAmount();
public static Either<List<Pair<TagKey<Fluid>, Integer>>, List<FluidStack>> mapFluid(FluidIngredient ingredient) {
int amount = ingredient.getAmount();
CompoundTag tag = ingredient.getNbt();

List<Pair<TagKey<Fluid>, Long>> tags = new ArrayList<>();
List<Pair<TagKey<Fluid>, Integer>> tags = new ArrayList<>();
List<FluidStack> fluids = new ArrayList<>();
for (FluidIngredient.Value value : ingredient.values) {
if (value instanceof FluidIngredient.TagValue tagValue) {
tags.add(Pair.of(tagValue.getTag(), amount));
} else {
fluids.addAll(value.getFluids().stream().map(fluid -> FluidStack.create(fluid, amount, tag)).toList());
fluids.addAll(value.getFluids().stream().map(fluid -> new FluidStack(fluid, amount, tag)).toList());
}
}
if (!tags.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import com.gregtechceu.gtceu.api.gui.fancy.IFancyConfigurator;
import com.gregtechceu.gtceu.api.item.tool.GTToolType;
import com.gregtechceu.gtceu.api.item.tool.IToolGridHighLight;
import com.gregtechceu.gtceu.api.transfer.fluid.IFluidHandlerModifiable;
import com.gregtechceu.gtceu.client.renderer.cover.ICoverRenderer;

import com.lowdragmc.lowdraglib.gui.texture.ResourceTexture;
import com.lowdragmc.lowdraglib.side.fluid.IFluidTransfer;
import com.lowdragmc.lowdraglib.side.item.IItemTransfer;
import com.lowdragmc.lowdraglib.syncdata.IEnhancedManaged;
import com.lowdragmc.lowdraglib.syncdata.annotation.DescSynced;
Expand Down Expand Up @@ -222,7 +222,7 @@ public IItemTransfer getItemTransferCap(IItemTransfer defaultValue) {
}

@Nullable
public IFluidTransfer getFluidTransferCap(IFluidTransfer defaultValue) {
public IFluidHandlerModifiable getFluidHandlerCap(IFluidHandlerModifiable defaultValue) {
return defaultValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.gregtechceu.gtceu.api.cover.filter;

import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import com.lowdragmc.lowdraglib.syncdata.IEnhancedManaged;

import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

public interface FilterHandlers {

Expand Down
Loading