From 48b161e798c8eae0a1d43f6d31fdf4abe50a279b Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:55:04 -0700 Subject: [PATCH 01/26] new fluid tank list --- .../api/capability/impl/FluidTankList2.java | 330 ++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 src/main/java/gregtech/api/capability/impl/FluidTankList2.java diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java new file mode 100644 index 00000000000..fd1c2d3e604 --- /dev/null +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -0,0 +1,330 @@ +package gregtech.api.capability.impl; + +import gregtech.api.capability.IFilter; +import gregtech.api.capability.IFilteredFluidContainer; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraftforge.common.util.Constants; +import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; +import net.minecraftforge.fluids.capability.IFluidHandler; +import net.minecraftforge.fluids.capability.IFluidTankProperties; + +import com.google.common.collect.AbstractIterator; +import org.apache.commons.lang3.ArrayUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +public class FluidTankList2 implements IFluidHandler, INBTSerializable, Iterable { + + public static Comparator ENTRY_COMPARATOR = (o1, o2) -> { + // #1: non-empty tank first + boolean empty1 = o1.getFluidAmount() <= 0; + boolean empty2 = o2.getFluidAmount() <= 0; + if (empty1 != empty2) return empty1 ? 1 : -1; + + // #2: filter priority + IFilter filter1 = o1.getFilter(); + IFilter filter2 = o2.getFilter(); + if (filter1 == null) return filter2 == null ? 0 : 1; + if (filter2 == null) return -1; + return IFilter.FILTER_COMPARATOR.compare(filter1, filter2); + }; + + private final boolean allowSameFluidFill; + private TankWrapper[] tanks = new TankWrapper[0]; + private IFluidTankProperties[] properties = new IFluidTankProperties[0]; + + public FluidTankList2(boolean allowSameFluidFill, IFluidTank... fluidTanks) { + if (!ArrayUtils.isEmpty(fluidTanks)) { + tanks = new TankWrapper[fluidTanks.length]; + properties = new IFluidTankProperties[fluidTanks.length]; + Arrays.setAll(tanks, value -> { + var tank = wrap(fluidTanks[value]); + properties[value] = createProp(tank); + return tank; + }); + } + this.allowSameFluidFill = allowSameFluidFill; + } + + public FluidTankList2(boolean allowSameFluidFill, @NotNull List fluidTanks) { + this(allowSameFluidFill, fluidTanks.toArray(new IFluidTank[0])); + } + + @Override + public IFluidTankProperties[] getTankProperties() { + return properties; + } + + @Override + public int fill(FluidStack resource, boolean doFill) { + if (resource == null || resource.amount <= 0) { + return 0; + } + int totalInserted = 0; + boolean inputFluidCopied = false; + // flag value indicating whether the fluid was stored in 'distinct' slot at least once + boolean distinctSlotVisited = false; + + var fluidTanks = this.tanks.clone(); + Arrays.sort(fluidTanks, ENTRY_COMPARATOR); + + // search for tanks with same fluid type first + for (var tank : fluidTanks) { + // if the fluid to insert matches the tank, insert the fluid + if (resource.isFluidEqual(tank.getFluid())) { + int inserted = tank.fill(resource, doFill); + if (inserted > 0) { + totalInserted += inserted; + if (resource.amount - inserted <= 0) { + return totalInserted; + } + if (!inputFluidCopied) { + inputFluidCopied = true; + resource = resource.copy(); + } + resource.amount -= inserted; + } + // regardless of whether the insertion succeeded, presence of identical fluid in + // a slot prevents distinct fill to other slots + if (!tank.allowSameFluidFill) { + distinctSlotVisited = true; + } + } + } + // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid + for (var tank : fluidTanks) { + // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had + // received the fluid, skip this tank + boolean usesDistinctFluidFill = tank.allowSameFluidFill; + if ((usesDistinctFluidFill || !distinctSlotVisited) && tank.getFluidAmount() == 0) { + int inserted = tank.fill(resource, doFill); + if (inserted > 0) { + totalInserted += inserted; + if (resource.amount - inserted <= 0) { + return totalInserted; + } + if (!inputFluidCopied) { + inputFluidCopied = true; + resource = resource.copy(); + } + resource.amount -= inserted; + if (!usesDistinctFluidFill) { + distinctSlotVisited = true; + } + } + } + } + // return the amount of fluid that was inserted + return totalInserted; + } + + @Nullable + @Override + public FluidStack drain(FluidStack resource, boolean doDrain) { + if (resource == null || resource.amount <= 0) { + return null; + } + int amountLeft = resource.amount; + FluidStack totalDrained = null; + for (IFluidTank handler : tanks) { + if (!resource.isFluidEqual(handler.getFluid())) { + continue; + } + FluidStack drain = handler.drain(amountLeft, doDrain); + if (drain != null) { + if (totalDrained == null) { + totalDrained = drain; + } else { + totalDrained.amount += drain.amount; + } + amountLeft -= drain.amount; + if (amountLeft <= 0) { + return totalDrained; + } + } + } + return totalDrained; + } + + @Nullable + @Override + public FluidStack drain(int maxDrain, boolean doDrain) { + if (maxDrain <= 0) { + return null; + } + FluidStack totalDrained = null; + for (IFluidTank handler : tanks) { + if (totalDrained == null) { + var drained = handler.drain(maxDrain, doDrain); + if (drained != null) { + totalDrained = drained.copy(); + maxDrain -= totalDrained.amount; + } + } else { + if (!totalDrained.isFluidEqual(handler.getFluid())) { + continue; + } + FluidStack drain = handler.drain(maxDrain, doDrain); + if (drain != null) { + totalDrained.amount += drain.amount; + maxDrain -= drain.amount; + } + } + if (maxDrain <= 0) { + return totalDrained; + } + } + return totalDrained; + } + + @Override + public NBTTagCompound serializeNBT() { + NBTTagCompound fluidInventory = new NBTTagCompound(); + NBTTagList tanks = new NBTTagList(); + for (TankWrapper tank : this.tanks) { + tanks.appendTag(tank.serializeNBT()); + } + fluidInventory.setTag("Tanks", tanks); + return fluidInventory; + } + + @Override + public void deserializeNBT(NBTTagCompound nbt) { + NBTTagList tanks = nbt.getTagList("Tanks", Constants.NBT.TAG_COMPOUND); + for (int i = 0; i < tanks.tagCount(); i++) { + this.tanks[i].deserializeNBT(tanks.getCompoundTagAt(i)); + } + } + + @Override + public @NotNull Iterator iterator() { + return new AbstractIterator<>() { + + final int length = tanks.length; + int index = 0; + + @Override + protected IFluidTank computeNext() { + return index < length ? tanks[index++] : endOfData(); + } + }; + } + + private TankWrapper wrap(IFluidTank tank) { + return tank instanceof TankWrapper ? (TankWrapper) tank : new TankWrapper(tank, allowSameFluidFill); + } + + private IFluidTankProperties createProp(IFluidTank tank) { + return new IFluidTankProperties() { + + @Override + public FluidStack getContents() { + return tank.getFluid() == null ? null : tank.getFluid().copy(); + } + + @Override + public int getCapacity() { + return tank.getCapacity(); + } + + @Override + public boolean canFill() { + return allowSameFluidFill; + } + + @Override + public boolean canDrain() { + return true; + } + + @Override + public boolean canFillFluidType(FluidStack fluidStack) { + // special logic + return false; + } + + @Override + public boolean canDrainFluidType(FluidStack fluidStack) { + return true; + } + }; + } + + public static class TankWrapper implements IFluidTank, IFilteredFluidContainer, INBTSerializable { + + final IFluidTank tank; + private final boolean allowSameFluidFill; + + private TankWrapper(IFluidTank tank, boolean allowSameFluidFill) { + this.allowSameFluidFill = allowSameFluidFill; + this.tank = tank; + } + + @Override + public FluidStack getFluid() { + return tank.getFluid(); + } + + @Override + public int getFluidAmount() { + return tank.getFluidAmount(); + } + + @Override + public int getCapacity() { + return tank.getCapacity(); + } + + @Override + public FluidTankInfo getInfo() { + return tank.getInfo(); + } + + @Override + public int fill(FluidStack resource, boolean doFill) { + return tank.fill(resource, doFill); + } + + @Override + public FluidStack drain(int maxDrain, boolean doDrain) { + return tank.drain(maxDrain, doDrain); + } + + @Override + public @Nullable IFilter getFilter() { + return tank instanceof IFilteredFluidContainer filter ? filter.getFilter() : null; + } + + @SuppressWarnings("rawtypes") + @Override + public NBTTagCompound serializeNBT() { + if (tank instanceof FluidTank fluidTank) { + return fluidTank.writeToNBT(new NBTTagCompound()); + } else if (tank instanceof INBTSerializable serializable) { + return (NBTTagCompound) serializable.serializeNBT(); + } + return new NBTTagCompound(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + public void deserializeNBT(NBTTagCompound nbt) { + if (tank instanceof FluidTank fluidTank) { + fluidTank.readFromNBT(nbt); + } else if (tank instanceof INBTSerializable serializable) { + serializable.deserializeNBT(nbt); + } + } + } +} From cdf18273b859a6e984396fafddb57b5af9aed152 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:15:47 -0700 Subject: [PATCH 02/26] to string --- .../api/capability/impl/FluidTankList2.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index fd1c2d3e604..bc4a53005a2 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -221,6 +221,29 @@ protected IFluidTank computeNext() { }; } + @Override + public String toString() { + return toString(false); + } + + public String toString(boolean lineBreak) { + StringBuilder stb = new StringBuilder("FluidTankList[").append(this.tanks.length).append(";"); + for (int i = 0; i < this.tanks.length; i++) { + if (i != 0) stb.append(','); + stb.append(lineBreak ? "\n " : " "); + + FluidStack fluid = this.tanks[i].getFluid(); + if (fluid == null || fluid.amount == 0) { + stb.append("None 0 / ").append(this.tanks[i].getCapacity()); + } else { + stb.append(fluid.getFluid().getName()).append(' ').append(fluid.amount) + .append(" / ").append(this.tanks[i].getCapacity()); + } + } + if (lineBreak) stb.append('\n'); + return stb.append(']').toString(); + } + private TankWrapper wrap(IFluidTank tank) { return tank instanceof TankWrapper ? (TankWrapper) tank : new TankWrapper(tank, allowSameFluidFill); } @@ -240,7 +263,7 @@ public int getCapacity() { @Override public boolean canFill() { - return allowSameFluidFill; + return true; } @Override @@ -263,7 +286,7 @@ public boolean canDrainFluidType(FluidStack fluidStack) { public static class TankWrapper implements IFluidTank, IFilteredFluidContainer, INBTSerializable { - final IFluidTank tank; + private final IFluidTank tank; private final boolean allowSameFluidFill; private TankWrapper(IFluidTank tank, boolean allowSameFluidFill) { From 94344485cd9b8c6bd44874990b43d69d74ac1f0b Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:34:20 -0700 Subject: [PATCH 03/26] interface --- .../api/capability/IMultipleTankHandler2.java | 176 ++++++++++++++++++ .../api/capability/impl/FluidTankList2.java | 123 ++++-------- 2 files changed, 217 insertions(+), 82 deletions(-) create mode 100644 src/main/java/gregtech/api/capability/IMultipleTankHandler2.java diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java new file mode 100644 index 00000000000..038a4513a36 --- /dev/null +++ b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java @@ -0,0 +1,176 @@ +package gregtech.api.capability; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidTank; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidTank; +import net.minecraftforge.fluids.capability.IFluidHandler; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; + +/** + * Base class for multi-tank fluid handlers. Handles insertion logic, along with other standard + * {@link IFluidHandler} functionalities. + * + * @see gregtech.api.capability.impl.FluidTankList FluidTankList + */ +public interface IMultipleTankHandler2 extends IFluidHandler, Iterable, INBTSerializable { + + /** + * Comparator for entries that can be used in insertion logic + */ + Comparator ENTRY_COMPARATOR = (o1, o2) -> { + // #1: non-empty tank first + boolean empty1 = o1.getFluidAmount() <= 0; + boolean empty2 = o2.getFluidAmount() <= 0; + if (empty1 != empty2) return empty1 ? 1 : -1; + + // #2: filter priority + IFilter filter1 = o1.getFilter(); + IFilter filter2 = o2.getFilter(); + if (filter1 == null) return filter2 == null ? 0 : 1; + if (filter2 == null) return -1; + return IFilter.FILTER_COMPARATOR.compare(filter1, filter2); + }; + + /** + * @return unmodifiable view of {@code MultiFluidTankEntry}s. Note that it's still possible to access + * and modify inner contents of the tanks. + */ + @NotNull + List getFluidTanks(); + + /** + * @return Number of tanks in this tank handler + */ + int getTanks(); + + @NotNull + IMultipleTankHandler2.Entry getTankAt(int index); + + /** + * @return {@code false} if insertion to this fluid handler enforces input to be + * filled in one slot at max. {@code true} if it bypasses the rule. + */ + boolean allowSameFluidFill(); + + /** + * Tries to search tank with contents equal to {@code fluidStack}. If {@code fluidStack} is + * {@code null}, an empty tank is searched instead. + * + * @param fluidStack Fluid stack to search index + * @return Index corresponding to tank at {@link #getFluidTanks()} with matching + */ + default int getIndexOfFluid(@Nullable FluidStack fluidStack) { + List fluidTanks = getFluidTanks(); + for (int i = 0; i < fluidTanks.size(); i++) { + FluidStack tankStack = fluidTanks.get(i).getFluid(); + if (fluidStack == tankStack || tankStack != null && tankStack.isFluidEqual(fluidStack)) { + return i; + } + } + return -1; + } + + @Override + default Iterator iterator() { + return getFluidTanks().iterator(); + } + + /** + * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler2} for accessing + * information such as {@link IMultipleTankHandler2#allowSameFluidFill()}. + */ + interface Entry extends IFluidTank, IFluidHandler, IFilteredFluidContainer, INBTSerializable { + + @NotNull + IMultipleTankHandler2 getParentHandler(); + + @NotNull + IFluidTank getDelegate(); + + default boolean allowSameFluidFill() { + return getParentHandler().allowSameFluidFill(); + } + + @Nullable + @Override + default IFilter getFilter() { + return getDelegate() instanceof IFilteredFluidContainer filtered ? filtered.getFilter() : null; + } + + @Nullable + @Override + default FluidStack getFluid() { + return getDelegate().getFluid(); + } + + @Override + default int getFluidAmount() { + return getDelegate().getFluidAmount(); + } + + @Override + default int getCapacity() { + return getDelegate().getCapacity(); + } + + @Override + default FluidTankInfo getInfo() { + return getDelegate().getInfo(); + } + + @Override + default int fill(FluidStack resource, boolean doFill) { + return getDelegate().fill(resource, doFill); + } + + @Nullable + @Override + default FluidStack drain(FluidStack resource, boolean doDrain) { + if (resource == null || resource.amount <= 0) { + return null; + } + if (getDelegate() instanceof IFluidHandler fluidHandler) { + return fluidHandler.drain(resource, doDrain); + } + // just imitate the logic + FluidStack fluid = getFluid(); + return fluid != null && fluid.isFluidEqual(resource) ? drain(resource.amount, doDrain) : null; + } + + @Nullable + @Override + default FluidStack drain(int maxDrain, boolean doDrain) { + return getDelegate().drain(maxDrain, doDrain); + } + + @SuppressWarnings("rawtypes") + @Override + default NBTTagCompound serializeNBT() { + if (getDelegate() instanceof FluidTank fluidTank) { + return fluidTank.writeToNBT(new NBTTagCompound()); + } else if (getDelegate() instanceof INBTSerializable serializable) { + return (NBTTagCompound) serializable.serializeNBT(); + } + return new NBTTagCompound(); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Override + default void deserializeNBT(NBTTagCompound nbt) { + if (getDelegate() instanceof FluidTank fluidTank) { + fluidTank.readFromNBT(nbt); + } else if (getDelegate() instanceof INBTSerializable serializable) { + serializable.deserializeNBT(nbt); + } + } + } +} diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index bc4a53005a2..71d1a127581 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -3,6 +3,9 @@ import gregtech.api.capability.IFilter; import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.IMultipleTankHandler2; + import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; @@ -20,33 +23,20 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; -public class FluidTankList2 implements IFluidHandler, INBTSerializable, Iterable { - - public static Comparator ENTRY_COMPARATOR = (o1, o2) -> { - // #1: non-empty tank first - boolean empty1 = o1.getFluidAmount() <= 0; - boolean empty2 = o2.getFluidAmount() <= 0; - if (empty1 != empty2) return empty1 ? 1 : -1; - - // #2: filter priority - IFilter filter1 = o1.getFilter(); - IFilter filter2 = o2.getFilter(); - if (filter1 == null) return filter2 == null ? 0 : 1; - if (filter2 == null) return -1; - return IFilter.FILTER_COMPARATOR.compare(filter1, filter2); - }; +public class FluidTankList2 implements IMultipleTankHandler2 { private final boolean allowSameFluidFill; - private TankWrapper[] tanks = new TankWrapper[0]; + private Entry[] tanks = new Entry[0]; private IFluidTankProperties[] properties = new IFluidTankProperties[0]; public FluidTankList2(boolean allowSameFluidFill, IFluidTank... fluidTanks) { if (!ArrayUtils.isEmpty(fluidTanks)) { - tanks = new TankWrapper[fluidTanks.length]; + tanks = new Entry[fluidTanks.length]; properties = new IFluidTankProperties[fluidTanks.length]; Arrays.setAll(tanks, value -> { var tank = wrap(fluidTanks[value]); @@ -97,7 +87,7 @@ public int fill(FluidStack resource, boolean doFill) { } // regardless of whether the insertion succeeded, presence of identical fluid in // a slot prevents distinct fill to other slots - if (!tank.allowSameFluidFill) { + if (!tank.allowSameFluidFill()) { distinctSlotVisited = true; } } @@ -106,7 +96,7 @@ public int fill(FluidStack resource, boolean doFill) { for (var tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - boolean usesDistinctFluidFill = tank.allowSameFluidFill; + boolean usesDistinctFluidFill = tank.allowSameFluidFill(); if ((usesDistinctFluidFill || !distinctSlotVisited) && tank.getFluidAmount() == 0) { int inserted = tank.fill(resource, doFill); if (inserted > 0) { @@ -192,7 +182,7 @@ public FluidStack drain(int maxDrain, boolean doDrain) { public NBTTagCompound serializeNBT() { NBTTagCompound fluidInventory = new NBTTagCompound(); NBTTagList tanks = new NBTTagList(); - for (TankWrapper tank : this.tanks) { + for (Entry tank : this.tanks) { tanks.appendTag(tank.serializeNBT()); } fluidInventory.setTag("Tanks", tanks); @@ -208,17 +198,23 @@ public void deserializeNBT(NBTTagCompound nbt) { } @Override - public @NotNull Iterator iterator() { - return new AbstractIterator<>() { + public @NotNull List getFluidTanks() { + return Collections.unmodifiableList(Arrays.asList(this.tanks)); + } - final int length = tanks.length; - int index = 0; + @Override + public int getTanks() { + return tanks.length; + } - @Override - protected IFluidTank computeNext() { - return index < length ? tanks[index++] : endOfData(); - } - }; + @Override + public @NotNull IMultipleTankHandler2.Entry getTankAt(int index) { + return tanks[index]; + } + + @Override + public boolean allowSameFluidFill() { + return allowSameFluidFill; } @Override @@ -245,10 +241,10 @@ public String toString(boolean lineBreak) { } private TankWrapper wrap(IFluidTank tank) { - return tank instanceof TankWrapper ? (TankWrapper) tank : new TankWrapper(tank, allowSameFluidFill); + return tank instanceof TankWrapper ? (TankWrapper) tank : new TankWrapper(tank, this); } - private IFluidTankProperties createProp(IFluidTank tank) { + protected static IFluidTankProperties createProp(IFluidTank tank) { return new IFluidTankProperties() { @Override @@ -284,70 +280,33 @@ public boolean canDrainFluidType(FluidStack fluidStack) { }; } - public static class TankWrapper implements IFluidTank, IFilteredFluidContainer, INBTSerializable { + public static class TankWrapper implements Entry { private final IFluidTank tank; - private final boolean allowSameFluidFill; + private final IMultipleTankHandler2 parent; + private final IFluidTankProperties[] props; - private TankWrapper(IFluidTank tank, boolean allowSameFluidFill) { - this.allowSameFluidFill = allowSameFluidFill; + private TankWrapper(IFluidTank tank, IMultipleTankHandler2 parent) { this.tank = tank; + this.parent = parent; + this.props = new IFluidTankProperties[] { + createProp(this) + }; } @Override - public FluidStack getFluid() { - return tank.getFluid(); + public @NotNull IMultipleTankHandler2 getParentHandler() { + return parent; } @Override - public int getFluidAmount() { - return tank.getFluidAmount(); + public @NotNull IFluidTank getDelegate() { + return tank; } @Override - public int getCapacity() { - return tank.getCapacity(); - } - - @Override - public FluidTankInfo getInfo() { - return tank.getInfo(); - } - - @Override - public int fill(FluidStack resource, boolean doFill) { - return tank.fill(resource, doFill); - } - - @Override - public FluidStack drain(int maxDrain, boolean doDrain) { - return tank.drain(maxDrain, doDrain); - } - - @Override - public @Nullable IFilter getFilter() { - return tank instanceof IFilteredFluidContainer filter ? filter.getFilter() : null; - } - - @SuppressWarnings("rawtypes") - @Override - public NBTTagCompound serializeNBT() { - if (tank instanceof FluidTank fluidTank) { - return fluidTank.writeToNBT(new NBTTagCompound()); - } else if (tank instanceof INBTSerializable serializable) { - return (NBTTagCompound) serializable.serializeNBT(); - } - return new NBTTagCompound(); - } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - @Override - public void deserializeNBT(NBTTagCompound nbt) { - if (tank instanceof FluidTank fluidTank) { - fluidTank.readFromNBT(nbt); - } else if (tank instanceof INBTSerializable serializable) { - serializable.deserializeNBT(nbt); - } + public IFluidTankProperties[] getTankProperties() { + return this.props; } } } From 23e7244c14a567bf6de219c90d56dbb5d5e00588 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:02:39 -0700 Subject: [PATCH 04/26] make entry implement tank property --- .../api/capability/IMultipleTankHandler2.java | 22 +++++- .../api/capability/impl/FluidTankList2.java | 74 ++++--------------- 2 files changed, 36 insertions(+), 60 deletions(-) diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java index 038a4513a36..d34a2f3ef8f 100644 --- a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java +++ b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java @@ -7,6 +7,7 @@ import net.minecraftforge.fluids.FluidTankInfo; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; +import net.minecraftforge.fluids.capability.IFluidTankProperties; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -21,7 +22,8 @@ * * @see gregtech.api.capability.impl.FluidTankList FluidTankList */ -public interface IMultipleTankHandler2 extends IFluidHandler, Iterable, INBTSerializable { +public interface IMultipleTankHandler2 extends IFluidHandler, Iterable, + INBTSerializable { /** * Comparator for entries that can be used in insertion logic @@ -88,7 +90,8 @@ default Iterator iterator() { * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler2} for accessing * information such as {@link IMultipleTankHandler2#allowSameFluidFill()}. */ - interface Entry extends IFluidTank, IFluidHandler, IFilteredFluidContainer, INBTSerializable { + interface Entry extends IFluidTank, IFluidHandler, IFilteredFluidContainer, INBTSerializable, + IFluidTankProperties { @NotNull IMultipleTankHandler2 getParentHandler(); @@ -127,6 +130,21 @@ default FluidTankInfo getInfo() { return getDelegate().getInfo(); } + @Override + default FluidStack getContents() { + return getFluid() == null ? null : getFluid().copy(); + } + + @Override + default boolean canFill() { + return true; + } + + @Override + default boolean canDrain() { + return true; + } + @Override default int fill(FluidStack resource, boolean doFill) { return getDelegate().fill(resource, doFill); diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 71d1a127581..003cdb4101e 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -1,48 +1,31 @@ package gregtech.api.capability.impl; -import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidContainer; - -import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.IMultipleTankHandler2; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; -import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.FluidTank; -import net.minecraftforge.fluids.FluidTankInfo; import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidTankProperties; -import com.google.common.collect.AbstractIterator; import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; import java.util.List; public class FluidTankList2 implements IMultipleTankHandler2 { private final boolean allowSameFluidFill; private Entry[] tanks = new Entry[0]; - private IFluidTankProperties[] properties = new IFluidTankProperties[0]; public FluidTankList2(boolean allowSameFluidFill, IFluidTank... fluidTanks) { if (!ArrayUtils.isEmpty(fluidTanks)) { tanks = new Entry[fluidTanks.length]; - properties = new IFluidTankProperties[fluidTanks.length]; - Arrays.setAll(tanks, value -> { - var tank = wrap(fluidTanks[value]); - properties[value] = createProp(tank); - return tank; - }); + Arrays.setAll(tanks, value -> wrap(fluidTanks[value])); } this.allowSameFluidFill = allowSameFluidFill; } @@ -53,7 +36,7 @@ public FluidTankList2(boolean allowSameFluidFill, @NotNull List Date: Mon, 23 Dec 2024 23:43:11 -0700 Subject: [PATCH 05/26] rework transfer logic entry is not a handler --- .../api/capability/IMultipleTankHandler2.java | 30 +++---- .../api/capability/impl/FluidTankList2.java | 82 +++++++++---------- .../gregtech/api/util/GTTransferUtils.java | 29 +++++++ 3 files changed, 81 insertions(+), 60 deletions(-) diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java index d34a2f3ef8f..a2660a78790 100644 --- a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java +++ b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java @@ -82,7 +82,7 @@ default int getIndexOfFluid(@Nullable FluidStack fluidStack) { } @Override - default Iterator iterator() { + default @NotNull Iterator iterator() { return getFluidTanks().iterator(); } @@ -90,7 +90,7 @@ default Iterator iterator() { * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler2} for accessing * information such as {@link IMultipleTankHandler2#allowSameFluidFill()}. */ - interface Entry extends IFluidTank, IFluidHandler, IFilteredFluidContainer, INBTSerializable, + interface Entry extends IFluidTank, IFilteredFluidContainer, INBTSerializable, IFluidTankProperties { @NotNull @@ -150,19 +150,19 @@ default int fill(FluidStack resource, boolean doFill) { return getDelegate().fill(resource, doFill); } - @Nullable - @Override - default FluidStack drain(FluidStack resource, boolean doDrain) { - if (resource == null || resource.amount <= 0) { - return null; - } - if (getDelegate() instanceof IFluidHandler fluidHandler) { - return fluidHandler.drain(resource, doDrain); - } - // just imitate the logic - FluidStack fluid = getFluid(); - return fluid != null && fluid.isFluidEqual(resource) ? drain(resource.amount, doDrain) : null; - } +// @Nullable +// @Override +// default FluidStack drain(FluidStack resource, boolean doDrain) { +// if (resource == null || resource.amount <= 0) { +// return null; +// } +// if (getDelegate() instanceof IFluidHandler fluidHandler) { +// return fluidHandler.drain(resource, doDrain); +// } +// // just imitate the logic +// FluidStack fluid = getFluid(); +// return fluid != null && fluid.isFluidEqual(resource) ? drain(resource.amount, doDrain) : null; +// } @Nullable @Override diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 003cdb4101e..fd166ec7205 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -41,61 +41,53 @@ public IFluidTankProperties[] getTankProperties() { @Override public int fill(FluidStack resource, boolean doFill) { - if (resource == null || resource.amount <= 0) { + if (resource == null || resource.amount <= 0) return 0; - } + int totalInserted = 0; boolean inputFluidCopied = false; - // flag value indicating whether the fluid was stored in 'distinct' slot at least once - boolean distinctSlotVisited = false; var fluidTanks = this.tanks.clone(); Arrays.sort(fluidTanks, ENTRY_COMPARATOR); // search for tanks with same fluid type first for (var tank : fluidTanks) { + if (!resource.isFluidEqual(tank.getFluid())) + continue; + // if the fluid to insert matches the tank, insert the fluid - if (resource.isFluidEqual(tank.getFluid())) { - int inserted = tank.fill(resource, doFill); - if (inserted > 0) { - totalInserted += inserted; - if (resource.amount - inserted <= 0) { - return totalInserted; - } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; - } - // regardless of whether the insertion succeeded, presence of identical fluid in - // a slot prevents distinct fill to other slots - if (!tank.allowSameFluidFill()) { - distinctSlotVisited = true; - } + int inserted = tank.fill(resource, doFill); + if (inserted <= 0) continue; + + totalInserted += inserted; + if (resource.amount - inserted <= 0) { + return totalInserted; } + if (!inputFluidCopied) { + inputFluidCopied = true; + resource = resource.copy(); + } + resource.amount -= inserted; } + // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid - for (var tank : fluidTanks) { + for (Entry tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - boolean usesDistinctFluidFill = tank.allowSameFluidFill(); - if ((usesDistinctFluidFill || !distinctSlotVisited) && tank.getFluidAmount() == 0) { - int inserted = tank.fill(resource, doFill); - if (inserted > 0) { - totalInserted += inserted; - if (resource.amount - inserted <= 0) { - return totalInserted; - } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; - if (!usesDistinctFluidFill) { - distinctSlotVisited = true; - } + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) { + continue; + } + int inserted = tank.fill(resource, doFill); + if (inserted > 0) { + totalInserted += inserted; + if (resource.amount - inserted <= 0) { + return totalInserted; + } + if (!inputFluidCopied) { + inputFluidCopied = true; + resource = resource.copy(); } + resource.amount -= inserted; } } // return the amount of fluid that was inserted @@ -231,12 +223,12 @@ public static class TankWrapper implements Entry { private final IFluidTank tank; private final IMultipleTankHandler2 parent; - private final IFluidTankProperties[] props; +// private final IFluidTankProperties[] props; private TankWrapper(IFluidTank tank, IMultipleTankHandler2 parent) { this.tank = tank; this.parent = parent; - this.props = new IFluidTankProperties[] { this }; +// this.props = new IFluidTankProperties[] { this }; } @Override @@ -249,10 +241,10 @@ private TankWrapper(IFluidTank tank, IMultipleTankHandler2 parent) { return tank; } - @Override - public IFluidTankProperties[] getTankProperties() { - return this.props; - } +// @Override +// public IFluidTankProperties[] getTankProperties() { +// return this.props; +// } @Override public boolean canFillFluidType(FluidStack fluidStack) { diff --git a/src/main/java/gregtech/api/util/GTTransferUtils.java b/src/main/java/gregtech/api/util/GTTransferUtils.java index d79024f844b..f07facfeecb 100644 --- a/src/main/java/gregtech/api/util/GTTransferUtils.java +++ b/src/main/java/gregtech/api/util/GTTransferUtils.java @@ -168,6 +168,35 @@ public static boolean addFluidsToFluidHandler(IMultipleTankHandler fluidHandler, return true; } + /** + * Simulates the insertion of fluid into a target fluid handler, then optionally performs the insertion.

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 fluidStacks the items to insert into {@code fluidHandler}. + * @param fluidHandler the target inventory + * @param simulate whether to simulate ({@code true}) or actually perform the insertion ({@code false}) + * @return {@code true} if the insertion succeeded, {@code false} otherwise. + */ + public static boolean addFluidsToFluidHandler(List<@NotNull FluidStack> fluidStacks, + IFluidHandler fluidHandler, + boolean simulate) { + if (simulate) { + for (FluidStack stack : fluidStacks) { + int filled = fluidHandler.fill(stack, false); + if (filled < stack.amount) + return false; + } + return true; + } + + for (FluidStack fluidStack : fluidStacks) { + fluidHandler.fill(fluidStack, true); + } + return true; + } + /** * Inserts items by trying to fill slots with the same item first, and then fill empty slots. */ From c1e568a0d57eaffd5cdb748b93c952a411891495 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Mon, 23 Dec 2024 23:48:52 -0700 Subject: [PATCH 06/26] a bit more work --- .../api/capability/impl/FluidTankList2.java | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index fd166ec7205..647326fc548 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -44,50 +44,43 @@ public int fill(FluidStack resource, boolean doFill) { if (resource == null || resource.amount <= 0) return 0; + FluidStack copy = resource.copy(); + int totalInserted = 0; - boolean inputFluidCopied = false; var fluidTanks = this.tanks.clone(); Arrays.sort(fluidTanks, ENTRY_COMPARATOR); // search for tanks with same fluid type first - for (var tank : fluidTanks) { - if (!resource.isFluidEqual(tank.getFluid())) + for (Entry tank : fluidTanks) { + if (!copy.isFluidEqual(tank.getFluid())) continue; // if the fluid to insert matches the tank, insert the fluid - int inserted = tank.fill(resource, doFill); + int inserted = tank.fill(copy, doFill); if (inserted <= 0) continue; totalInserted += inserted; - if (resource.amount - inserted <= 0) { + copy.amount -= inserted; + if (copy.amount <= 0) { return totalInserted; } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; } // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid for (Entry tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) { + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(copy)) continue; - } - int inserted = tank.fill(resource, doFill); - if (inserted > 0) { - totalInserted += inserted; - if (resource.amount - inserted <= 0) { - return totalInserted; - } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; + + int inserted = tank.fill(copy, doFill); + if (inserted <= 0) continue; + + totalInserted += inserted; + copy.amount -= inserted; + if (copy.amount <= 0) { + return totalInserted; } } // return the amount of fluid that was inserted From 1eb26762ee8c72fbf76efa3cae0ac8301c4c26d6 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Tue, 24 Dec 2024 14:35:44 -0700 Subject: [PATCH 07/26] switch to inner class --- .../api/capability/IMultipleTankHandler2.java | 77 +++++++++++-------- .../api/capability/impl/FluidTankList2.java | 55 ++----------- 2 files changed, 50 insertions(+), 82 deletions(-) diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java index a2660a78790..c080ff3cffd 100644 --- a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java +++ b/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java @@ -86,93 +86,106 @@ default int getIndexOfFluid(@Nullable FluidStack fluidStack) { return getFluidTanks().iterator(); } + default Entry wrap(IFluidTank tank) { + return tank instanceof Entry ? (Entry) tank : new Entry(tank, this); + } + /** * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler2} for accessing * information such as {@link IMultipleTankHandler2#allowSameFluidFill()}. */ - interface Entry extends IFluidTank, IFilteredFluidContainer, INBTSerializable, + class Entry implements IFluidTank, IFilteredFluidContainer, INBTSerializable, IFluidTankProperties { - @NotNull - IMultipleTankHandler2 getParentHandler(); + private final IFluidTank tank; + private final IMultipleTankHandler2 parent; + + private Entry(IFluidTank tank, IMultipleTankHandler2 parent) { + this.tank = tank; + this.parent = parent; + } + + public @NotNull IMultipleTankHandler2 getParentHandler() { + return parent; + } - @NotNull - IFluidTank getDelegate(); + public @NotNull IFluidTank getDelegate() { + return tank; + } - default boolean allowSameFluidFill() { + public boolean allowSameFluidFill() { return getParentHandler().allowSameFluidFill(); } @Nullable @Override - default IFilter getFilter() { + public IFilter getFilter() { return getDelegate() instanceof IFilteredFluidContainer filtered ? filtered.getFilter() : null; } @Nullable @Override - default FluidStack getFluid() { + public FluidStack getFluid() { return getDelegate().getFluid(); } @Override - default int getFluidAmount() { + public int getFluidAmount() { return getDelegate().getFluidAmount(); } @Override - default int getCapacity() { + public int getCapacity() { return getDelegate().getCapacity(); } @Override - default FluidTankInfo getInfo() { + public FluidTankInfo getInfo() { return getDelegate().getInfo(); } @Override - default FluidStack getContents() { + public FluidStack getContents() { return getFluid() == null ? null : getFluid().copy(); } @Override - default boolean canFill() { + public boolean canFill() { return true; } @Override - default boolean canDrain() { + public boolean canDrain() { return true; } @Override - default int fill(FluidStack resource, boolean doFill) { - return getDelegate().fill(resource, doFill); + public boolean canFillFluidType(FluidStack fluidStack) { + if (allowSameFluidFill() || fluidStack == null) return true; + int i = parent.getIndexOfFluid(fluidStack); + var tank = parent.getTankAt(i); + return tank.getFluidAmount() < tank.getCapacity(); } -// @Nullable -// @Override -// default FluidStack drain(FluidStack resource, boolean doDrain) { -// if (resource == null || resource.amount <= 0) { -// return null; -// } -// if (getDelegate() instanceof IFluidHandler fluidHandler) { -// return fluidHandler.drain(resource, doDrain); -// } -// // just imitate the logic -// FluidStack fluid = getFluid(); -// return fluid != null && fluid.isFluidEqual(resource) ? drain(resource.amount, doDrain) : null; -// } + @Override + public boolean canDrainFluidType(FluidStack fluidStack) { + return true; + } + + @Override + public int fill(FluidStack resource, boolean doFill) { + return getDelegate().fill(resource, doFill); + } @Nullable @Override - default FluidStack drain(int maxDrain, boolean doDrain) { + public FluidStack drain(int maxDrain, boolean doDrain) { return getDelegate().drain(maxDrain, doDrain); } @SuppressWarnings("rawtypes") @Override - default NBTTagCompound serializeNBT() { + public NBTTagCompound serializeNBT() { if (getDelegate() instanceof FluidTank fluidTank) { return fluidTank.writeToNBT(new NBTTagCompound()); } else if (getDelegate() instanceof INBTSerializable serializable) { @@ -183,7 +196,7 @@ default NBTTagCompound serializeNBT() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override - default void deserializeNBT(NBTTagCompound nbt) { + public void deserializeNBT(NBTTagCompound nbt) { if (getDelegate() instanceof FluidTank fluidTank) { fluidTank.readFromNBT(nbt); } else if (getDelegate() instanceof INBTSerializable serializable) { diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 647326fc548..b8f522cefc8 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -34,11 +34,6 @@ public FluidTankList2(boolean allowSameFluidFill, @NotNull List getFluidTanks() { return Collections.unmodifiableList(Arrays.asList(this.tanks)); @@ -207,49 +207,4 @@ public String toString(boolean lineBreak) { if (lineBreak) stb.append('\n'); return stb.append(']').toString(); } - - private TankWrapper wrap(IFluidTank tank) { - return tank instanceof TankWrapper ? (TankWrapper) tank : new TankWrapper(tank, this); - } - - public static class TankWrapper implements Entry { - - private final IFluidTank tank; - private final IMultipleTankHandler2 parent; -// private final IFluidTankProperties[] props; - - private TankWrapper(IFluidTank tank, IMultipleTankHandler2 parent) { - this.tank = tank; - this.parent = parent; -// this.props = new IFluidTankProperties[] { this }; - } - - @Override - public @NotNull IMultipleTankHandler2 getParentHandler() { - return parent; - } - - @Override - public @NotNull IFluidTank getDelegate() { - return tank; - } - -// @Override -// public IFluidTankProperties[] getTankProperties() { -// return this.props; -// } - - @Override - public boolean canFillFluidType(FluidStack fluidStack) { - if (allowSameFluidFill() || fluidStack == null) return true; - int i = parent.getIndexOfFluid(fluidStack); - var tank = parent.getTankAt(i); - return tank.getFluidAmount() < tank.getCapacity(); - } - - @Override - public boolean canDrainFluidType(FluidStack fluidStack) { - return true; - } - } } From 03c99b1b94e8c5a227cfd25590273ef29ba31213 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Tue, 24 Dec 2024 14:45:15 -0700 Subject: [PATCH 08/26] add new constructor --- .../api/capability/impl/FluidTankList2.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index b8f522cefc8..33ff5854c08 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -1,5 +1,6 @@ package gregtech.api.capability.impl; +import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.IMultipleTankHandler2; import net.minecraft.nbt.NBTTagCompound; @@ -13,6 +14,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -34,6 +36,24 @@ public FluidTankList2(boolean allowSameFluidFill, @NotNull List { + if (value < tanks) return parent.getTankAt(value); + else return wrap(additionalTanks[value - tanks]); + }); + + this.allowSameFluidFill = allowSameFluidFill; + } + @Override public int fill(FluidStack resource, boolean doFill) { if (resource == null || resource.amount <= 0) From 0a4d2f0762ff971aa0d5fd2ba350c6b8206d544e Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Wed, 25 Dec 2024 19:20:38 -0700 Subject: [PATCH 09/26] make abstract class instead of interface rename misc --- ...Handler2.java => MultipleTankHandler.java} | 34 ++++++------- .../api/capability/impl/FluidTankList2.java | 50 +++++++++---------- .../gregtech/api/util/GTTransferUtils.java | 6 ++- 3 files changed, 44 insertions(+), 46 deletions(-) rename src/main/java/gregtech/api/capability/{IMultipleTankHandler2.java => MultipleTankHandler.java} (84%) diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java similarity index 84% rename from src/main/java/gregtech/api/capability/IMultipleTankHandler2.java rename to src/main/java/gregtech/api/capability/MultipleTankHandler.java index c080ff3cffd..1d9015286d5 100644 --- a/src/main/java/gregtech/api/capability/IMultipleTankHandler2.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -22,13 +22,13 @@ * * @see gregtech.api.capability.impl.FluidTankList FluidTankList */ -public interface IMultipleTankHandler2 extends IFluidHandler, Iterable, - INBTSerializable { +public abstract class MultipleTankHandler implements IFluidHandler, Iterable, + INBTSerializable { /** * Comparator for entries that can be used in insertion logic */ - Comparator ENTRY_COMPARATOR = (o1, o2) -> { + public static final Comparator ENTRY_COMPARATOR = (o1, o2) -> { // #1: non-empty tank first boolean empty1 = o1.getFluidAmount() <= 0; boolean empty2 = o2.getFluidAmount() <= 0; @@ -47,21 +47,21 @@ public interface IMultipleTankHandler2 extends IFluidHandler, Iterable getFluidTanks(); + public abstract List getFluidTanks(); /** * @return Number of tanks in this tank handler */ - int getTanks(); + public abstract int size(); @NotNull - IMultipleTankHandler2.Entry getTankAt(int index); + public abstract Entry getTankAt(int index); /** * @return {@code false} if insertion to this fluid handler enforces input to be * filled in one slot at max. {@code true} if it bypasses the rule. */ - boolean allowSameFluidFill(); + public abstract boolean allowSameFluidFill(); /** * Tries to search tank with contents equal to {@code fluidStack}. If {@code fluidStack} is @@ -70,7 +70,7 @@ public interface IMultipleTankHandler2 extends IFluidHandler, Iterable fluidTanks = getFluidTanks(); for (int i = 0; i < fluidTanks.size(); i++) { FluidStack tankStack = fluidTanks.get(i).getFluid(); @@ -82,30 +82,30 @@ default int getIndexOfFluid(@Nullable FluidStack fluidStack) { } @Override - default @NotNull Iterator iterator() { + public final @NotNull Iterator iterator() { return getFluidTanks().iterator(); } - default Entry wrap(IFluidTank tank) { + protected final Entry wrap(IFluidTank tank) { return tank instanceof Entry ? (Entry) tank : new Entry(tank, this); } /** - * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler2} for accessing - * information such as {@link IMultipleTankHandler2#allowSameFluidFill()}. + * Entry of multi fluid tanks. Retains reference to original {@link MultipleTankHandler} for accessing + * information such as {@link MultipleTankHandler#allowSameFluidFill()}. */ - class Entry implements IFluidTank, IFilteredFluidContainer, INBTSerializable, - IFluidTankProperties { + public static class Entry implements IFluidTank, IFilteredFluidContainer, INBTSerializable, + IFluidTankProperties { private final IFluidTank tank; - private final IMultipleTankHandler2 parent; + private final MultipleTankHandler parent; - private Entry(IFluidTank tank, IMultipleTankHandler2 parent) { + private Entry(IFluidTank tank, MultipleTankHandler parent) { this.tank = tank; this.parent = parent; } - public @NotNull IMultipleTankHandler2 getParentHandler() { + public @NotNull MultipleTankHandler getParentHandler() { return parent; } diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 33ff5854c08..161894a567e 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -1,7 +1,6 @@ package gregtech.api.capability.impl; -import gregtech.api.capability.IMultipleTankHandler; -import gregtech.api.capability.IMultipleTankHandler2; +import gregtech.api.capability.MultipleTankHandler; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -14,12 +13,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -public class FluidTankList2 implements IMultipleTankHandler2 { +public final class FluidTankList2 extends MultipleTankHandler { private final boolean allowSameFluidFill; private Entry[] tanks = new Entry[0]; @@ -36,9 +34,9 @@ public FluidTankList2(boolean allowSameFluidFill, @NotNull List
Simulating will not modify any of the input parameters. Insertion will either succeed completely, or fail + * Simulates the insertion of fluid into a target fluid handler, then optionally performs the insertion.
+ *
+ * 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}. * From 6aa37dad857b1476550454632d7863228bc3f6ca Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Wed, 25 Dec 2024 19:27:07 -0700 Subject: [PATCH 10/26] new method --- .../java/gregtech/api/capability/MultipleTankHandler.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 1d9015286d5..7ee7b46d3b4 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -1,5 +1,7 @@ package gregtech.api.capability; +import gregtech.api.capability.impl.FluidTankList2; + import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; @@ -9,6 +11,7 @@ import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidTankProperties; +import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -90,6 +93,11 @@ protected final Entry wrap(IFluidTank tank) { return tank instanceof Entry ? (Entry) tank : new Entry(tank, this); } + public MultipleTankHandler addTanks(IFluidTank... tanks) { + if (ArrayUtils.isEmpty(tanks)) return this; + return new FluidTankList2(allowSameFluidFill(), this, tanks); + } + /** * Entry of multi fluid tanks. Retains reference to original {@link MultipleTankHandler} for accessing * information such as {@link MultipleTankHandler#allowSameFluidFill()}. From f9bdb6173419f73a3e38a38aa5726654f7f2e8f0 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:33:21 -0700 Subject: [PATCH 11/26] test new impl --- .../api/capability/MultipleTankHandler.java | 9 +++- .../api/capability/impl/FluidTankList2.java | 21 +++++---- .../capability/impl/FluidTankListTest.java | 47 +++++-------------- 3 files changed, 31 insertions(+), 46 deletions(-) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 7ee7b46d3b4..d407f149b21 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -2,6 +2,9 @@ import gregtech.api.capability.impl.FluidTankList2; +import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; + import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; @@ -16,6 +19,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -170,9 +174,12 @@ public boolean canDrain() { @Override public boolean canFillFluidType(FluidStack fluidStack) { if (allowSameFluidFill() || fluidStack == null) return true; + // this doesn't work with simulated fills + // that info needs to be stored somewhere in the parent somehow int i = parent.getIndexOfFluid(fluidStack); + if (i == -1) return true; var tank = parent.getTankAt(i); - return tank.getFluidAmount() < tank.getCapacity(); + return tank.getFluidAmount() + fluidStack.amount <= tank.getCapacity(); } @Override diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 161894a567e..1f838fe8924 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -2,6 +2,9 @@ import gregtech.api.capability.MultipleTankHandler; +import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.IntSets; + import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; @@ -61,12 +64,13 @@ public int fill(FluidStack resource, boolean doFill) { int totalInserted = 0; - var fluidTanks = this.tanks.clone(); + Entry[] fluidTanks = this.tanks.clone(); Arrays.sort(fluidTanks, ENTRY_COMPARATOR); // search for tanks with same fluid type first - for (Entry tank : fluidTanks) { - if (!copy.isFluidEqual(tank.getFluid())) + for (int i = 0; i < fluidTanks.length; i++) { + Entry tank = fluidTanks[i]; + if (tank.getFluidAmount() == 0 || !copy.isFluidEqual(tank.getFluid())) continue; // if the fluid to insert matches the tank, insert the fluid @@ -75,16 +79,14 @@ public int fill(FluidStack resource, boolean doFill) { totalInserted += inserted; copy.amount -= inserted; - if (copy.amount <= 0) { - return totalInserted; - } + if (copy.amount <= 0) return totalInserted; } // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid for (Entry tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(copy)) + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) continue; int inserted = tank.fill(copy, doFill); @@ -92,10 +94,9 @@ public int fill(FluidStack resource, boolean doFill) { totalInserted += inserted; copy.amount -= inserted; - if (copy.amount <= 0) { - return totalInserted; - } + if (copy.amount <= 0) return totalInserted; } + // return the amount of fluid that was inserted return totalInserted; } diff --git a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java index a734933b9fe..55e23619f54 100644 --- a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java +++ b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java @@ -2,6 +2,7 @@ import gregtech.Bootstrap; import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.unification.material.Materials; import gregtech.api.util.OverlayedFluidHandler; @@ -153,8 +154,8 @@ public void testMultipleFluidFills() { @Test public void testMixedSameFluidFill() { - new FluidHandlerTester(new FluidTankList(true, - new FluidTankList(false, + new FluidHandlerTester(new FluidTankList2(true, + new FluidTankList2(false, new FluidTank(1000), new FluidTank(1000)), new FluidTank(1000), @@ -169,8 +170,8 @@ public void testMixedSameFluidFill() { new FluidStack(WATER, 1000), new FluidStack(WATER, 400)); - new FluidHandlerTester(new FluidTankList(false, - new FluidTankList(true, + new FluidHandlerTester(new FluidTankList2(false, + new FluidTankList2(true, new FluidTank(1000), new FluidTank(1000)), new FluidTank(1000), @@ -312,17 +313,14 @@ public void testFilterOrdering() { private static final class FluidHandlerTester { - private final FluidTankList tank; + private final FluidTankList2 tank; - @Nullable - private OverlayedFluidHandler overlayedFluidHandler; - - FluidHandlerTester(FluidTankList tank) { + FluidHandlerTester(FluidTankList2 tank) { this.tank = tank; } FluidHandlerTester(boolean allowSameFluidFill, IFluidTank... tanks) { - this(new FluidTankList(allowSameFluidFill, tanks)); + this(new FluidTankList2(allowSameFluidFill, tanks)); } FluidHandlerTester fill(Fluid fluid, int amount) { @@ -334,19 +332,8 @@ FluidHandlerTester fill(FluidStack fluidStack) { String tankString = this.tank.toString(true); int tankFillSim = this.tank.fill(fluidStack, false); - - if (this.overlayedFluidHandler != null) { - String overlayString = this.overlayedFluidHandler.toString(true); - int ofhSim = this.overlayedFluidHandler.insertFluid(fluidStack, fluidStack.amount); - - if (tankFillSim != ofhSim) { - throw new AssertionError("Result of simulation fill from tank and OFH differ.\n" + - "Tank Simulation: " + tankFillSim + ", OFH simulation: " + ofhSim + "\n" + - "Tank: " + tankString + "\n" + - "OFH: " + overlayString); - } - } int actualFill = this.tank.fill(fluidStack, true); + if (tankFillSim != actualFill) { throw new AssertionError("Simulation fill to tank and actual fill differ.\n" + "Simulated Fill: " + tankFillSim + ", Actual Fill: " + actualFill + "\n" + @@ -360,9 +347,6 @@ FluidHandlerTester drain(Fluid fluid, int amount) { } FluidHandlerTester drain(FluidStack fluidStack) { - if (this.overlayedFluidHandler != null) { - throw new IllegalStateException("Cannot drain stuff in simulation"); - } // make string representation before modifying the state, to produce better error message String tankString = this.tank.toString(true); @@ -378,9 +362,6 @@ FluidHandlerTester drain(FluidStack fluidStack) { } FluidHandlerTester drain(int amount) { - if (this.overlayedFluidHandler != null) { - throw new IllegalStateException("Cannot drain stuff in simulation"); - } // make string representation before modifying the state, to produce better error message String tankString = this.tank.toString(true); @@ -396,20 +377,16 @@ FluidHandlerTester drain(int amount) { } FluidHandlerTester beginSimulation() { - if (this.overlayedFluidHandler != null) { - throw new IllegalStateException("Simulation already begun"); - } - this.overlayedFluidHandler = new OverlayedFluidHandler(this.tank); return this; } FluidHandlerTester expectContents(@NotNull FluidStack... optionalFluidStacks) { - if (optionalFluidStacks.length != this.tank.getTanks()) { + if (optionalFluidStacks.length != this.tank.size()) { throw new IllegalArgumentException("Wrong number of fluids to compare; " + - "expected: " + this.tank.getTanks() + ", provided: " + optionalFluidStacks.length); + "expected: " + this.tank.size() + ", provided: " + optionalFluidStacks.length); } for (int i = 0; i < optionalFluidStacks.length; i++) { - IMultipleTankHandler.MultiFluidTankEntry tank = this.tank.getTankAt(i); + var tank = this.tank.getTankAt(i); if (!eq(tank.getFluid(), optionalFluidStacks[i])) { throw new AssertionError("Contents of the tank don't match expected state.\n" + "Expected: [\n " + Arrays.stream(optionalFluidStacks) From 80e5f8188db3ae79bd1d013472dbc9cbb01421d5 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 20:59:14 -0700 Subject: [PATCH 12/26] fix overflow + spotless --- .../api/capability/MultipleTankHandler.java | 4 ---- .../api/capability/impl/FluidTankList2.java | 15 +++++++-------- .../api/capability/impl/FluidTankListTest.java | 3 --- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index d407f149b21..94be9cc0836 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -2,9 +2,6 @@ import gregtech.api.capability.impl.FluidTankList2; -import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; -import it.unimi.dsi.fastutil.ints.Int2ObjectMap; - import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; @@ -19,7 +16,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Comparator; -import java.util.HashMap; import java.util.Iterator; import java.util.List; diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java index 1f838fe8924..072a1dd8017 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java @@ -2,9 +2,6 @@ import gregtech.api.capability.MultipleTankHandler; -import it.unimi.dsi.fastutil.ints.IntSet; -import it.unimi.dsi.fastutil.ints.IntSets; - import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; @@ -68,9 +65,8 @@ public int fill(FluidStack resource, boolean doFill) { Arrays.sort(fluidTanks, ENTRY_COMPARATOR); // search for tanks with same fluid type first - for (int i = 0; i < fluidTanks.length; i++) { - Entry tank = fluidTanks[i]; - if (tank.getFluidAmount() == 0 || !copy.isFluidEqual(tank.getFluid())) + for (Entry tank : fluidTanks) { + if (tank.getFluidAmount() == 0 || !resource.isFluidEqual(tank.getFluid())) continue; // if the fluid to insert matches the tank, insert the fluid @@ -82,12 +78,14 @@ public int fill(FluidStack resource, boolean doFill) { if (copy.amount <= 0) return totalInserted; } + boolean overflow = false; + // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid for (Entry tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) - continue; + if (overflow && !tank.allowSameFluidFill()) continue; + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) continue; int inserted = tank.fill(copy, doFill); if (inserted <= 0) continue; @@ -95,6 +93,7 @@ public int fill(FluidStack resource, boolean doFill) { totalInserted += inserted; copy.amount -= inserted; if (copy.amount <= 0) return totalInserted; + else overflow = true; } // return the amount of fluid that was inserted diff --git a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java index 55e23619f54..7fb6219bfa3 100644 --- a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java +++ b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java @@ -1,10 +1,7 @@ package gregtech.api.capability.impl; import gregtech.Bootstrap; -import gregtech.api.capability.IMultipleTankHandler; -import gregtech.api.capability.MultipleTankHandler; import gregtech.api.unification.material.Materials; -import gregtech.api.util.OverlayedFluidHandler; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; From de0fa3ae3df0f99606ff8d62ee365bab8c6fcae3 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:41:24 -0700 Subject: [PATCH 13/26] fix filling distinct tanks --- .../api/capability/MultipleTankHandler.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 94be9cc0836..25e19bb53f1 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -7,6 +7,7 @@ import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTank; import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.FluidUtil; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidTankProperties; @@ -170,12 +171,14 @@ public boolean canDrain() { @Override public boolean canFillFluidType(FluidStack fluidStack) { if (allowSameFluidFill() || fluidStack == null) return true; - // this doesn't work with simulated fills - // that info needs to be stored somewhere in the parent somehow - int i = parent.getIndexOfFluid(fluidStack); - if (i == -1) return true; - var tank = parent.getTankAt(i); - return tank.getFluidAmount() + fluidStack.amount <= tank.getCapacity(); + for (Entry tank : getParentHandler()) { + // only consider tanks that do not allow same fluid fill + if (tank.allowSameFluidFill() || this == tank) continue; + if (fluidStack.isFluidEqual(tank.getFluid())) { + return tank.getFluidAmount() + fluidStack.amount <= tank.getCapacity(); + } + } + return true; } @Override From c340b15f1763d86ddf55d565217fa08db3bdfdce Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:44:05 -0700 Subject: [PATCH 14/26] update parallel logic test --- .../java/gregtech/api/recipes/logic/ParallelLogicTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java b/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java index 0b1de7c397a..4c704c29ca6 100644 --- a/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java +++ b/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java @@ -3,7 +3,9 @@ import gregtech.Bootstrap; import gregtech.api.GTValues; import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; +import gregtech.api.capability.impl.FluidTankList2; import gregtech.api.recipes.Recipe; import gregtech.api.recipes.RecipeBuilder; import gregtech.api.recipes.RecipeMap; @@ -726,7 +728,7 @@ public void getMaxRatioFluid_DifferentNonConsumedTest() { importFluidBus.getImportFluids().fill(Materials.Water.getFluid(1000), true); secondImportFluidBus.getImportFluids().fill(Materials.Acetone.getFluid(1), true); - IMultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), + MultipleTankHandler tankHandler = new FluidTankList2(false, importFluidBus.getImportFluids().getTankAt(0), secondImportFluidBus.getImportFluids().getTankAt(0)); int fluidRatio = ParallelLogic.getMaxRatioFluid(GTHashMaps.fromFluidHandler(tankHandler), From 8e0605239b2df171071bc7e7c64152a1b7e2a422 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:04:20 -0700 Subject: [PATCH 15/26] we do a little replace all --- .../gregtech/api/capability/IDualHandler.java | 2 +- .../api/capability/IMultipleTankHandler.java | 248 ------------------ .../api/capability/MultipleTankHandler.java | 9 +- .../capability/impl/AbstractRecipeLogic.java | 22 +- .../capability/impl/BoilerRecipeLogic.java | 4 +- .../impl/DistillationTowerLogicHandler.java | 8 +- .../api/capability/impl/FluidTankList.java | 228 ++++++++-------- .../api/capability/impl/FluidTankList2.java | 226 ---------------- .../impl/MultiblockRecipeLogic.java | 8 +- .../impl/SteamMultiblockRecipeLogic.java | 10 +- .../SimpleMachineMetaTileEntity.java | 6 +- .../multiblock/FuelMultiblockController.java | 6 +- .../RecipeMapMultiblockController.java | 10 +- .../RecipeMapSteamMultiblockController.java | 6 +- .../java/gregtech/api/recipes/Recipe.java | 4 +- .../java/gregtech/api/recipes/RecipeMap.java | 4 +- .../logic/IParallelableRecipeLogic.java | 10 +- .../api/recipes/logic/ParallelLogic.java | 24 +- .../gregtech/api/recipes/ui/RecipeMapUI.java | 8 +- .../recipes/ui/impl/DistillationTowerUI.java | 6 +- .../gregtech/api/util/GTTransferUtils.java | 8 +- .../java/gregtech/api/util/GTUtility.java | 6 +- .../api/util/OverlayedFluidHandler.java | 21 +- .../common/covers/CoverDigitalInterface.java | 2 +- .../items/behaviors/TricorderBehavior.java | 2 +- .../MetaTileEntityDistillationTower.java | 6 +- .../electric/MetaTileEntityFluidDrill.java | 6 +- .../electric/MetaTileEntityLargeMiner.java | 2 +- .../MetaTileEntityProcessingArray.java | 4 +- .../LargeTurbineWorkableHandler.java | 4 +- .../MetaTileEntityLargeCombustionEngine.java | 8 +- ...etaTileEntityMultiblockNotifiablePart.java | 4 +- .../storage/MetaTileEntityBuffer.java | 2 +- ...etaTileEntityQuantumStorageController.java | 2 +- .../DriverRecipeMapMultiblockController.java | 4 +- .../capability/impl/FluidTankListTest.java | 14 +- .../impl/MultiblockRecipeLogicTest.java | 14 +- .../logic/IParallelableRecipeLogicTest.java | 6 +- .../api/recipes/logic/ParallelLogicTest.java | 12 +- 39 files changed, 239 insertions(+), 737 deletions(-) delete mode 100644 src/main/java/gregtech/api/capability/IMultipleTankHandler.java delete mode 100644 src/main/java/gregtech/api/capability/impl/FluidTankList2.java diff --git a/src/main/java/gregtech/api/capability/IDualHandler.java b/src/main/java/gregtech/api/capability/IDualHandler.java index 595efc6a9f2..42ad1dfb0dd 100644 --- a/src/main/java/gregtech/api/capability/IDualHandler.java +++ b/src/main/java/gregtech/api/capability/IDualHandler.java @@ -8,7 +8,7 @@ public interface IDualHandler { boolean hasItemHandlers(); - IMultipleTankHandler getFluidTanks(); + MultipleTankHandler getFluidTanks(); IItemHandler getItemHandlers(); } diff --git a/src/main/java/gregtech/api/capability/IMultipleTankHandler.java b/src/main/java/gregtech/api/capability/IMultipleTankHandler.java deleted file mode 100644 index b9e89c71d30..00000000000 --- a/src/main/java/gregtech/api/capability/IMultipleTankHandler.java +++ /dev/null @@ -1,248 +0,0 @@ -package gregtech.api.capability; - -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.common.util.INBTSerializable; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.FluidTank; -import net.minecraftforge.fluids.FluidTankInfo; -import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidHandler; -import net.minecraftforge.fluids.capability.IFluidTankProperties; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; - -/** - * Base class for multi-tank fluid handlers. Handles insertion logic, along with other standard - * {@link IFluidHandler} functionalities. - * - * @see gregtech.api.capability.impl.FluidTankList FluidTankList - */ -public interface IMultipleTankHandler extends IFluidHandler, Iterable { - - /** - * Comparator for entries that can be used in insertion logic - */ - Comparator ENTRY_COMPARATOR = (o1, o2) -> { - // #1: non-empty tank first - boolean empty1 = o1.getFluidAmount() <= 0; - boolean empty2 = o2.getFluidAmount() <= 0; - if (empty1 != empty2) return empty1 ? 1 : -1; - - // #2: filter priority - IFilter filter1 = o1.getFilter(); - IFilter filter2 = o2.getFilter(); - if (filter1 == null) return filter2 == null ? 0 : 1; - if (filter2 == null) return -1; - return IFilter.FILTER_COMPARATOR.compare(filter1, filter2); - }; - - /** - * @return unmodifiable view of {@code MultiFluidTankEntry}s. Note that it's still possible to access - * and modify inner contents of the tanks. - */ - @NotNull - List getFluidTanks(); - - /** - * @return Number of tanks in this tank handler - */ - int getTanks(); - - @NotNull - MultiFluidTankEntry getTankAt(int index); - - /** - * @return {@code false} if insertion to this fluid handler enforces input to be - * filled in one slot at max. {@code true} if it bypasses the rule. - */ - boolean allowSameFluidFill(); - - /** - * Tries to search tank with contents equal to {@code fluidStack}. If {@code fluidStack} is - * {@code null}, an empty tank is searched instead. - * - * @param fluidStack Fluid stack to search index - * @return Index corresponding to tank at {@link #getFluidTanks()} with matching - */ - default int getIndexOfFluid(@Nullable FluidStack fluidStack) { - List fluidTanks = getFluidTanks(); - for (int i = 0; i < fluidTanks.size(); i++) { - FluidStack tankStack = fluidTanks.get(i).getFluid(); - if (fluidStack == tankStack || tankStack != null && tankStack.isFluidEqual(fluidStack)) { - return i; - } - } - return -1; - } - - @Override - default Iterator iterator() { - return getFluidTanks().iterator(); - } - - /** - * Entry of multi fluid tanks. Retains reference to original {@link IMultipleTankHandler} for accessing - * information such as {@link IMultipleTankHandler#allowSameFluidFill()}. - */ - final class MultiFluidTankEntry implements IFluidTank, IFluidHandler, IFilteredFluidContainer { - - private final IMultipleTankHandler tank; - private final IFluidTank delegate; - - public MultiFluidTankEntry(@NotNull IMultipleTankHandler tank, @NotNull IFluidTank delegate) { - this.tank = tank; - this.delegate = delegate; - } - - @NotNull - public IMultipleTankHandler getTank() { - return tank; - } - - @NotNull - public IFluidTank getDelegate() { - return delegate; - } - - public boolean allowSameFluidFill() { - return tank.allowSameFluidFill(); - } - - @Nullable - @Override - public IFilter getFilter() { - return this.delegate instanceof IFilteredFluidContainer filtered ? filtered.getFilter() : null; - } - - @NotNull - public IFluidTankProperties[] getTankProperties() { - return delegate instanceof IFluidHandler fluidHandler ? - fluidHandler.getTankProperties() : - new IFluidTankProperties[] { new FallbackTankProperty() }; - } - - public NBTTagCompound trySerialize() { - if (delegate instanceof FluidTank fluidTank) { - return fluidTank.writeToNBT(new NBTTagCompound()); - } else if (delegate instanceof INBTSerializable serializable) { - try { - return (NBTTagCompound) serializable.serializeNBT(); - } catch (ClassCastException ignored) {} - } - return new NBTTagCompound(); - } - - @SuppressWarnings({ "unchecked" }) - public void tryDeserialize(NBTTagCompound tag) { - if (delegate instanceof FluidTank fluidTank) { - fluidTank.readFromNBT(tag); - } else if (delegate instanceof INBTSerializable serializable) { - try { - serializable.deserializeNBT(tag); - } catch (ClassCastException ignored) {} - } - } - - @Nullable - @Override - public FluidStack getFluid() { - return delegate.getFluid(); - } - - @Override - public int getFluidAmount() { - return delegate.getFluidAmount(); - } - - @Override - public int getCapacity() { - return delegate.getCapacity(); - } - - @Override - public FluidTankInfo getInfo() { - return delegate.getInfo(); - } - - @Override - public int fill(FluidStack resource, boolean doFill) { - return delegate.fill(resource, doFill); - } - - @Nullable - @Override - public FluidStack drain(FluidStack resource, boolean doDrain) { - if (resource == null || resource.amount <= 0) { - return null; - } - if (delegate instanceof IFluidHandler fluidHandler) { - return fluidHandler.drain(resource, doDrain); - } - // just imitate the logic - FluidStack fluid = delegate.getFluid(); - return fluid != null && fluid.isFluidEqual(resource) ? drain(resource.amount, doDrain) : null; - } - - @Nullable - @Override - public FluidStack drain(int maxDrain, boolean doDrain) { - return delegate.drain(maxDrain, doDrain); - } - - @Override - public int hashCode() { - return delegate.hashCode(); - } - - @Override - @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") - public boolean equals(Object obj) { - return this == obj || delegate.equals(obj); - } - - @Override - public String toString() { - return delegate.toString(); - } - - private final class FallbackTankProperty implements IFluidTankProperties { - - @Nullable - @Override - public FluidStack getContents() { - return delegate.getFluid(); - } - - @Override - public int getCapacity() { - return delegate.getCapacity(); - } - - @Override - public boolean canFill() { - return true; - } - - @Override - public boolean canDrain() { - return true; - } - - @Override - public boolean canFillFluidType(FluidStack fluidStack) { - IFilter filter = getFilter(); - return filter == null || filter.test(fluidStack); - } - - @Override - public boolean canDrainFluidType(FluidStack fluidStack) { - return true; - } - } - } -} diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 25e19bb53f1..87e223a1c90 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -1,13 +1,12 @@ package gregtech.api.capability; -import gregtech.api.capability.impl.FluidTankList2; +import gregtech.api.capability.impl.FluidTankList; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTank; import net.minecraftforge.fluids.FluidTankInfo; -import net.minecraftforge.fluids.FluidUtil; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidTankProperties; @@ -24,7 +23,7 @@ * Base class for multi-tank fluid handlers. Handles insertion logic, along with other standard * {@link IFluidHandler} functionalities. * - * @see gregtech.api.capability.impl.FluidTankList FluidTankList + * @see FluidTankList FluidTankList */ public abstract class MultipleTankHandler implements IFluidHandler, Iterable, INBTSerializable { @@ -47,7 +46,7 @@ public abstract class MultipleTankHandler implements IFluidHandler, Iterable map = getRecipeMap(); if (map == null || !isRecipeMapValid(map)) { return null; @@ -681,7 +681,7 @@ protected static boolean areItemStacksEqual(@NotNull ItemStack stackA, @NotNull */ protected final @Nullable Recipe setupAndConsumeRecipeInputs(@NotNull Recipe recipe, @NotNull IItemHandlerModifiable importInventory, - @NotNull IMultipleTankHandler importFluids) { + @NotNull MultipleTankHandler importFluids) { calculateOverclock(recipe); modifyOverclockPost(ocResult, recipe.propertyStorage()); @@ -731,7 +731,7 @@ protected boolean checkOutputSpaceItems(@NotNull Recipe recipe, @NotNull IItemHa * @param exportFluids the inventory to output to * @return if the recipe can be successfully output to the inventory */ - protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull IMultipleTankHandler exportFluids) { + protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull MultipleTankHandler exportFluids) { // We have already trimmed fluid outputs at this time if (!metaTileEntity.canVoidRecipeFluidOutputs() && !GTTransferUtils.addFluidsToFluidHandler(exportFluids, true, recipe.getAllFluidOutputs())) { @@ -752,7 +752,7 @@ protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull IMulti */ protected @Nullable Recipe subTickOC(@NotNull OCResult ocResult, @NotNull Recipe recipe, @NotNull IItemHandlerModifiable importInventory, - @NotNull IMultipleTankHandler importFluids) { + @NotNull MultipleTankHandler importFluids) { RecipeMap map = getRecipeMap(); if (map == null) { return null; diff --git a/src/main/java/gregtech/api/capability/impl/BoilerRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/BoilerRecipeLogic.java index 28b2ee7afd0..e3bed4bf0e4 100644 --- a/src/main/java/gregtech/api/capability/impl/BoilerRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/BoilerRecipeLogic.java @@ -2,7 +2,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.IMultiblockController; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.recipes.Recipe; import gregtech.api.recipes.RecipeMap; import gregtech.api.recipes.RecipeMaps; @@ -77,7 +77,7 @@ protected void trySearchNewRecipe() { // can optimize with an override of checkPreviousRecipe() and a check here - IMultipleTankHandler importFluids = boiler.getImportFluids(); + MultipleTankHandler importFluids = boiler.getImportFluids(); boolean didStartRecipe = false; for (IFluidTank fluidTank : importFluids.getFluidTanks()) { diff --git a/src/main/java/gregtech/api/capability/impl/DistillationTowerLogicHandler.java b/src/main/java/gregtech/api/capability/impl/DistillationTowerLogicHandler.java index e674db66763..8e99c04e370 100644 --- a/src/main/java/gregtech/api/capability/impl/DistillationTowerLogicHandler.java +++ b/src/main/java/gregtech/api/capability/impl/DistillationTowerLogicHandler.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IDistillationTower; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.multiblock.IMultiblockAbilityPart; import gregtech.api.metatileentity.multiblock.MultiblockAbility; import gregtech.api.pattern.BlockPattern; @@ -31,7 +31,7 @@ public class DistillationTowerLogicHandler { private int layerCount; private List orderedFluidOutputs; - private IMultipleTankHandler fluidTanks; + private MultipleTankHandler fluidTanks; public DistillationTowerLogicHandler(IDistillationTower tower) { this.tower = tower; @@ -138,11 +138,11 @@ public List getOrderedFluidOutputs() { return orderedFluidOutputs; } - protected void setFluidTanks(IMultipleTankHandler fluidTanks) { + protected void setFluidTanks(MultipleTankHandler fluidTanks) { this.fluidTanks = fluidTanks; } - public IMultipleTankHandler getFluidTanks() { + public MultipleTankHandler getFluidTanks() { return fluidTanks; } diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList.java b/src/main/java/gregtech/api/capability/impl/FluidTankList.java index 29594258fb4..b8643b4c630 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList.java @@ -1,145 +1,101 @@ package gregtech.api.capability.impl; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; import net.minecraftforge.common.util.Constants; -import net.minecraftforge.common.util.INBTSerializable; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.IFluidTankProperties; +import org.apache.commons.lang3.ArrayUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -public class FluidTankList implements IMultipleTankHandler, INBTSerializable { +public final class FluidTankList extends MultipleTankHandler { - private final MultiFluidTankEntry[] fluidTanks; private final boolean allowSameFluidFill; + private Entry[] tanks = new Entry[0]; public FluidTankList(boolean allowSameFluidFill, IFluidTank... fluidTanks) { - ArrayList list = new ArrayList<>(); - for (IFluidTank tank : fluidTanks) list.add(wrapIntoEntry(tank)); - this.fluidTanks = list.toArray(new MultiFluidTankEntry[0]); + if (!ArrayUtils.isEmpty(fluidTanks)) { + tanks = new Entry[fluidTanks.length]; + Arrays.setAll(tanks, value -> wrap(fluidTanks[value])); + } this.allowSameFluidFill = allowSameFluidFill; } public FluidTankList(boolean allowSameFluidFill, @NotNull List fluidTanks) { - ArrayList list = new ArrayList<>(); - for (IFluidTank tank : fluidTanks) list.add(wrapIntoEntry(tank)); - this.fluidTanks = list.toArray(new MultiFluidTankEntry[0]); - this.allowSameFluidFill = allowSameFluidFill; + this(allowSameFluidFill, fluidTanks.toArray(new IFluidTank[0])); } - public FluidTankList(boolean allowSameFluidFill, @NotNull IMultipleTankHandler parent, + public FluidTankList(boolean allowSameFluidFill, @NotNull MultipleTankHandler parent, IFluidTank... additionalTanks) { - ArrayList list = new ArrayList<>(parent.getFluidTanks()); - for (IFluidTank tank : additionalTanks) list.add(wrapIntoEntry(tank)); - this.fluidTanks = list.toArray(new MultiFluidTankEntry[0]); - this.allowSameFluidFill = allowSameFluidFill; - } - - private MultiFluidTankEntry wrapIntoEntry(IFluidTank tank) { - return tank instanceof MultiFluidTankEntry entry ? entry : new MultiFluidTankEntry(this, tank); - } - - @NotNull - @Override - public List getFluidTanks() { - return Collections.unmodifiableList(Arrays.asList(fluidTanks)); - } + int tanks = parent.size(); + int additional = 0; - @Override - public int getTanks() { - return fluidTanks.length; - } + if (!ArrayUtils.isEmpty(additionalTanks)) + additional = additionalTanks.length; - @NotNull - @Override - public MultiFluidTankEntry getTankAt(int index) { - return fluidTanks[index]; - } + this.tanks = new Entry[tanks + additional]; - @NotNull - @Override - public IFluidTankProperties[] getTankProperties() { - ArrayList propertiesList = new ArrayList<>(); - for (MultiFluidTankEntry fluidTank : fluidTanks) { - Collections.addAll(propertiesList, fluidTank.getTankProperties()); - } - return propertiesList.toArray(new IFluidTankProperties[0]); - } + Arrays.setAll(this.tanks, value -> { + if (value < tanks) return parent.getTankAt(value); + else return wrap(additionalTanks[value - tanks]); + }); - @Override - public boolean allowSameFluidFill() { - return allowSameFluidFill; + this.allowSameFluidFill = allowSameFluidFill; } @Override public int fill(FluidStack resource, boolean doFill) { - if (resource == null || resource.amount <= 0) { + if (resource == null || resource.amount <= 0) return 0; - } + + FluidStack copy = resource.copy(); + int totalInserted = 0; - boolean inputFluidCopied = false; - // flag value indicating whether the fluid was stored in 'distinct' slot at least once - boolean distinctSlotVisited = false; - MultiFluidTankEntry[] fluidTanks = this.fluidTanks.clone(); - Arrays.sort(fluidTanks, IMultipleTankHandler.ENTRY_COMPARATOR); + Entry[] fluidTanks = this.tanks.clone(); + Arrays.sort(fluidTanks, ENTRY_COMPARATOR); // search for tanks with same fluid type first - for (MultiFluidTankEntry tank : fluidTanks) { + for (Entry tank : fluidTanks) { + if (tank.getFluidAmount() == 0 || !resource.isFluidEqual(tank.getFluid())) + continue; + // if the fluid to insert matches the tank, insert the fluid - if (resource.isFluidEqual(tank.getFluid())) { - int inserted = tank.fill(resource, doFill); - if (inserted > 0) { - totalInserted += inserted; - if (resource.amount - inserted <= 0) { - return totalInserted; - } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; - } - // regardless of whether the insertion succeeded, presence of identical fluid in - // a slot prevents distinct fill to other slots - if (!tank.allowSameFluidFill()) { - distinctSlotVisited = true; - } - } + int inserted = tank.fill(copy, doFill); + if (inserted <= 0) continue; + + totalInserted += inserted; + copy.amount -= inserted; + if (copy.amount <= 0) return totalInserted; } + + boolean overflow = false; + // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid - for (MultiFluidTankEntry tank : fluidTanks) { + for (Entry tank : fluidTanks) { // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had // received the fluid, skip this tank - boolean usesDistinctFluidFill = tank.allowSameFluidFill(); - if ((usesDistinctFluidFill || !distinctSlotVisited) && tank.getFluidAmount() == 0) { - int inserted = tank.fill(resource, doFill); - if (inserted > 0) { - totalInserted += inserted; - if (resource.amount - inserted <= 0) { - return totalInserted; - } - if (!inputFluidCopied) { - inputFluidCopied = true; - resource = resource.copy(); - } - resource.amount -= inserted; - if (!usesDistinctFluidFill) { - distinctSlotVisited = true; - } - } - } + if (overflow && !tank.allowSameFluidFill()) continue; + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) continue; + + int inserted = tank.fill(copy, doFill); + if (inserted <= 0) continue; + + totalInserted += inserted; + copy.amount -= inserted; + if (copy.amount <= 0) return totalInserted; + else overflow = true; } + // return the amount of fluid that was inserted return totalInserted; } @@ -147,12 +103,12 @@ public int fill(FluidStack resource, boolean doFill) { @Nullable @Override public FluidStack drain(FluidStack resource, boolean doDrain) { - if (resource == null || resource.amount <= 0) { + if (resource == null || resource.amount <= 0) return null; - } + int amountLeft = resource.amount; FluidStack totalDrained = null; - for (IFluidTank handler : fluidTanks) { + for (IFluidTank handler : tanks) { if (!resource.isFluidEqual(handler.getFluid())) { continue; } @@ -175,29 +131,28 @@ public FluidStack drain(FluidStack resource, boolean doDrain) { @Nullable @Override public FluidStack drain(int maxDrain, boolean doDrain) { - if (maxDrain <= 0) { - return null; - } + if (maxDrain <= 0) return null; + FluidStack totalDrained = null; - for (IFluidTank handler : fluidTanks) { + for (IFluidTank handler : tanks) { if (totalDrained == null) { - totalDrained = handler.drain(maxDrain, doDrain); - if (totalDrained != null) { - maxDrain -= totalDrained.amount; - } + var drained = handler.drain(maxDrain, doDrain); + if (drained == null) continue; + + totalDrained = drained.copy(); + maxDrain -= totalDrained.amount; + } else { - if (!totalDrained.isFluidEqual(handler.getFluid())) { + if (!totalDrained.isFluidEqual(handler.getFluid())) continue; - } + FluidStack drain = handler.drain(maxDrain, doDrain); - if (drain != null) { - totalDrained.amount += drain.amount; - maxDrain -= drain.amount; - } - } - if (maxDrain <= 0) { - return totalDrained; + if (drain == null) continue; + + totalDrained.amount += drain.amount; + maxDrain -= drain.amount; } + if (maxDrain <= 0) return totalDrained; } return totalDrained; } @@ -206,8 +161,8 @@ public FluidStack drain(int maxDrain, boolean doDrain) { public NBTTagCompound serializeNBT() { NBTTagCompound fluidInventory = new NBTTagCompound(); NBTTagList tanks = new NBTTagList(); - for (int i = 0; i < this.getTanks(); i++) { - tanks.appendTag(this.fluidTanks[i].trySerialize()); + for (Entry tank : this.tanks) { + tanks.appendTag(tank.serializeNBT()); } fluidInventory.setTag("Tanks", tanks); return fluidInventory; @@ -216,28 +171,53 @@ public NBTTagCompound serializeNBT() { @Override public void deserializeNBT(NBTTagCompound nbt) { NBTTagList tanks = nbt.getTagList("Tanks", Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < Math.min(fluidTanks.length, tanks.tagCount()); i++) { - this.fluidTanks[i].tryDeserialize(tanks.getCompoundTagAt(i)); + for (int i = 0; i < tanks.tagCount(); i++) { + this.tanks[i].deserializeNBT(tanks.getCompoundTagAt(i)); } } + @Override + public IFluidTankProperties[] getTankProperties() { + return this.tanks; + } + + @Override + public @NotNull List getFluidTanks() { + return Collections.unmodifiableList(Arrays.asList(this.tanks)); + } + + @Override + public int size() { + return tanks.length; + } + + @Override + public @NotNull Entry getTankAt(int index) { + return tanks[index]; + } + + @Override + public boolean allowSameFluidFill() { + return allowSameFluidFill; + } + @Override public String toString() { return toString(false); } public String toString(boolean lineBreak) { - StringBuilder stb = new StringBuilder("FluidTankList[").append(this.fluidTanks.length).append(";"); - for (int i = 0; i < this.fluidTanks.length; i++) { + StringBuilder stb = new StringBuilder("FluidTankList[").append(this.tanks.length).append(";"); + for (int i = 0; i < this.tanks.length; i++) { if (i != 0) stb.append(','); stb.append(lineBreak ? "\n " : " "); - FluidStack fluid = this.fluidTanks[i].getFluid(); + FluidStack fluid = this.tanks[i].getFluid(); if (fluid == null || fluid.amount == 0) { - stb.append("None 0 / ").append(this.fluidTanks[i].getCapacity()); + stb.append("None 0 / ").append(this.tanks[i].getCapacity()); } else { stb.append(fluid.getFluid().getName()).append(' ').append(fluid.amount) - .append(" / ").append(this.fluidTanks[i].getCapacity()); + .append(" / ").append(this.tanks[i].getCapacity()); } } if (lineBreak) stb.append('\n'); diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java b/src/main/java/gregtech/api/capability/impl/FluidTankList2.java deleted file mode 100644 index 072a1dd8017..00000000000 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList2.java +++ /dev/null @@ -1,226 +0,0 @@ -package gregtech.api.capability.impl; - -import gregtech.api.capability.MultipleTankHandler; - -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; -import net.minecraftforge.common.util.Constants; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidTankProperties; - -import org.apache.commons.lang3.ArrayUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public final class FluidTankList2 extends MultipleTankHandler { - - private final boolean allowSameFluidFill; - private Entry[] tanks = new Entry[0]; - - public FluidTankList2(boolean allowSameFluidFill, IFluidTank... fluidTanks) { - if (!ArrayUtils.isEmpty(fluidTanks)) { - tanks = new Entry[fluidTanks.length]; - Arrays.setAll(tanks, value -> wrap(fluidTanks[value])); - } - this.allowSameFluidFill = allowSameFluidFill; - } - - public FluidTankList2(boolean allowSameFluidFill, @NotNull List fluidTanks) { - this(allowSameFluidFill, fluidTanks.toArray(new IFluidTank[0])); - } - - public FluidTankList2(boolean allowSameFluidFill, @NotNull MultipleTankHandler parent, - IFluidTank... additionalTanks) { - int tanks = parent.size(); - int additional = 0; - - if (!ArrayUtils.isEmpty(additionalTanks)) - additional = additionalTanks.length; - - this.tanks = new Entry[tanks + additional]; - - Arrays.setAll(this.tanks, value -> { - if (value < tanks) return parent.getTankAt(value); - else return wrap(additionalTanks[value - tanks]); - }); - - this.allowSameFluidFill = allowSameFluidFill; - } - - @Override - public int fill(FluidStack resource, boolean doFill) { - if (resource == null || resource.amount <= 0) - return 0; - - FluidStack copy = resource.copy(); - - int totalInserted = 0; - - Entry[] fluidTanks = this.tanks.clone(); - Arrays.sort(fluidTanks, ENTRY_COMPARATOR); - - // search for tanks with same fluid type first - for (Entry tank : fluidTanks) { - if (tank.getFluidAmount() == 0 || !resource.isFluidEqual(tank.getFluid())) - continue; - - // if the fluid to insert matches the tank, insert the fluid - int inserted = tank.fill(copy, doFill); - if (inserted <= 0) continue; - - totalInserted += inserted; - copy.amount -= inserted; - if (copy.amount <= 0) return totalInserted; - } - - boolean overflow = false; - - // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid - for (Entry tank : fluidTanks) { - // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had - // received the fluid, skip this tank - if (overflow && !tank.allowSameFluidFill()) continue; - if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) continue; - - int inserted = tank.fill(copy, doFill); - if (inserted <= 0) continue; - - totalInserted += inserted; - copy.amount -= inserted; - if (copy.amount <= 0) return totalInserted; - else overflow = true; - } - - // return the amount of fluid that was inserted - return totalInserted; - } - - @Nullable - @Override - public FluidStack drain(FluidStack resource, boolean doDrain) { - if (resource == null || resource.amount <= 0) - return null; - - int amountLeft = resource.amount; - FluidStack totalDrained = null; - for (IFluidTank handler : tanks) { - if (!resource.isFluidEqual(handler.getFluid())) { - continue; - } - FluidStack drain = handler.drain(amountLeft, doDrain); - if (drain != null) { - if (totalDrained == null) { - totalDrained = drain; - } else { - totalDrained.amount += drain.amount; - } - amountLeft -= drain.amount; - if (amountLeft <= 0) { - return totalDrained; - } - } - } - return totalDrained; - } - - @Nullable - @Override - public FluidStack drain(int maxDrain, boolean doDrain) { - if (maxDrain <= 0) return null; - - FluidStack totalDrained = null; - for (IFluidTank handler : tanks) { - if (totalDrained == null) { - var drained = handler.drain(maxDrain, doDrain); - if (drained == null) continue; - - totalDrained = drained.copy(); - maxDrain -= totalDrained.amount; - - } else { - if (!totalDrained.isFluidEqual(handler.getFluid())) - continue; - - FluidStack drain = handler.drain(maxDrain, doDrain); - if (drain == null) continue; - - totalDrained.amount += drain.amount; - maxDrain -= drain.amount; - } - if (maxDrain <= 0) return totalDrained; - } - return totalDrained; - } - - @Override - public NBTTagCompound serializeNBT() { - NBTTagCompound fluidInventory = new NBTTagCompound(); - NBTTagList tanks = new NBTTagList(); - for (Entry tank : this.tanks) { - tanks.appendTag(tank.serializeNBT()); - } - fluidInventory.setTag("Tanks", tanks); - return fluidInventory; - } - - @Override - public void deserializeNBT(NBTTagCompound nbt) { - NBTTagList tanks = nbt.getTagList("Tanks", Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < tanks.tagCount(); i++) { - this.tanks[i].deserializeNBT(tanks.getCompoundTagAt(i)); - } - } - - @Override - public IFluidTankProperties[] getTankProperties() { - return this.tanks; - } - - @Override - public @NotNull List getFluidTanks() { - return Collections.unmodifiableList(Arrays.asList(this.tanks)); - } - - @Override - public int size() { - return tanks.length; - } - - @Override - public @NotNull Entry getTankAt(int index) { - return tanks[index]; - } - - @Override - public boolean allowSameFluidFill() { - return allowSameFluidFill; - } - - @Override - public String toString() { - return toString(false); - } - - public String toString(boolean lineBreak) { - StringBuilder stb = new StringBuilder("FluidTankList[").append(this.tanks.length).append(";"); - for (int i = 0; i < this.tanks.length; i++) { - if (i != 0) stb.append(','); - stb.append(lineBreak ? "\n " : " "); - - FluidStack fluid = this.tanks[i].getFluid(); - if (fluid == null || fluid.amount == 0) { - stb.append("None 0 / ").append(this.tanks[i].getCapacity()); - } else { - stb.append(fluid.getFluid().getName()).append(' ').append(fluid.amount) - .append(" / ").append(this.tanks[i].getCapacity()); - } - } - if (lineBreak) stb.append('\n'); - return stb.append(']').toString(); - } -} diff --git a/src/main/java/gregtech/api/capability/impl/MultiblockRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/MultiblockRecipeLogic.java index ff95f0fbb0c..aff48abec99 100644 --- a/src/main/java/gregtech/api/capability/impl/MultiblockRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/MultiblockRecipeLogic.java @@ -4,7 +4,7 @@ import gregtech.api.capability.IEnergyContainer; import gregtech.api.capability.IMultiblockController; import gregtech.api.capability.IMultipleRecipeMaps; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.multiblock.MultiblockAbility; import gregtech.api.metatileentity.multiblock.MultiblockWithDisplayBase; import gregtech.api.metatileentity.multiblock.RecipeMapMultiblockController; @@ -94,13 +94,13 @@ protected IItemHandlerModifiable getOutputInventory() { } @Override - protected IMultipleTankHandler getInputTank() { + protected MultipleTankHandler getInputTank() { RecipeMapMultiblockController controller = (RecipeMapMultiblockController) metaTileEntity; return controller.getInputFluidInventory(); } @Override - protected IMultipleTankHandler getOutputTank() { + protected MultipleTankHandler getOutputTank() { RecipeMapMultiblockController controller = (RecipeMapMultiblockController) metaTileEntity; return controller.getOutputFluidInventory(); } @@ -200,7 +200,7 @@ protected void trySearchNewRecipeDistinct() { long maxVoltage = getMaxVoltage(); Recipe currentRecipe; List importInventory = getInputBuses(); - IMultipleTankHandler importFluids = getInputTank(); + MultipleTankHandler importFluids = getInputTank(); // Our caching implementation // This guarantees that if we get a recipe cache hit, our efficiency is no different from other machines diff --git a/src/main/java/gregtech/api/capability/impl/SteamMultiblockRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/SteamMultiblockRecipeLogic.java index 2c05b255d40..94674cda7ff 100644 --- a/src/main/java/gregtech/api/capability/impl/SteamMultiblockRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/SteamMultiblockRecipeLogic.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.GTValues; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.multiblock.RecipeMapSteamMultiblockController; import gregtech.api.recipes.Recipe; import gregtech.api.recipes.RecipeMap; @@ -27,14 +27,14 @@ public class SteamMultiblockRecipeLogic extends AbstractRecipeLogic { - private IMultipleTankHandler steamFluidTank; + private MultipleTankHandler steamFluidTank; private IFluidTank steamFluidTankCombined; // EU per mB private final double conversionRate; public SteamMultiblockRecipeLogic(RecipeMapSteamMultiblockController tileEntity, RecipeMap recipeMap, - IMultipleTankHandler steamFluidTank, double conversionRate) { + MultipleTankHandler steamFluidTank, double conversionRate) { super(tileEntity, recipeMap); this.steamFluidTank = steamFluidTank; this.conversionRate = conversionRate; @@ -59,7 +59,7 @@ protected IItemHandlerModifiable getOutputInventory() { return controller.getOutputInventory(); } - protected IMultipleTankHandler getSteamFluidTank() { + protected MultipleTankHandler getSteamFluidTank() { RecipeMapSteamMultiblockController controller = (RecipeMapSteamMultiblockController) metaTileEntity; return controller.getSteamFluidTank(); } @@ -69,7 +69,7 @@ private void combineSteamTanks() { if (steamFluidTank == null) steamFluidTankCombined = new FluidTank(0); else { - int capacity = steamFluidTank.getTanks() * 64000; + int capacity = steamFluidTank.size() * 64000; steamFluidTankCombined = new FluidTank(capacity); steamFluidTankCombined.fill(steamFluidTank.drain(capacity, false), true); } diff --git a/src/main/java/gregtech/api/metatileentity/SimpleMachineMetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/SimpleMachineMetaTileEntity.java index 64b8d3c86eb..3db5a293efb 100644 --- a/src/main/java/gregtech/api/metatileentity/SimpleMachineMetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/SimpleMachineMetaTileEntity.java @@ -182,7 +182,7 @@ public void addCover(@NotNull EnumFacing side, @NotNull Cover cover) { @SideOnly(Side.CLIENT) public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline) { super.renderMetaTileEntity(renderState, translation, pipeline); - if (outputFacingFluids != null && getExportFluids().getTanks() > 0) { + if (outputFacingFluids != null && getExportFluids().size() > 0) { Textures.PIPE_OUT_OVERLAY.renderSided(outputFacingFluids, renderState, RenderUtil.adjustTrans(translation, outputFacingFluids, 2), pipeline); } @@ -508,7 +508,7 @@ protected ModularUI.Builder createGuiTemplate(EntityPlayer player) { .shouldUseBaseBackground()); leftButtonStartX += 18; } - if (exportFluids.getTanks() > 0) { + if (exportFluids.size() > 0) { builder.widget(new ToggleButtonWidget(leftButtonStartX, 62 + yOffset, 18, 18, GuiTextures.BUTTON_FLUID_OUTPUT, this::isAutoOutputFluids, this::setAutoOutputFluids) .setTooltipText("gregtech.gui.fluid_auto_output.tooltip") @@ -521,7 +521,7 @@ protected ModularUI.Builder createGuiTemplate(EntityPlayer player) { .setTooltipHoverString("gregtech.gui.overclock.description") .setButtonTexture(GuiTextures.BUTTON_OVERCLOCK)); - if (exportItems.getSlots() + exportFluids.getTanks() <= 9) { + if (exportItems.getSlots() + exportFluids.size() <= 9) { ImageWidget logo = new ImageWidget(152, 63 + yOffset, 17, 17, GTValues.XMAS.get() ? getXmasLogo() : getLogo()).setIgnoreColor(true); diff --git a/src/main/java/gregtech/api/metatileentity/multiblock/FuelMultiblockController.java b/src/main/java/gregtech/api/metatileentity/multiblock/FuelMultiblockController.java index 790a150ec0c..7fe5302040c 100644 --- a/src/main/java/gregtech/api/metatileentity/multiblock/FuelMultiblockController.java +++ b/src/main/java/gregtech/api/metatileentity/multiblock/FuelMultiblockController.java @@ -2,7 +2,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.IEnergyContainer; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.EnergyContainerList; import gregtech.api.capability.impl.MultiblockFuelRecipeLogic; import gregtech.api.recipes.RecipeMap; @@ -123,12 +123,12 @@ public List getDataInfo() { return list; } - protected int[] getTotalFluidAmount(FluidStack testStack, IMultipleTankHandler multiTank) { + protected int[] getTotalFluidAmount(FluidStack testStack, MultipleTankHandler multiTank) { int fluidAmount = 0; int fluidCapacity = 0; for (var tank : multiTank) { if (tank != null) { - FluidStack drainStack = tank.drain(testStack, false); + FluidStack drainStack = tank.drain(testStack.amount, false); if (drainStack != null && drainStack.amount > 0) { fluidAmount += drainStack.amount; fluidCapacity += tank.getCapacity(); diff --git a/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapMultiblockController.java b/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapMultiblockController.java index 4949074c91a..bedf55286dc 100644 --- a/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapMultiblockController.java +++ b/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapMultiblockController.java @@ -3,7 +3,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.IDistinctBusController; import gregtech.api.capability.IEnergyContainer; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.EnergyContainerList; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.capability.impl.ItemHandlerList; @@ -45,8 +45,8 @@ public abstract class RecipeMapMultiblockController extends MultiblockWithDispla protected MultiblockRecipeLogic recipeMapWorkable; protected IItemHandlerModifiable inputInventory; protected IItemHandlerModifiable outputInventory; - protected IMultipleTankHandler inputFluidInventory; - protected IMultipleTankHandler outputFluidInventory; + protected MultipleTankHandler inputFluidInventory; + protected MultipleTankHandler outputFluidInventory; protected IEnergyContainer energyContainer; private boolean isDistinct = false; @@ -72,11 +72,11 @@ public IItemHandlerModifiable getOutputInventory() { return outputInventory; } - public IMultipleTankHandler getInputFluidInventory() { + public MultipleTankHandler getInputFluidInventory() { return inputFluidInventory; } - public IMultipleTankHandler getOutputFluidInventory() { + public MultipleTankHandler getOutputFluidInventory() { return outputFluidInventory; } diff --git a/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapSteamMultiblockController.java b/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapSteamMultiblockController.java index fd396609419..0d0e6b93bd3 100644 --- a/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapSteamMultiblockController.java +++ b/src/main/java/gregtech/api/metatileentity/multiblock/RecipeMapSteamMultiblockController.java @@ -1,6 +1,6 @@ package gregtech.api.metatileentity.multiblock; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.capability.impl.ItemHandlerList; import gregtech.api.capability.impl.SteamMultiblockRecipeLogic; @@ -41,7 +41,7 @@ public abstract class RecipeMapSteamMultiblockController extends MultiblockWithD protected IItemHandlerModifiable inputInventory; protected IItemHandlerModifiable outputInventory; - protected IMultipleTankHandler steamFluidTank; + protected MultipleTankHandler steamFluidTank; public RecipeMapSteamMultiblockController(ResourceLocation metaTileEntityId, RecipeMap recipeMap, double conversionRate) { @@ -59,7 +59,7 @@ public IItemHandlerModifiable getOutputInventory() { return outputInventory; } - public IMultipleTankHandler getSteamFluidTank() { + public MultipleTankHandler getSteamFluidTank() { return steamFluidTank; } diff --git a/src/main/java/gregtech/api/recipes/Recipe.java b/src/main/java/gregtech/api/recipes/Recipe.java index ff495651ac5..f1abc7b3271 100644 --- a/src/main/java/gregtech/api/recipes/Recipe.java +++ b/src/main/java/gregtech/api/recipes/Recipe.java @@ -1,6 +1,6 @@ package gregtech.api.recipes; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.recipes.category.GTRecipeCategory; import gregtech.api.recipes.chance.boost.ChanceBoostFunction; import gregtech.api.recipes.chance.output.ChancedOutputList; @@ -185,7 +185,7 @@ public static Recipe trimRecipeOutputs(Recipe currentRecipe, RecipeMap recipe } public final boolean matches(boolean consumeIfSuccessful, IItemHandlerModifiable inputs, - IMultipleTankHandler fluidInputs) { + MultipleTankHandler fluidInputs) { Pair fluids = null; Pair items = null; diff --git a/src/main/java/gregtech/api/recipes/RecipeMap.java b/src/main/java/gregtech/api/recipes/RecipeMap.java index a2c2ef7b1f4..3abda0813fa 100644 --- a/src/main/java/gregtech/api/recipes/RecipeMap.java +++ b/src/main/java/gregtech/api/recipes/RecipeMap.java @@ -2,7 +2,7 @@ import gregtech.api.GTValues; import gregtech.api.GregTechAPI; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.gui.ModularUI; import gregtech.api.gui.resources.TextureArea; @@ -533,7 +533,7 @@ amount, getMaxFluidOutputs()), } @Nullable - public Recipe findRecipe(long voltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs) { + public Recipe findRecipe(long voltage, IItemHandlerModifiable inputs, MultipleTankHandler fluidInputs) { return this.findRecipe(voltage, GTUtility.itemHandlerToList(inputs), GTUtility.fluidHandlerToList(fluidInputs)); } diff --git a/src/main/java/gregtech/api/recipes/logic/IParallelableRecipeLogic.java b/src/main/java/gregtech/api/recipes/logic/IParallelableRecipeLogic.java index 423c1e4357d..06b93367537 100644 --- a/src/main/java/gregtech/api/recipes/logic/IParallelableRecipeLogic.java +++ b/src/main/java/gregtech/api/recipes/logic/IParallelableRecipeLogic.java @@ -1,6 +1,6 @@ package gregtech.api.recipes.logic; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.IVoidable; import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.multiblock.ParallelLogicType; @@ -42,9 +42,9 @@ default void applyParallelBonus(@NotNull RecipeBuilder builder) {} default RecipeBuilder findMultipliedParallelRecipe(@NotNull RecipeMap recipeMap, @NotNull Recipe currentRecipe, @NotNull IItemHandlerModifiable inputs, - @NotNull IMultipleTankHandler fluidInputs, + @NotNull MultipleTankHandler fluidInputs, @NotNull IItemHandlerModifiable outputs, - @NotNull IMultipleTankHandler fluidOutputs, int parallelLimit, + @NotNull MultipleTankHandler fluidOutputs, int parallelLimit, long maxVoltage, @NotNull IVoidable voidable) { return ParallelLogic.doParallelRecipes( currentRecipe, @@ -85,9 +85,9 @@ default RecipeBuilder findAppendedParallelItemRecipe(@NotNull RecipeMap re // Recipes passed in here should be already trimmed, if desired default Recipe findParallelRecipe(@NotNull Recipe currentRecipe, @NotNull IItemHandlerModifiable inputs, - @NotNull IMultipleTankHandler fluidInputs, + @NotNull MultipleTankHandler fluidInputs, @NotNull IItemHandlerModifiable outputs, - @NotNull IMultipleTankHandler fluidOutputs, long maxVoltage, int parallelLimit) { + @NotNull MultipleTankHandler fluidOutputs, long maxVoltage, int parallelLimit) { if (parallelLimit > 1 && getRecipeMap() != null) { RecipeBuilder parallelBuilder = switch (getParallelLogicType()) { case MULTIPLY -> findMultipliedParallelRecipe(getRecipeMap(), currentRecipe, inputs, fluidInputs, diff --git a/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java b/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java index c056890d086..1381086cefe 100644 --- a/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java +++ b/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java @@ -1,6 +1,6 @@ package gregtech.api.recipes.logic; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.IVoidable; import gregtech.api.recipes.FluidKey; import gregtech.api.recipes.Recipe; @@ -10,7 +10,6 @@ import gregtech.api.util.GTHashMaps; import gregtech.api.util.GTUtility; import gregtech.api.util.ItemStackHashStrategy; -import gregtech.api.util.OverlayedFluidHandler; import gregtech.api.util.OverlayedItemHandler; import net.minecraft.item.ItemStack; @@ -41,7 +40,7 @@ public abstract class ParallelLogic { */ public static int getMaxRecipeMultiplier(@NotNull Recipe recipe, @NotNull IItemHandlerModifiable inputs, - @NotNull IMultipleTankHandler fluidInputs, int parallelAmount) { + @NotNull MultipleTankHandler fluidInputs, int parallelAmount) { // Find all the items in the combined Item Input inventories and create oversized ItemStacks Object2IntMap ingredientStacks = GTHashMaps.fromItemHandler(inputs); @@ -71,7 +70,7 @@ public static int getMaxRecipeMultiplier(@NotNull Recipe recipe, @NotNull IItemH * @return returns the amount of recipes that can be merged successfully into a given output inventory */ public static int limitByOutputMerging(@NotNull Recipe recipe, @NotNull IItemHandlerModifiable outputs, - @NotNull IMultipleTankHandler fluidOutputs, int parallelAmount, + @NotNull MultipleTankHandler fluidOutputs, int parallelAmount, boolean voidItems, boolean voidFluids) { int modifiedItemParallelAmount = Integer.MAX_VALUE; int modifiedFluidParallelAmount = Integer.MAX_VALUE; @@ -103,7 +102,7 @@ public static int limitByOutputMerging(@NotNull Recipe recipe, @NotNull IItemHan if (voidFluids) { modifiedFluidParallelAmount = parallelAmount; } else { - modifiedFluidParallelAmount = limitParallelByFluids(recipe, new OverlayedFluidHandler(fluidOutputs), + modifiedFluidParallelAmount = limitParallelByFluids(recipe, fluidOutputs, modifiedItemParallelAmount); } @@ -120,7 +119,7 @@ public static int limitByOutputMerging(@NotNull Recipe recipe, @NotNull IItemHan * @param recipe the recipe from which we get the input to product ratio * @param multiplier the maximum possible multiplied we can get from the input inventory * see - * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, IMultipleTankHandler, int)} + * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, MultipleTankHandler, int)} * @return the amount of times a {@link Recipe} outputs can be merged into an inventory without * voiding products. */ @@ -166,7 +165,7 @@ public static int limitParallelByItems(@NotNull Recipe recipe, @NotNull Overlaye * @param outputsToAppend the recipe outputs from the recipe we want to append to the recipe we are building * @param multiplier the maximum possible multiplied we can get from the input inventory * see - * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, IMultipleTankHandler, int)} + * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, MultipleTankHandler, int)} * @return the amount of times a {@link Recipe} outputs can be merged into an inventory without * voiding products. */ @@ -253,17 +252,16 @@ public static int limitParallelByItemsIncremental(@NotNull List recip * @param recipe the recipe from which we get the fluid input to product ratio * @param multiplier the maximum possible multiplied we can get from the input tanks * see - * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, IMultipleTankHandler, int)} + * {@link ParallelLogic#getMaxRecipeMultiplier(Recipe, IItemHandlerModifiable, MultipleTankHandler, int)} * @return the amount of times a {@link Recipe} outputs can be merged into a fluid handler without * voiding products. */ public static int limitParallelByFluids(@NotNull Recipe recipe, - @NotNull OverlayedFluidHandler overlayedFluidHandler, int multiplier) { + @NotNull MultipleTankHandler overlayedFluidHandler, int multiplier) { int minMultiplier = 0; int maxMultiplier = multiplier; while (minMultiplier != maxMultiplier) { - overlayedFluidHandler.reset(); int amountLeft = 0; @@ -275,7 +273,7 @@ public static int limitParallelByFluids(@NotNull Recipe recipe, } else { amountLeft = fluidStack.amount * multiplier; } - int inserted = overlayedFluidHandler.insertFluid(fluidStack, amountLeft); + int inserted = overlayedFluidHandler.fill(fluidStack, false); if (inserted > 0) { amountLeft -= inserted; } @@ -476,9 +474,9 @@ protected static int getMaxRatioFluid(@NotNull Map countFluid // take care of voiding public static RecipeBuilder doParallelRecipes(@NotNull Recipe currentRecipe, @NotNull RecipeMap recipeMap, @NotNull IItemHandlerModifiable importInventory, - @NotNull IMultipleTankHandler importFluids, + @NotNull MultipleTankHandler importFluids, @NotNull IItemHandlerModifiable exportInventory, - @NotNull IMultipleTankHandler exportFluids, int parallelAmount, + @NotNull MultipleTankHandler exportFluids, int parallelAmount, long maxVoltage, @NotNull IVoidable voidable) { // First check if we are limited by recipe inputs. This can short circuit a lot of consecutive checking int multiplierByInputs = getMaxRecipeMultiplier(currentRecipe, importInventory, importFluids, parallelAmount); diff --git a/src/main/java/gregtech/api/recipes/ui/RecipeMapUI.java b/src/main/java/gregtech/api/recipes/ui/RecipeMapUI.java index a6d66d3e62e..42f61345e63 100644 --- a/src/main/java/gregtech/api/recipes/ui/RecipeMapUI.java +++ b/src/main/java/gregtech/api/recipes/ui/RecipeMapUI.java @@ -192,7 +192,7 @@ protected void addInventorySlotGroup(@NotNull ModularUI.Builder builder, @NotNull IItemHandlerModifiable itemHandler, @NotNull FluidTankList fluidHandler, boolean isOutputs, int yOffset) { int itemInputsCount = itemHandler.getSlots(); - int fluidInputsCount = fluidHandler.getTanks(); + int fluidInputsCount = fluidHandler.size(); boolean invertFluids = false; if (itemInputsCount == 0) { int tmp = itemInputsCount; @@ -205,9 +205,9 @@ protected void addInventorySlotGroup(@NotNull ModularUI.Builder builder, int itemSlotsToDown = inputSlotGrid[1]; int startInputsX = isOutputs ? 106 : 70 - itemSlotsToLeft * 18; int startInputsY = 33 - (int) (itemSlotsToDown / 2.0 * 18) + yOffset; - boolean wasGroup = itemHandler.getSlots() + fluidHandler.getTanks() == 12; + boolean wasGroup = itemHandler.getSlots() + fluidHandler.size() == 12; if (wasGroup) startInputsY -= 9; - else if (itemHandler.getSlots() >= 6 && fluidHandler.getTanks() >= 2 && !isOutputs) startInputsY -= 9; + else if (itemHandler.getSlots() >= 6 && fluidHandler.size() >= 2 && !isOutputs) startInputsY -= 9; for (int i = 0; i < itemSlotsToDown; i++) { for (int j = 0; j < itemSlotsToLeft; j++) { int slotIndex = i * itemSlotsToLeft + j; @@ -256,7 +256,7 @@ protected void addSlot(ModularUI.Builder builder, int x, int y, int slotIndex, I getOverlaysForSlot(isOutputs, false, slotIndex == itemHandler.getSlots() - 1))); } else { builder.widget(new TankWidget(fluidHandler.getTankAt(slotIndex), x, y, 18, 18).setAlwaysShowFull(true) - .setBackgroundTexture(getOverlaysForSlot(isOutputs, true, slotIndex == fluidHandler.getTanks() - 1)) + .setBackgroundTexture(getOverlaysForSlot(isOutputs, true, slotIndex == fluidHandler.size() - 1)) .setContainerClicking(true, !isOutputs)); } } diff --git a/src/main/java/gregtech/api/recipes/ui/impl/DistillationTowerUI.java b/src/main/java/gregtech/api/recipes/ui/impl/DistillationTowerUI.java index e11f636fe6a..227914218ad 100644 --- a/src/main/java/gregtech/api/recipes/ui/impl/DistillationTowerUI.java +++ b/src/main/java/gregtech/api/recipes/ui/impl/DistillationTowerUI.java @@ -67,7 +67,7 @@ protected void addInventorySlotGroup(@NotNull ModularUI.Builder builder, @NotNull IItemHandlerModifiable itemHandler, @NotNull FluidTankList fluidHandler, boolean isOutputs, int yOffset) { int itemInputsCount = itemHandler.getSlots(); - int fluidInputsCount = fluidHandler.getTanks(); + int fluidInputsCount = fluidHandler.size(); boolean invertFluids = false; if (itemInputsCount == 0) { int tmp = itemInputsCount; @@ -80,9 +80,9 @@ protected void addInventorySlotGroup(@NotNull ModularUI.Builder builder, int itemSlotsToDown = inputSlotGrid[1]; int startInputsX = isOutputs ? 104 : 68 - itemSlotsToLeft * 18; int startInputsY = 55 - (int) (itemSlotsToDown / 2.0 * 18) + yOffset; - boolean wasGroupOutput = itemHandler.getSlots() + fluidHandler.getTanks() == 12; + boolean wasGroupOutput = itemHandler.getSlots() + fluidHandler.size() == 12; if (wasGroupOutput && isOutputs) startInputsY -= 9; - if (itemHandler.getSlots() == 6 && fluidHandler.getTanks() == 2 && !isOutputs) startInputsY -= 9; + if (itemHandler.getSlots() == 6 && fluidHandler.size() == 2 && !isOutputs) startInputsY -= 9; if (!isOutputs) { addSlot(builder, 40, startInputsY + (itemSlotsToDown - 1) * 18 - 18, 0, itemHandler, fluidHandler, invertFluids, false); diff --git a/src/main/java/gregtech/api/util/GTTransferUtils.java b/src/main/java/gregtech/api/util/GTTransferUtils.java index abac3afb385..bcfea4b8948 100644 --- a/src/main/java/gregtech/api/util/GTTransferUtils.java +++ b/src/main/java/gregtech/api/util/GTTransferUtils.java @@ -1,6 +1,6 @@ package gregtech.api.util; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidActionResult; @@ -148,13 +148,13 @@ public static boolean addItemsToItemHandler(final IItemHandler handler, * @param fluidStacks the items to insert into {@code fluidHandler}. * @return {@code true} if the insertion succeeded, {@code false} otherwise. */ - public static boolean addFluidsToFluidHandler(IMultipleTankHandler fluidHandler, + public static boolean addFluidsToFluidHandler(MultipleTankHandler fluidHandler, boolean simulate, List fluidStacks) { if (simulate) { - OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(fluidHandler); + // OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(fluidHandler); for (FluidStack fluidStack : fluidStacks) { - int inserted = overlayedFluidHandler.insertFluid(fluidStack, fluidStack.amount); + int inserted = fluidHandler.fill(fluidStack, false); if (inserted != fluidStack.amount) { return false; } diff --git a/src/main/java/gregtech/api/util/GTUtility.java b/src/main/java/gregtech/api/util/GTUtility.java index 39b4a6a0d65..b6fc5f55f84 100644 --- a/src/main/java/gregtech/api/util/GTUtility.java +++ b/src/main/java/gregtech/api/util/GTUtility.java @@ -3,7 +3,7 @@ import gregtech.api.GTValues; import gregtech.api.GregTechAPI; import gregtech.api.block.machines.MachineItemBlock; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.cover.CoverDefinition; import gregtech.api.fluids.GTFluid; import gregtech.api.gui.widgets.ProgressWidget; @@ -431,8 +431,8 @@ public int size() { * @return a list of fluidstack linked with given fluid handler * modifications in list will reflect on fluid handler and wise-versa */ - public static List fluidHandlerToList(IMultipleTankHandler fluidInputs) { - List backedList = fluidInputs.getFluidTanks(); + public static List fluidHandlerToList(MultipleTankHandler fluidInputs) { + List backedList = fluidInputs.getFluidTanks(); return new AbstractList() { @Override diff --git a/src/main/java/gregtech/api/util/OverlayedFluidHandler.java b/src/main/java/gregtech/api/util/OverlayedFluidHandler.java index ece86333d65..5240a8c0390 100644 --- a/src/main/java/gregtech/api/util/OverlayedFluidHandler.java +++ b/src/main/java/gregtech/api/util/OverlayedFluidHandler.java @@ -1,11 +1,12 @@ package gregtech.api.util; -import gregtech.api.capability.IMultipleTankHandler; -import gregtech.api.capability.IMultipleTankHandler.MultiFluidTankEntry; +import gregtech.api.capability.MultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler.Entry; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.capability.IFluidTankProperties; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -14,20 +15,20 @@ import java.util.List; /** - * Simulates consecutive fills to {@link IMultipleTankHandler} instance. + * Simulates consecutive fills to {@link MultipleTankHandler} instance. */ +@Deprecated +@ApiStatus.ScheduledForRemoval(inVersion = "2.9") public class OverlayedFluidHandler { private final List overlayedTanks; - public OverlayedFluidHandler(@NotNull IMultipleTankHandler tank) { + public OverlayedFluidHandler(@NotNull MultipleTankHandler tank) { this.overlayedTanks = new ArrayList<>(); - MultiFluidTankEntry[] entries = tank.getFluidTanks().toArray(new MultiFluidTankEntry[0]); - Arrays.sort(entries, IMultipleTankHandler.ENTRY_COMPARATOR); - for (MultiFluidTankEntry fluidTank : entries) { - for (IFluidTankProperties property : fluidTank.getTankProperties()) { - this.overlayedTanks.add(new OverlayedTank(property, fluidTank.allowSameFluidFill())); - } + Entry[] entries = tank.getFluidTanks().toArray(new Entry[0]); + Arrays.sort(entries, MultipleTankHandler.ENTRY_COMPARATOR); + for (Entry fluidTank : entries) { + this.overlayedTanks.add(new OverlayedTank(fluidTank, fluidTank.allowSameFluidFill())); } } diff --git a/src/main/java/gregtech/common/covers/CoverDigitalInterface.java b/src/main/java/gregtech/common/covers/CoverDigitalInterface.java index 0063a113faa..e46930a1de2 100644 --- a/src/main/java/gregtech/common/covers/CoverDigitalInterface.java +++ b/src/main/java/gregtech/common/covers/CoverDigitalInterface.java @@ -321,7 +321,7 @@ public void update() { int maxSlotLimit = Integer.MAX_VALUE; if (this.getCoverableView() instanceof MetaTileEntity metaTileEntity) { maxSlotLimit = this.mode == MODE.ITEM ? metaTileEntity.getImportItems().getSlots() : - metaTileEntity.getImportFluids().getTanks(); + metaTileEntity.getImportFluids().size(); } double x = 0; double y = 1 - rayTraceResult.hitVec.y + rayTraceResult.getBlockPos().getY(); diff --git a/src/main/java/gregtech/common/items/behaviors/TricorderBehavior.java b/src/main/java/gregtech/common/items/behaviors/TricorderBehavior.java index 1045603125a..acc62ae0267 100644 --- a/src/main/java/gregtech/common/items/behaviors/TricorderBehavior.java +++ b/src/main/java/gregtech/common/items/behaviors/TricorderBehavior.java @@ -298,7 +298,7 @@ else if (metaTileEntity instanceof IDataInfoProvider) list.add(new TextComponentTranslation("behavior.tricorder.quantum_controller.connected_items", TextFormatting.RED.toString() + handler.getItemHandlers().getSlots())); list.add(new TextComponentTranslation("behavior.tricorder.quantum_controller.connected_fluids", - TextFormatting.RED.toString() + handler.getFluidTanks().getTanks())); + TextFormatting.RED.toString() + handler.getFluidTanks().size())); } else if (metaTileEntity instanceof IQuantumStoragestorage) { var qcontrollor = storage.getQuantumController(); if (qcontrollor != null) { diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityDistillationTower.java b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityDistillationTower.java index 2c7eea8410a..96d9ebee784 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityDistillationTower.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityDistillationTower.java @@ -1,7 +1,7 @@ package gregtech.common.metatileentities.multi.electric; import gregtech.api.capability.IDistillationTower; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.DistillationTowerLogicHandler; import gregtech.api.capability.impl.MultiblockRecipeLogic; import gregtech.api.metatileentity.MetaTileEntity; @@ -180,7 +180,7 @@ protected void outputRecipeOutputs() { } @Override - protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull IMultipleTankHandler exportFluids) { + protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull MultipleTankHandler exportFluids) { // We have already trimmed fluid outputs at this time if (!metaTileEntity.canVoidRecipeFluidOutputs() && !handler.applyFluidToOutputs(recipe.getAllFluidOutputs(), false)) { @@ -191,7 +191,7 @@ protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull IMulti } @Override - protected IMultipleTankHandler getOutputTank() { + protected MultipleTankHandler getOutputTank() { return handler.getFluidTanks(); } } diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java index dca07a39e5f..4e690124aaa 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java @@ -3,8 +3,8 @@ import gregtech.api.GTValues; import gregtech.api.capability.GregtechTileCapabilities; import gregtech.api.capability.IEnergyContainer; -import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.IWorkable; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.EnergyContainerList; import gregtech.api.capability.impl.FluidDrillLogic; import gregtech.api.capability.impl.FluidTankList; @@ -61,8 +61,8 @@ public class MetaTileEntityFluidDrill extends MultiblockWithDisplayBase private final FluidDrillLogic minerLogic; private final int tier; - protected IMultipleTankHandler inputFluidInventory; - protected IMultipleTankHandler outputFluidInventory; + protected MultipleTankHandler inputFluidInventory; + protected MultipleTankHandler outputFluidInventory; protected IEnergyContainer energyContainer; public MetaTileEntityFluidDrill(ResourceLocation metaTileEntityId, int tier) { diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityLargeMiner.java b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityLargeMiner.java index cc2b78fa6ba..98305ada7e0 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityLargeMiner.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityLargeMiner.java @@ -75,7 +75,7 @@ public class MetaTileEntityLargeMiner extends MultiblockWithDisplayBase private final int tier; private IEnergyContainer energyContainer; - protected IMultipleTankHandler inputFluidInventory; + protected MultipleTankHandler inputFluidInventory; protected IItemHandlerModifiable outputInventory; private boolean silkTouch = false; diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityProcessingArray.java b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityProcessingArray.java index 4759bb1dec7..40371933cc7 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityProcessingArray.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityProcessingArray.java @@ -1,7 +1,7 @@ package gregtech.common.metatileentities.multi.electric; import gregtech.api.GTValues; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.AbstractRecipeLogic; import gregtech.api.capability.impl.MultiblockRecipeLogic; import gregtech.api.metatileentity.IMachineHatchMultiblock; @@ -387,7 +387,7 @@ public int getParallelLimit() { } @Override - protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, IMultipleTankHandler fluidInputs) { + protected Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, MultipleTankHandler fluidInputs) { return super.findRecipe(Math.min(super.getMaxVoltage(), this.machineVoltage), inputs, fluidInputs); } diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java index 46edc9619e7..199e7c20788 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java @@ -1,8 +1,8 @@ package gregtech.common.metatileentities.multi.electric.generator; import gregtech.api.GTValues; -import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.IRotorHolder; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.MultiblockFuelRecipeLogic; import gregtech.api.metatileentity.multiblock.FuelMultiblockController; import gregtech.api.metatileentity.multiblock.MultiblockAbility; @@ -113,7 +113,7 @@ protected boolean checkPreviousRecipe() { @Override protected @Nullable Recipe findRecipe(long maxVoltage, IItemHandlerModifiable inputs, - IMultipleTankHandler fluidInputs) { + MultipleTankHandler fluidInputs) { RecipeMap map = getRecipeMap(); if (map == null || !isRecipeMapValid(map)) { return null; diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/MetaTileEntityLargeCombustionEngine.java b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/MetaTileEntityLargeCombustionEngine.java index 5bdd31eafc8..20e772e3542 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/MetaTileEntityLargeCombustionEngine.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/MetaTileEntityLargeCombustionEngine.java @@ -3,7 +3,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.GregtechCapabilities; import gregtech.api.capability.IEnergyContainer; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.MultiblockFuelRecipeLogic; import gregtech.api.fluids.store.FluidStorageKeys; import gregtech.api.gui.GuiTextures; @@ -364,7 +364,7 @@ protected void updateRecipeProgress() { protected void checkOxygen() { // check oxygen if present to boost production, and if the dynamo hatch supports it if (combustionEngine.isBoostAllowed()) { - IMultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); + MultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); FluidStack boosterStack = isExtreme ? LIQUID_OXYGEN_STACK : OXYGEN_STACK; isOxygenBoosted = boosterStack.isFluidStackIdentical(inputTank.drain(boosterStack, false)); } @@ -379,7 +379,7 @@ protected void drainOxygen() { protected boolean checkLubricant() { // check lubricant and invalidate if it fails - IMultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); + MultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); if (LUBRICANT_STACK.isFluidStackIdentical(inputTank.drain(LUBRICANT_STACK, false))) { return true; } else { @@ -390,7 +390,7 @@ protected boolean checkLubricant() { protected void drainLubricant() { if (totalContinuousRunningTime == 1 || totalContinuousRunningTime % 72 == 0) { - IMultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); + MultipleTankHandler inputTank = combustionEngine.getInputFluidInventory(); inputTank.drain(LUBRICANT_STACK, true); } } diff --git a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityMultiblockNotifiablePart.java b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityMultiblockNotifiablePart.java index 1d3d2be909f..b147a909e7d 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityMultiblockNotifiablePart.java +++ b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityMultiblockNotifiablePart.java @@ -1,7 +1,7 @@ package gregtech.common.metatileentities.multi.multiblockpart; -import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.INotifiableHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.capability.impl.NotifiableItemStackHandler; import gregtech.api.metatileentity.multiblock.MultiblockControllerBase; @@ -55,7 +55,7 @@ private List getPartHandlers() { FluidTankList fluidTankList = getFluidHandlers(); if (fluidTankList != null) { for (IFluidTank fluidTank : fluidTankList) { - if (fluidTank instanceof IMultipleTankHandler.MultiFluidTankEntry entry) { + if (fluidTank instanceof MultipleTankHandler.Entry entry) { fluidTank = entry.getDelegate(); } if (fluidTank instanceof INotifiableHandler) { diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityBuffer.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityBuffer.java index 1ecb31c1821..da4811c0dea 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityBuffer.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityBuffer.java @@ -76,7 +76,7 @@ protected ModularUI createUI(EntityPlayer entityPlayer) { int invTier = tier + 2; ModularUI.Builder builder = ModularUI.builder(GuiTextures.BACKGROUND, 176, Math.max(166, 18 + 18 * invTier + 94));// 176, 166 - for (int i = 0; i < this.fluidTankList.getTanks(); i++) { + for (int i = 0; i < this.fluidTankList.size(); i++) { builder.widget(new TankWidget(this.fluidTankList.getTankAt(i), 176 - 8 - 18, 18 + 18 * i, 18, 18) .setAlwaysShowFull(true) .setBackgroundTexture(GuiTextures.FLUID_SLOT) diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java index c4431e902a9..444b9b5fbf9 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumStorageController.java @@ -492,7 +492,7 @@ private void rebuildCache() { @Override public boolean hasFluidTanks() { - return getFluidTanks().getTanks() > 0; + return getFluidTanks().size() > 0; } @Override diff --git a/src/main/java/gregtech/integration/opencomputers/drivers/DriverRecipeMapMultiblockController.java b/src/main/java/gregtech/integration/opencomputers/drivers/DriverRecipeMapMultiblockController.java index 7db4e9bd232..1a81c354dad 100644 --- a/src/main/java/gregtech/integration/opencomputers/drivers/DriverRecipeMapMultiblockController.java +++ b/src/main/java/gregtech/integration/opencomputers/drivers/DriverRecipeMapMultiblockController.java @@ -1,7 +1,7 @@ package gregtech.integration.opencomputers.drivers; import gregtech.api.capability.GregtechTileCapabilities; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.MultiblockRecipeLogic; import gregtech.api.metatileentity.interfaces.IGregTechTileEntity; import gregtech.api.metatileentity.multiblock.RecipeMapMultiblockController; @@ -126,7 +126,7 @@ public Object[] getOutputInventory(final Context context, final Arguments args) } @NotNull - private Object[] getTank(IMultipleTankHandler handler) { + private Object[] getTank(MultipleTankHandler handler) { List> result = new ArrayList<>(); handler.getFluidTanks().forEach(tank -> { Map map = new Object2ObjectOpenHashMap<>(); diff --git a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java index 7fb6219bfa3..a5307f4e154 100644 --- a/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java +++ b/src/test/java/gregtech/api/capability/impl/FluidTankListTest.java @@ -151,8 +151,8 @@ public void testMultipleFluidFills() { @Test public void testMixedSameFluidFill() { - new FluidHandlerTester(new FluidTankList2(true, - new FluidTankList2(false, + new FluidHandlerTester(new FluidTankList(true, + new FluidTankList(false, new FluidTank(1000), new FluidTank(1000)), new FluidTank(1000), @@ -167,8 +167,8 @@ public void testMixedSameFluidFill() { new FluidStack(WATER, 1000), new FluidStack(WATER, 400)); - new FluidHandlerTester(new FluidTankList2(false, - new FluidTankList2(true, + new FluidHandlerTester(new FluidTankList(false, + new FluidTankList(true, new FluidTank(1000), new FluidTank(1000)), new FluidTank(1000), @@ -310,14 +310,14 @@ public void testFilterOrdering() { private static final class FluidHandlerTester { - private final FluidTankList2 tank; + private final FluidTankList tank; - FluidHandlerTester(FluidTankList2 tank) { + FluidHandlerTester(FluidTankList tank) { this.tank = tank; } FluidHandlerTester(boolean allowSameFluidFill, IFluidTank... tanks) { - this(new FluidTankList2(allowSameFluidFill, tanks)); + this(new FluidTankList(allowSameFluidFill, tanks)); } FluidHandlerTester fill(Fluid fluid, int amount) { diff --git a/src/test/java/gregtech/api/capability/impl/MultiblockRecipeLogicTest.java b/src/test/java/gregtech/api/capability/impl/MultiblockRecipeLogicTest.java index 47c4287aef1..471605c4f68 100644 --- a/src/test/java/gregtech/api/capability/impl/MultiblockRecipeLogicTest.java +++ b/src/test/java/gregtech/api/capability/impl/MultiblockRecipeLogicTest.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.Bootstrap; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.metatileentity.MetaTileEntity; import gregtech.api.metatileentity.MetaTileEntityHolder; import gregtech.api.metatileentity.multiblock.MultiblockAbility; @@ -216,12 +216,12 @@ protected IItemHandlerModifiable getOutputInventory() { } @Override - protected IMultipleTankHandler getInputTank() { + protected MultipleTankHandler getInputTank() { return importFluidBus.getImportFluids(); } @Override - protected IMultipleTankHandler getOutputTank() { + protected MultipleTankHandler getOutputTank() { return importFluidBus.getExportFluids(); } }; @@ -463,12 +463,12 @@ protected IItemHandlerModifiable getOutputInventory() { } @Override - protected IMultipleTankHandler getInputTank() { + protected MultipleTankHandler getInputTank() { return importFluidBus.getImportFluids(); } @Override - protected IMultipleTankHandler getOutputTank() { + protected MultipleTankHandler getOutputTank() { return importFluidBus.getExportFluids(); } @@ -690,12 +690,12 @@ protected IItemHandlerModifiable getOutputInventory() { } @Override - protected IMultipleTankHandler getInputTank() { + protected MultipleTankHandler getInputTank() { return new FluidTankList(false); } @Override - protected IMultipleTankHandler getOutputTank() { + protected MultipleTankHandler getOutputTank() { return new FluidTankList(false); } diff --git a/src/test/java/gregtech/api/recipes/logic/IParallelableRecipeLogicTest.java b/src/test/java/gregtech/api/recipes/logic/IParallelableRecipeLogicTest.java index 882d9287e6a..bd8a49db015 100644 --- a/src/test/java/gregtech/api/recipes/logic/IParallelableRecipeLogicTest.java +++ b/src/test/java/gregtech/api/recipes/logic/IParallelableRecipeLogicTest.java @@ -1,7 +1,7 @@ package gregtech.api.recipes.logic; import gregtech.Bootstrap; -import gregtech.api.capability.IMultipleTankHandler; +import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; import gregtech.api.capability.impl.MultiblockRecipeLogic; import gregtech.api.metatileentity.MetaTileEntity; @@ -335,7 +335,7 @@ public void findMultipliedRecipe_FluidOnlyMaxParallelTest() { importFluidBus.getImportFluids().fill(Materials.Toluene.getFluid(4000), true); secondImportFluidBus.getImportFluids().fill(Materials.RawGasoline.getFluid(50000), true); - IMultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), + MultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), secondImportFluidBus.getImportFluids().getTankAt(0)); RecipeBuilder parallelRecipe = logic.findMultipliedParallelRecipe(map, recipe, @@ -388,7 +388,7 @@ public void findMultipliedRecipe_FluidOnlyLessThanMaxParallelTest() { importFluidBus.getImportFluids().fill(Materials.Toluene.getFluid(2000), true); secondImportFluidBus.getImportFluids().fill(Materials.RawGasoline.getFluid(50000), true); - IMultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), + MultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), secondImportFluidBus.getImportFluids().getTankAt(0)); RecipeBuilder parallelRecipe = logic.findMultipliedParallelRecipe(map, recipe, diff --git a/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java b/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java index 4c704c29ca6..256512311a4 100644 --- a/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java +++ b/src/test/java/gregtech/api/recipes/logic/ParallelLogicTest.java @@ -2,10 +2,8 @@ import gregtech.Bootstrap; import gregtech.api.GTValues; -import gregtech.api.capability.IMultipleTankHandler; import gregtech.api.capability.MultipleTankHandler; import gregtech.api.capability.impl.FluidTankList; -import gregtech.api.capability.impl.FluidTankList2; import gregtech.api.recipes.Recipe; import gregtech.api.recipes.RecipeBuilder; import gregtech.api.recipes.RecipeMap; @@ -13,7 +11,6 @@ import gregtech.api.recipes.builders.BlastRecipeBuilder; import gregtech.api.unification.material.Materials; import gregtech.api.util.GTHashMaps; -import gregtech.api.util.OverlayedFluidHandler; import gregtech.api.util.OverlayedItemHandler; import gregtech.common.metatileentities.MetaTileEntities; import gregtech.common.metatileentities.electric.SimpleMachineMetaTileEntityResizable; @@ -33,6 +30,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.notNullValue; +@SuppressWarnings("DataFlowIssue") public class ParallelLogicTest { MetaTileEntityItemBus importItemBus = new MetaTileEntityItemBus(gregtechId("item_bus.export.lv"), 1, false); @@ -366,7 +364,7 @@ public void limitParallelByFluids_MaxParallelTest() { importItemBus.getImportItems().insertItem(0, new ItemStack(Blocks.COBBLESTONE, 16), false); int itemRatio = ParallelLogic.limitParallelByFluids(recipe, - new OverlayedFluidHandler(exportFluidBus.getExportFluids()), parallelLimit); + exportFluidBus.getExportFluids(), parallelLimit); assertThat(itemRatio, is(4)); } @@ -397,7 +395,7 @@ public void limitParallelByFluids_PartialParallelsTest() { exportFluidBus.getExportFluids().fill(Materials.Acetone.getFluid(15800), true); int itemRatio = ParallelLogic.limitParallelByFluids(recipe, - new OverlayedFluidHandler(exportFluidBus.getExportFluids()), parallelLimit); + exportFluidBus.getExportFluids(), parallelLimit); assertThat(itemRatio, is(2)); } @@ -428,7 +426,7 @@ public void limitParallelByFluids_FluidOutputFullTest() { exportFluidBus.getExportFluids().fill(Materials.Acetone.getFluid(16000), true); int itemRatio = ParallelLogic.limitParallelByFluids(recipe, - new OverlayedFluidHandler(exportFluidBus.getExportFluids()), parallelLimit); + exportFluidBus.getExportFluids(), parallelLimit); assertThat(itemRatio, is(0)); } @@ -728,7 +726,7 @@ public void getMaxRatioFluid_DifferentNonConsumedTest() { importFluidBus.getImportFluids().fill(Materials.Water.getFluid(1000), true); secondImportFluidBus.getImportFluids().fill(Materials.Acetone.getFluid(1), true); - MultipleTankHandler tankHandler = new FluidTankList2(false, importFluidBus.getImportFluids().getTankAt(0), + MultipleTankHandler tankHandler = new FluidTankList(false, importFluidBus.getImportFluids().getTankAt(0), secondImportFluidBus.getImportFluids().getTankAt(0)); int fluidRatio = ParallelLogic.getMaxRatioFluid(GTHashMaps.fromFluidHandler(tankHandler), From f8fd4aaacae13e946dd06c19d5f85222f796fb8c Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:29:27 -0700 Subject: [PATCH 16/26] do tank instead of handler why check for handler --- .../java/gregtech/api/gui/widgets/TankWidget.java | 4 ---- .../gregtech/api/metatileentity/MetaTileEntity.java | 13 +++++++------ .../multiblock/MultiblockWithDisplayBase.java | 6 +----- .../generator/LargeTurbineWorkableHandler.java | 7 ++----- 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/java/gregtech/api/gui/widgets/TankWidget.java b/src/main/java/gregtech/api/gui/widgets/TankWidget.java index 44e04a2768d..c915ec8c875 100644 --- a/src/main/java/gregtech/api/gui/widgets/TankWidget.java +++ b/src/main/java/gregtech/api/gui/widgets/TankWidget.java @@ -25,7 +25,6 @@ import net.minecraft.util.text.TextFormatting; import net.minecraftforge.fluids.*; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; -import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandlerItem; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -102,9 +101,6 @@ public TankWidget setFluidRenderOffset(int fluidRenderOffset) { } public TankWidget setContainerClicking(boolean allowClickContainerFilling, boolean allowClickContainerEmptying) { - if (!(fluidTank instanceof IFluidHandler)) - throw new IllegalStateException( - "Container IO is only supported for fluid tanks that implement IFluidHandler"); this.allowClickFilling = allowClickContainerFilling; this.allowClickEmptying = allowClickContainerEmptying; return this; diff --git a/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java b/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java index b0648610103..71877e85cae 100644 --- a/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java +++ b/src/main/java/gregtech/api/metatileentity/MetaTileEntity.java @@ -71,6 +71,7 @@ import net.minecraftforge.common.util.Constants.NBT; import net.minecraftforge.fluids.FluidActionResult; import net.minecraftforge.fluids.FluidUtil; +import net.minecraftforge.fluids.IFluidTank; import net.minecraftforge.fluids.capability.CapabilityFluidHandler; import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fml.common.FMLCommonHandler; @@ -155,8 +156,8 @@ public abstract class MetaTileEntity implements ISyncedTileEntity, CoverHolder, protected List notifiedItemOutputList = new ArrayList<>(); protected List notifiedItemInputList = new ArrayList<>(); - protected List notifiedFluidInputList = new ArrayList<>(); - protected List notifiedFluidOutputList = new ArrayList<>(); + protected List notifiedFluidInputList = new ArrayList<>(); + protected List notifiedFluidOutputList = new ArrayList<>(); protected boolean muffled = false; @@ -386,9 +387,9 @@ public void addNotifiedInput(T input) { if (!notifiedItemInputList.contains(input)) { this.notifiedItemInputList.add((IItemHandlerModifiable) input); } - } else if (input instanceof IFluidHandler) { + } else if (input instanceof IFluidTank) { if (!notifiedFluidInputList.contains(input)) { - this.notifiedFluidInputList.add((IFluidHandler) input); + this.notifiedFluidInputList.add((IFluidTank) input); } } } @@ -1462,11 +1463,11 @@ public List getNotifiedItemInputList() { return notifiedItemInputList; } - public List getNotifiedFluidInputList() { + public List getNotifiedFluidInputList() { return notifiedFluidInputList; } - public List getNotifiedFluidOutputList() { + public List getNotifiedFluidOutputList() { return notifiedFluidOutputList; } diff --git a/src/main/java/gregtech/api/metatileentity/multiblock/MultiblockWithDisplayBase.java b/src/main/java/gregtech/api/metatileentity/multiblock/MultiblockWithDisplayBase.java index 55723ef456d..8620bc55774 100644 --- a/src/main/java/gregtech/api/metatileentity/multiblock/MultiblockWithDisplayBase.java +++ b/src/main/java/gregtech/api/metatileentity/multiblock/MultiblockWithDisplayBase.java @@ -28,8 +28,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -543,9 +541,7 @@ protected void setVoidingMode(int mode) { // After changing the voiding mode, reset the notified buses in case a recipe can run now that voiding mode has // been changed - for (IFluidTank tank : this.getAbilities(MultiblockAbility.IMPORT_FLUIDS)) { - this.getNotifiedFluidInputList().add((IFluidHandler) tank); - } + this.getNotifiedFluidInputList().addAll(this.getAbilities(MultiblockAbility.IMPORT_FLUIDS)); this.getNotifiedItemInputList().addAll(this.getAbilities(MultiblockAbility.IMPORT_ITEMS)); markDirty(); diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java index 199e7c20788..66b5811b53d 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/LargeTurbineWorkableHandler.java @@ -16,7 +16,6 @@ import net.minecraft.util.math.MathHelper; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.IFluidTank; -import net.minecraftforge.fluids.capability.IFluidHandler; import net.minecraftforge.items.IItemHandlerModifiable; import org.jetbrains.annotations.NotNull; @@ -183,9 +182,7 @@ public void invalidate() { public void updateTanks() { FuelMultiblockController controller = (FuelMultiblockController) this.metaTileEntity; - List tanks = controller.getNotifiedFluidInputList(); - for (IFluidTank tank : controller.getAbilities(MultiblockAbility.IMPORT_FLUIDS)) { - tanks.add((IFluidHandler) tank); - } + List tanks = controller.getNotifiedFluidInputList(); + tanks.addAll(controller.getAbilities(MultiblockAbility.IMPORT_FLUIDS)); } } From b57cee8b350664736e4ec6aa820113b3068f89f2 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:32:36 -0700 Subject: [PATCH 17/26] simplify constructor slightly comments --- .../java/gregtech/api/capability/MultipleTankHandler.java | 2 +- .../java/gregtech/api/capability/impl/FluidTankList.java | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 87e223a1c90..6b39b82768c 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -171,7 +171,7 @@ public boolean canDrain() { public boolean canFillFluidType(FluidStack fluidStack) { if (allowSameFluidFill() || fluidStack == null) return true; for (Entry tank : getParentHandler()) { - // only consider tanks that do not allow same fluid fill + // only consider other tanks that do not allow same fluid fill if (tank.allowSameFluidFill() || this == tank) continue; if (fluidStack.isFluidEqual(tank.getFluid())) { return tank.getFluidAmount() + fluidStack.amount <= tank.getCapacity(); diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList.java b/src/main/java/gregtech/api/capability/impl/FluidTankList.java index b8643b4c630..770be604bef 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList.java @@ -37,10 +37,7 @@ public FluidTankList(boolean allowSameFluidFill, @NotNull List Date: Thu, 26 Dec 2024 22:55:17 -0700 Subject: [PATCH 18/26] move methods --- .../api/capability/impl/FluidTankList.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList.java b/src/main/java/gregtech/api/capability/impl/FluidTankList.java index 770be604bef..58739e93fee 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList.java @@ -49,6 +49,31 @@ public FluidTankList(boolean allowSameFluidFill, @NotNull MultipleTankHandler pa this.allowSameFluidFill = allowSameFluidFill; } + @Override + public IFluidTankProperties[] getTankProperties() { + return this.tanks; + } + + @Override + public @NotNull List getFluidTanks() { + return Collections.unmodifiableList(Arrays.asList(this.tanks)); + } + + @Override + public int size() { + return tanks.length; + } + + @Override + public @NotNull Entry getTankAt(int index) { + return tanks[index]; + } + + @Override + public boolean allowSameFluidFill() { + return allowSameFluidFill; + } + @Override public int fill(FluidStack resource, boolean doFill) { if (resource == null || resource.amount <= 0) @@ -174,31 +199,6 @@ public void deserializeNBT(NBTTagCompound nbt) { } } - @Override - public IFluidTankProperties[] getTankProperties() { - return this.tanks; - } - - @Override - public @NotNull List getFluidTanks() { - return Collections.unmodifiableList(Arrays.asList(this.tanks)); - } - - @Override - public int size() { - return tanks.length; - } - - @Override - public @NotNull Entry getTankAt(int index) { - return tanks[index]; - } - - @Override - public boolean allowSameFluidFill() { - return allowSameFluidFill; - } - @Override public String toString() { return toString(false); From 5309d4732fc9a5987fbfb6816fdb75eaf20e30a9 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:51:35 -0700 Subject: [PATCH 19/26] add filtering to base handler classes rename and rework IFilteredHandler for item handlers as well deprecate some classes --- ...idContainer.java => IFilteredHandler.java} | 14 +++++-- .../api/capability/MultipleTankHandler.java | 4 +- .../capability/impl/CommonFluidFilters.java | 36 +++++++++------- .../capability/impl/FilteredFluidHandler.java | 5 ++- .../capability/impl/FilteredItemHandler.java | 22 ++++++---- .../impl/GTFluidHandlerItemStack.java | 4 +- .../impl/GTSimpleFluidHandlerItemStack.java | 5 ++- .../impl/NotifiableFilteredFluidHandler.java | 34 ++------------- .../capability/impl/NotifiableFluidTank.java | 32 +++++++++++++- .../impl/NotifiableItemStackHandler.java | 5 +-- .../capability/impl/SingleFluidFilter.java | 2 +- .../itemhandlers/GTItemStackHandler.java | 31 +++++++++++++- .../gregtech/api/items/metaitem/MetaItem.java | 4 +- .../MetaTileEntityFluidHatch.java | 42 ++++++++----------- .../storage/MetaTileEntityQuantumTank.java | 4 +- 15 files changed, 142 insertions(+), 102 deletions(-) rename src/main/java/gregtech/api/capability/{IFilteredFluidContainer.java => IFilteredHandler.java} (58%) diff --git a/src/main/java/gregtech/api/capability/IFilteredFluidContainer.java b/src/main/java/gregtech/api/capability/IFilteredHandler.java similarity index 58% rename from src/main/java/gregtech/api/capability/IFilteredFluidContainer.java rename to src/main/java/gregtech/api/capability/IFilteredHandler.java index c1920fda635..b0787c18fe6 100644 --- a/src/main/java/gregtech/api/capability/IFilteredFluidContainer.java +++ b/src/main/java/gregtech/api/capability/IFilteredHandler.java @@ -1,5 +1,6 @@ package gregtech.api.capability; +import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; import org.jetbrains.annotations.Nullable; @@ -10,18 +11,23 @@ * Interface for fluid containers ({@link net.minecraftforge.fluids.IFluidTank IFluidTank} or * {@link net.minecraftforge.fluids.capability.IFluidHandler IFluidHandler}) associated with {@link IFilter}. */ -public interface IFilteredFluidContainer { +public interface IFilteredHandler { /** * Compare logic for filtered instances. */ - Comparator COMPARATOR = Comparator.nullsLast( - Comparator.comparing(IFilteredFluidContainer::getFilter, IFilter.FILTER_COMPARATOR)); + Comparator> COMPARATOR = Comparator.nullsLast( + Comparator.comparing(IFilteredHandler::getFilter, IFilter.FILTER_COMPARATOR)); /** * @return instance of {@link IFilter} associated to this object, or {@code null} if there's no filter * associated. */ @Nullable - IFilter getFilter(); + IFilter getFilter(); + + // for type safe casting + interface FluidHandler extends IFilteredHandler {} + + interface ItemHandler extends IFilteredHandler {} } diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 6b39b82768c..b6f6e133f06 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -102,7 +102,7 @@ public MultipleTankHandler addTanks(IFluidTank... tanks) { * Entry of multi fluid tanks. Retains reference to original {@link MultipleTankHandler} for accessing * information such as {@link MultipleTankHandler#allowSameFluidFill()}. */ - public static class Entry implements IFluidTank, IFilteredFluidContainer, INBTSerializable, + public static class Entry implements IFluidTank, IFilteredHandler, INBTSerializable, IFluidTankProperties { private final IFluidTank tank; @@ -128,7 +128,7 @@ public boolean allowSameFluidFill() { @Nullable @Override public IFilter getFilter() { - return getDelegate() instanceof IFilteredFluidContainer filtered ? filtered.getFilter() : null; + return getDelegate() instanceof IFilteredHandler.FluidHandler filter ? filter.getFilter() : null; } @Nullable diff --git a/src/main/java/gregtech/api/capability/impl/CommonFluidFilters.java b/src/main/java/gregtech/api/capability/impl/CommonFluidFilters.java index bca26ca9f25..38f907084d3 100644 --- a/src/main/java/gregtech/api/capability/impl/CommonFluidFilters.java +++ b/src/main/java/gregtech/api/capability/impl/CommonFluidFilters.java @@ -11,12 +11,14 @@ import org.jetbrains.annotations.NotNull; +import static gregtech.api.capability.IFilter.*; + /** * Common fluid filter implementations. */ -public enum CommonFluidFilters implements IFilter { +public class CommonFluidFilters { - ALLOW_ALL { + public static final IFilter ALLOW_ALL = new IFilter<>() { @Override public boolean test(@NotNull FluidStack fluid) { @@ -29,11 +31,12 @@ public int getPriority() { } @Override - public IFilter negate() { + public @NotNull IFilter negate() { return DISALLOW_ALL; } - }, - DISALLOW_ALL { + }; + + public static final IFilter DISALLOW_ALL = new IFilter<>() { @Override public boolean test(@NotNull FluidStack fluid) { @@ -46,11 +49,12 @@ public int getPriority() { } @Override - public IFilter negate() { + public @NotNull IFilter negate() { return ALLOW_ALL; } - }, - BOILER_FLUID { + }; + + public static final IFilter BOILER_FLUID = new IFilter<>() { @Override public boolean test(@NotNull FluidStack fluid) { @@ -69,10 +73,11 @@ public boolean test(@NotNull FluidStack fluid) { @Override public int getPriority() { - return IFilter.whitelistLikePriority(); + return whitelistLikePriority(); } - }, - STEAM { + }; + + public static final IFilter STEAM = new IFilter<>() { @Override public boolean test(@NotNull FluidStack fluid) { @@ -81,10 +86,11 @@ public boolean test(@NotNull FluidStack fluid) { @Override public int getPriority() { - return IFilter.whitelistPriority(1); + return whitelistPriority(1); } - }, - LIGHTER_FUEL { + }; + + public static final IFilter LIGHTER_FUEL = new IFilter<>() { @Override public boolean test(@NotNull FluidStack fluidStack) { @@ -93,7 +99,7 @@ public boolean test(@NotNull FluidStack fluidStack) { @Override public int getPriority() { - return IFilter.whitelistPriority(2); + return whitelistPriority(2); } }; diff --git a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java index 7d6e2f6d811..62e6800e96f 100644 --- a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java +++ b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IFilteredHandler; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; @@ -10,7 +10,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class FilteredFluidHandler extends FluidTank implements IFilteredFluidContainer { +@Deprecated +public class FilteredFluidHandler extends FluidTank implements IFilteredHandler.FluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/FilteredItemHandler.java b/src/main/java/gregtech/api/capability/impl/FilteredItemHandler.java index 63e3131365e..0e5da9afca8 100644 --- a/src/main/java/gregtech/api/capability/impl/FilteredItemHandler.java +++ b/src/main/java/gregtech/api/capability/impl/FilteredItemHandler.java @@ -1,5 +1,6 @@ package gregtech.api.capability.impl; +import gregtech.api.capability.IFilter; import gregtech.api.items.itemhandlers.GTItemStackHandler; import gregtech.api.metatileentity.MetaTileEntity; @@ -11,14 +12,13 @@ import java.util.function.Predicate; +@Deprecated public class FilteredItemHandler extends GTItemStackHandler { public static Predicate getCapabilityFilter(Capability cap) { return stack -> stack.hasCapability(cap, null); } - private Predicate fillPredicate; - public FilteredItemHandler(MetaTileEntity metaTileEntity) { super(metaTileEntity, 1); } @@ -32,12 +32,18 @@ public FilteredItemHandler(MetaTileEntity metaTileEntity, NonNullList } public FilteredItemHandler setFillPredicate(Predicate fillPredicate) { - this.fillPredicate = fillPredicate; + setFilter(new IFilter<>() { + + @Override + public boolean test(@NotNull ItemStack stack) { + return fillPredicate.test(stack); + } + + @Override + public int getPriority() { + return IFilter.noPriority(); + } + }); return this; } - - @Override - public boolean isItemValid(int slot, @NotNull ItemStack stack) { - return fillPredicate == null || fillPredicate.test(stack); - } } diff --git a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java index 8e1d72cda81..d0f79c3425a 100644 --- a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IFilteredHandler; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredFluidContainer { +public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredHandler.FluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java index 8e1116ceb5b..98c0be49d7b 100644 --- a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IFilteredHandler; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple implements IFilteredFluidContainer { +public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple + implements IFilteredHandler.FluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/NotifiableFilteredFluidHandler.java b/src/main/java/gregtech/api/capability/impl/NotifiableFilteredFluidHandler.java index fe44ad8db4a..78b69fbee7f 100644 --- a/src/main/java/gregtech/api/capability/impl/NotifiableFilteredFluidHandler.java +++ b/src/main/java/gregtech/api/capability/impl/NotifiableFilteredFluidHandler.java @@ -1,39 +1,11 @@ package gregtech.api.capability.impl; -import gregtech.api.capability.INotifiableHandler; import gregtech.api.metatileentity.MetaTileEntity; -import java.util.ArrayList; -import java.util.List; - -public class NotifiableFilteredFluidHandler extends FilteredFluidHandler implements INotifiableHandler { - - List notifiableEntities = new ArrayList<>(); - private final boolean isExport; +@Deprecated +public class NotifiableFilteredFluidHandler extends NotifiableFluidTank { public NotifiableFilteredFluidHandler(int capacity, MetaTileEntity entityToNotify, boolean isExport) { - super(capacity); - this.notifiableEntities.add(entityToNotify); - this.isExport = isExport; - } - - @Override - protected void onContentsChanged() { - super.onContentsChanged(); - for (MetaTileEntity metaTileEntity : notifiableEntities) { - if (metaTileEntity != null && metaTileEntity.isValid()) { - addToNotifiedList(metaTileEntity, this, isExport); - } - } - } - - @Override - public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { - this.notifiableEntities.add(metaTileEntity); - } - - @Override - public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { - this.notifiableEntities.remove(metaTileEntity); + super(capacity, entityToNotify, isExport); } } diff --git a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java index 2669fa35097..c941f54fcfa 100644 --- a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java +++ b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java @@ -1,17 +1,24 @@ package gregtech.api.capability.impl; +import gregtech.api.capability.IFilter; +import gregtech.api.capability.IFilteredHandler; import gregtech.api.capability.INotifiableHandler; import gregtech.api.metatileentity.MetaTileEntity; +import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.FluidTank; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import java.util.ArrayList; import java.util.List; -public class NotifiableFluidTank extends FluidTank implements INotifiableHandler { +public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredHandler.FluidHandler { List notifiableEntities = new ArrayList<>(); private final boolean isExport; + private IFilter filter; public NotifiableFluidTank(int capacity, MetaTileEntity entityToNotify, boolean isExport) { super(capacity); @@ -38,4 +45,27 @@ public void addNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { public void removeNotifiableMetaTileEntity(MetaTileEntity metaTileEntity) { this.notifiableEntities.remove(metaTileEntity); } + + @Nullable + @Override + public IFilter getFilter() { + return this.filter; + } + + /** + * Set filter instance. If {@code null} is given, then the filter is set to be + * + * @param filter new filter instance + * @return this + */ + @NotNull + public NotifiableFluidTank setFilter(@Nullable IFilter filter) { + this.filter = filter; + return this; + } + + @Override + public boolean canFillFluidType(FluidStack fluid) { + return canFill() && (this.filter == null || this.filter.test(fluid)); + } } diff --git a/src/main/java/gregtech/api/capability/impl/NotifiableItemStackHandler.java b/src/main/java/gregtech/api/capability/impl/NotifiableItemStackHandler.java index 472c29e033f..ec6a44ba6a6 100644 --- a/src/main/java/gregtech/api/capability/impl/NotifiableItemStackHandler.java +++ b/src/main/java/gregtech/api/capability/impl/NotifiableItemStackHandler.java @@ -4,13 +4,10 @@ import gregtech.api.items.itemhandlers.GTItemStackHandler; import gregtech.api.metatileentity.MetaTileEntity; -import net.minecraftforge.items.IItemHandlerModifiable; - import java.util.ArrayList; import java.util.List; -public class NotifiableItemStackHandler extends GTItemStackHandler - implements IItemHandlerModifiable, INotifiableHandler { +public class NotifiableItemStackHandler extends GTItemStackHandler implements INotifiableHandler { List notifiableEntities = new ArrayList<>(); private final boolean isExport; diff --git a/src/main/java/gregtech/api/capability/impl/SingleFluidFilter.java b/src/main/java/gregtech/api/capability/impl/SingleFluidFilter.java index 51c1d8eb682..1f164f9353c 100644 --- a/src/main/java/gregtech/api/capability/impl/SingleFluidFilter.java +++ b/src/main/java/gregtech/api/capability/impl/SingleFluidFilter.java @@ -43,7 +43,7 @@ public int getPriority() { } @Override - public IFilter negate() { + public @NotNull IFilter negate() { return new SingleFluidFilter(this.fluid, !this.blacklist); } diff --git a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java index eab9612c747..d33126a32bb 100644 --- a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java +++ b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java @@ -1,14 +1,20 @@ package gregtech.api.items.itemhandlers; +import gregtech.api.capability.IFilter; +import gregtech.api.capability.IFilteredHandler; import gregtech.api.metatileentity.MetaTileEntity; import net.minecraft.item.ItemStack; import net.minecraft.util.NonNullList; import net.minecraftforge.items.ItemStackHandler; -public class GTItemStackHandler extends ItemStackHandler { +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; - final private MetaTileEntity metaTileEntity; +public class GTItemStackHandler extends ItemStackHandler implements IFilteredHandler.ItemHandler { + + private final MetaTileEntity metaTileEntity; + private IFilter filter; public GTItemStackHandler(MetaTileEntity metaTileEntity) { super(); @@ -30,4 +36,25 @@ public void onContentsChanged(int slot) { super.onContentsChanged(slot); metaTileEntity.markDirty(); } + + @Override + public ItemStack insertItem(int slot, ItemStack stack, boolean simulate) { + if (!isItemValid(slot, stack)) return stack; + return super.insertItem(slot, stack, simulate); + } + + @Override + public boolean isItemValid(int slot, @NotNull ItemStack stack) { + return filter == null || filter.test(stack); + } + + public GTItemStackHandler setFilter(IFilter filter) { + this.filter = filter; + return this; + } + + @Override + public @Nullable IFilter getFilter() { + return null; + } } diff --git a/src/main/java/gregtech/api/items/metaitem/MetaItem.java b/src/main/java/gregtech/api/items/metaitem/MetaItem.java index a17559b05b2..7bc7bf58f2d 100644 --- a/src/main/java/gregtech/api/items/metaitem/MetaItem.java +++ b/src/main/java/gregtech/api/items/metaitem/MetaItem.java @@ -3,7 +3,7 @@ import gregtech.api.GTValues; import gregtech.api.capability.GregtechCapabilities; import gregtech.api.capability.IElectricItem; -import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IFilteredHandler; import gregtech.api.capability.IPropertyFluidFilter; import gregtech.api.capability.impl.CombinedCapabilityProvider; import gregtech.api.capability.impl.ElectricItem; @@ -639,7 +639,7 @@ public void addInformation(@NotNull ItemStack itemStack, @Nullable World worldIn fluidTankProperties.getCapacity(), fluid == null ? "" : fluid.getLocalizedName())); - if (fluidHandler instanceof IFilteredFluidContainer filtered && + if (fluidHandler instanceof IFilteredHandler.FluidHandler filtered && filtered.getFilter() instanceof IPropertyFluidFilter propertyFilter) { propertyFilter.appendTooltips(lines, false, true); } diff --git a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java index 8cf7c4f2e6a..2b5e876cf50 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java +++ b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java @@ -339,10 +339,27 @@ private void setLocked(boolean locked) { fluidTank.onContentsChanged(); } - protected class HatchFluidTank extends NotifiableFluidTank implements IFilteredFluidContainer, IFilter { + // todo remove + @Deprecated + protected class HatchFluidTank extends NotifiableFluidTank { public HatchFluidTank(int capacity, MetaTileEntity entityToNotify, boolean isExport) { super(capacity, entityToNotify, isExport); + setFilter(new IFilter<>() { + + @Override + public boolean test(@NotNull FluidStack fluidStack) { + if (!isExportHatch) return true; + return !locked || lockedFluid == null || fluidStack.isFluidEqual(lockedFluid); + } + + @Override + public int getPriority() { + if (!isExportHatch) return IFilter.noPriority(); + return !locked || lockedFluid == null ? IFilter.noPriority() : + IFilter.whitelistPriority(1); + } + }); } @Override @@ -356,33 +373,10 @@ public int fillInternal(FluidStack resource, boolean doFill) { return accepted; } - @Override - public boolean canFillFluidType(FluidStack fluid) { - return test(fluid); - } - // override for visibility @Override public void onContentsChanged() { super.onContentsChanged(); } - - @Nullable - @Override - public IFilter getFilter() { - return this; - } - - @Override - public boolean test(@NotNull FluidStack fluidStack) { - if (!isExportHatch) return true; - return !locked || lockedFluid == null || fluidStack.isFluidEqual(lockedFluid); - } - - @Override - public int getPriority() { - if (!isExportHatch) return IFilter.noPriority(); - return !locked || lockedFluid == null ? IFilter.noPriority() : IFilter.whitelistPriority(1); - } } } diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java index 0d675b9b1c9..66fc832792f 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java @@ -3,7 +3,7 @@ import gregtech.api.capability.GregtechTileCapabilities; import gregtech.api.capability.IActiveOutputSide; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidContainer; +import gregtech.api.capability.IFilteredHandler; import gregtech.api.capability.impl.FilteredItemHandler; import gregtech.api.capability.impl.FluidHandlerProxy; import gregtech.api.capability.impl.FluidTankList; @@ -676,7 +676,7 @@ public int getLightOpacity() { return 0; } - private class QuantumFluidTank extends FluidTank implements IFilteredFluidContainer, IFilter { + private class QuantumFluidTank extends FluidTank implements IFilteredHandler.FluidHandler, IFilter { public QuantumFluidTank(int capacity) { super(capacity); From bc86ecdf9cc8a7f9b9e8aa7acf0fa770ee884ab4 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 00:37:53 -0700 Subject: [PATCH 20/26] reorder methods again --- .../gregtech/api/capability/impl/FluidTankList.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList.java b/src/main/java/gregtech/api/capability/impl/FluidTankList.java index 58739e93fee..6300105abbc 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList.java @@ -49,11 +49,6 @@ public FluidTankList(boolean allowSameFluidFill, @NotNull MultipleTankHandler pa this.allowSameFluidFill = allowSameFluidFill; } - @Override - public IFluidTankProperties[] getTankProperties() { - return this.tanks; - } - @Override public @NotNull List getFluidTanks() { return Collections.unmodifiableList(Arrays.asList(this.tanks)); @@ -69,6 +64,11 @@ public int size() { return tanks[index]; } + @Override + public IFluidTankProperties[] getTankProperties() { + return this.tanks; + } + @Override public boolean allowSameFluidFill() { return allowSameFluidFill; From c553531353d426b781536afa30700094909cc81b Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 01:07:23 -0700 Subject: [PATCH 21/26] fix test --- .../java/gregtech/api/capability/MultipleTankHandler.java | 6 ++++++ src/main/java/gregtech/api/recipes/logic/ParallelLogic.java | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index b6f6e133f06..55da71d8b3e 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -84,6 +84,12 @@ public final int getIndexOfFluid(@Nullable FluidStack fluidStack) { return -1; } + public int simulateFill(FluidStack stack, int amount) { + var insertable = stack.copy(); + insertable.amount = amount; + return fill(insertable, false); + } + @Override public final @NotNull Iterator iterator() { return getFluidTanks().iterator(); diff --git a/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java b/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java index 1381086cefe..75189b98576 100644 --- a/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java +++ b/src/main/java/gregtech/api/recipes/logic/ParallelLogic.java @@ -257,7 +257,7 @@ public static int limitParallelByItemsIncremental(@NotNull List recip * voiding products. */ public static int limitParallelByFluids(@NotNull Recipe recipe, - @NotNull MultipleTankHandler overlayedFluidHandler, int multiplier) { + @NotNull MultipleTankHandler tankHandler, int multiplier) { int minMultiplier = 0; int maxMultiplier = multiplier; @@ -273,7 +273,7 @@ public static int limitParallelByFluids(@NotNull Recipe recipe, } else { amountLeft = fluidStack.amount * multiplier; } - int inserted = overlayedFluidHandler.fill(fluidStack, false); + int inserted = tankHandler.simulateFill(fluidStack, amountLeft); if (inserted > 0) { amountLeft -= inserted; } From c84caa7cc1f1e5b3f7dce032c7fcb7d56980f6f7 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 01:12:22 -0700 Subject: [PATCH 22/26] remove unneeded method reorder parameters --- .../capability/impl/AbstractRecipeLogic.java | 4 +- .../gregtech/api/util/GTTransferUtils.java | 45 +++---------------- .../electric/MetaTileEntityFluidDrill.java | 4 +- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java index 57f0b7b20e9..632ae451b32 100644 --- a/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/AbstractRecipeLogic.java @@ -734,7 +734,7 @@ protected boolean checkOutputSpaceItems(@NotNull Recipe recipe, @NotNull IItemHa protected boolean checkOutputSpaceFluids(@NotNull Recipe recipe, @NotNull MultipleTankHandler exportFluids) { // We have already trimmed fluid outputs at this time if (!metaTileEntity.canVoidRecipeFluidOutputs() && - !GTTransferUtils.addFluidsToFluidHandler(exportFluids, true, recipe.getAllFluidOutputs())) { + !GTTransferUtils.addFluidsToFluidHandler(recipe.getAllFluidOutputs(), exportFluids, true)) { this.isOutputsFull = true; return false; } @@ -982,7 +982,7 @@ protected void completeRecipe() { */ protected void outputRecipeOutputs() { GTTransferUtils.addItemsToItemHandler(getOutputInventory(), false, itemOutputs); - GTTransferUtils.addFluidsToFluidHandler(getOutputTank(), false, fluidOutputs); + GTTransferUtils.addFluidsToFluidHandler(fluidOutputs, getOutputTank(), false); } /** diff --git a/src/main/java/gregtech/api/util/GTTransferUtils.java b/src/main/java/gregtech/api/util/GTTransferUtils.java index bcfea4b8948..651128764b8 100644 --- a/src/main/java/gregtech/api/util/GTTransferUtils.java +++ b/src/main/java/gregtech/api/util/GTTransferUtils.java @@ -134,40 +134,6 @@ public static boolean addItemsToItemHandler(final IItemHandler handler, return true; } - /** - * Simulates the insertion of fluid into a target fluid handler, then optionally performs the insertion. - *
- *
- * 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(MultipleTankHandler fluidHandler, - boolean simulate, - List fluidStacks) { - if (simulate) { - // OverlayedFluidHandler overlayedFluidHandler = new OverlayedFluidHandler(fluidHandler); - for (FluidStack fluidStack : fluidStacks) { - int inserted = fluidHandler.fill(fluidStack, false); - if (inserted != fluidStack.amount) { - return false; - } - } - return true; - } - - for (FluidStack fluidStack : fluidStacks) { - fluidHandler.fill(fluidStack, true); - } - return true; - } - /** * Simulates the insertion of fluid into a target fluid handler, then optionally performs the insertion.
*
fluidStacks, - IFluidHandler fluidHandler, + public static boolean addFluidsToFluidHandler(List fluidStacks, + MultipleTankHandler fluidHandler, boolean simulate) { if (simulate) { - for (FluidStack stack : fluidStacks) { - int filled = fluidHandler.fill(stack, false); - if (filled < stack.amount) + for (FluidStack fluidStack : fluidStacks) { + int inserted = fluidHandler.fill(fluidStack, false); + if (inserted != fluidStack.amount) { return false; + } } return true; } diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java index 4e690124aaa..882d9e1acfd 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/MetaTileEntityFluidDrill.java @@ -287,8 +287,8 @@ public void setWorkingEnabled(boolean isActivationAllowed) { } public boolean fillTanks(FluidStack stack, boolean simulate) { - return GTTransferUtils.addFluidsToFluidHandler(outputFluidInventory, simulate, - Collections.singletonList(stack)); + return GTTransferUtils.addFluidsToFluidHandler(Collections.singletonList(stack), outputFluidInventory, + simulate); } public int getEnergyTier() { From f4bccd0b0afc854dc6bbee58c4405db55009903d Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 15:53:24 -0700 Subject: [PATCH 23/26] pull out inner interfaces --- .../gregtech/api/capability/IFilteredFluidHandler.java | 6 ++++++ .../java/gregtech/api/capability/IFilteredHandler.java | 8 -------- .../gregtech/api/capability/IFilteredItemHandler.java | 5 +++++ .../java/gregtech/api/capability/MultipleTankHandler.java | 2 +- .../api/capability/impl/FilteredFluidHandler.java | 4 ++-- .../api/capability/impl/GTFluidHandlerItemStack.java | 4 ++-- .../capability/impl/GTSimpleFluidHandlerItemStack.java | 4 ++-- .../gregtech/api/capability/impl/NotifiableFluidTank.java | 4 ++-- .../api/items/itemhandlers/GTItemStackHandler.java | 4 ++-- src/main/java/gregtech/api/items/metaitem/MetaItem.java | 7 ++----- .../storage/MetaTileEntityQuantumTank.java | 7 ++----- 11 files changed, 26 insertions(+), 29 deletions(-) create mode 100644 src/main/java/gregtech/api/capability/IFilteredFluidHandler.java create mode 100644 src/main/java/gregtech/api/capability/IFilteredItemHandler.java diff --git a/src/main/java/gregtech/api/capability/IFilteredFluidHandler.java b/src/main/java/gregtech/api/capability/IFilteredFluidHandler.java new file mode 100644 index 00000000000..9fe92350d4b --- /dev/null +++ b/src/main/java/gregtech/api/capability/IFilteredFluidHandler.java @@ -0,0 +1,6 @@ +package gregtech.api.capability; + +import net.minecraftforge.fluids.FluidStack; + +// for type safe casting +public interface IFilteredFluidHandler extends IFilteredHandler {} diff --git a/src/main/java/gregtech/api/capability/IFilteredHandler.java b/src/main/java/gregtech/api/capability/IFilteredHandler.java index b0787c18fe6..5b8c6143253 100644 --- a/src/main/java/gregtech/api/capability/IFilteredHandler.java +++ b/src/main/java/gregtech/api/capability/IFilteredHandler.java @@ -1,8 +1,5 @@ package gregtech.api.capability; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - import org.jetbrains.annotations.Nullable; import java.util.Comparator; @@ -25,9 +22,4 @@ public interface IFilteredHandler { */ @Nullable IFilter getFilter(); - - // for type safe casting - interface FluidHandler extends IFilteredHandler {} - - interface ItemHandler extends IFilteredHandler {} } diff --git a/src/main/java/gregtech/api/capability/IFilteredItemHandler.java b/src/main/java/gregtech/api/capability/IFilteredItemHandler.java new file mode 100644 index 00000000000..c99dd02d314 --- /dev/null +++ b/src/main/java/gregtech/api/capability/IFilteredItemHandler.java @@ -0,0 +1,5 @@ +package gregtech.api.capability; + +import net.minecraft.item.ItemStack; + +public interface IFilteredItemHandler extends IFilteredHandler {} diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 55da71d8b3e..64ebac9b67a 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -134,7 +134,7 @@ public boolean allowSameFluidFill() { @Nullable @Override public IFilter getFilter() { - return getDelegate() instanceof IFilteredHandler.FluidHandler filter ? filter.getFilter() : null; + return getDelegate() instanceof IFilteredFluidHandler filter ? filter.getFilter() : null; } @Nullable diff --git a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java index 62e6800e96f..12b82f740ee 100644 --- a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java +++ b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.IFilteredFluidHandler; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.Nullable; @Deprecated -public class FilteredFluidHandler extends FluidTank implements IFilteredHandler.FluidHandler { +public class FilteredFluidHandler extends FluidTank implements IFilteredFluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java index d0f79c3425a..fe329460edc 100644 --- a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.IFilteredFluidHandler; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredHandler.FluidHandler { +public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredFluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java index 98c0be49d7b..b24fddec5d9 100644 --- a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.IFilteredFluidHandler; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -12,7 +12,7 @@ import org.jetbrains.annotations.Nullable; public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple - implements IFilteredHandler.FluidHandler { + implements IFilteredFluidHandler { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java index c941f54fcfa..25bf623ad58 100644 --- a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java +++ b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.IFilteredFluidHandler; import gregtech.api.capability.INotifiableHandler; import gregtech.api.metatileentity.MetaTileEntity; @@ -14,7 +14,7 @@ import java.util.ArrayList; import java.util.List; -public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredHandler.FluidHandler { +public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredFluidHandler { List notifiableEntities = new ArrayList<>(); private final boolean isExport; diff --git a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java index d33126a32bb..3503901fa29 100644 --- a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java +++ b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java @@ -1,7 +1,7 @@ package gregtech.api.items.itemhandlers; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.IFilteredItemHandler; import gregtech.api.metatileentity.MetaTileEntity; import net.minecraft.item.ItemStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTItemStackHandler extends ItemStackHandler implements IFilteredHandler.ItemHandler { +public class GTItemStackHandler extends ItemStackHandler implements IFilteredItemHandler { private final MetaTileEntity metaTileEntity; private IFilter filter; diff --git a/src/main/java/gregtech/api/items/metaitem/MetaItem.java b/src/main/java/gregtech/api/items/metaitem/MetaItem.java index 7bc7bf58f2d..a7056d94ead 100644 --- a/src/main/java/gregtech/api/items/metaitem/MetaItem.java +++ b/src/main/java/gregtech/api/items/metaitem/MetaItem.java @@ -1,10 +1,7 @@ package gregtech.api.items.metaitem; import gregtech.api.GTValues; -import gregtech.api.capability.GregtechCapabilities; -import gregtech.api.capability.IElectricItem; -import gregtech.api.capability.IFilteredHandler; -import gregtech.api.capability.IPropertyFluidFilter; +import gregtech.api.capability.*; import gregtech.api.capability.impl.CombinedCapabilityProvider; import gregtech.api.capability.impl.ElectricItem; import gregtech.api.gui.ModularUI; @@ -639,7 +636,7 @@ public void addInformation(@NotNull ItemStack itemStack, @Nullable World worldIn fluidTankProperties.getCapacity(), fluid == null ? "" : fluid.getLocalizedName())); - if (fluidHandler instanceof IFilteredHandler.FluidHandler filtered && + if (fluidHandler instanceof IFilteredFluidHandler filtered && filtered.getFilter() instanceof IPropertyFluidFilter propertyFilter) { propertyFilter.appendTooltips(lines, false, true); } diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java index 66fc832792f..476b10541cb 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java @@ -1,9 +1,6 @@ package gregtech.common.metatileentities.storage; -import gregtech.api.capability.GregtechTileCapabilities; -import gregtech.api.capability.IActiveOutputSide; -import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredHandler; +import gregtech.api.capability.*; import gregtech.api.capability.impl.FilteredItemHandler; import gregtech.api.capability.impl.FluidHandlerProxy; import gregtech.api.capability.impl.FluidTankList; @@ -676,7 +673,7 @@ public int getLightOpacity() { return 0; } - private class QuantumFluidTank extends FluidTank implements IFilteredHandler.FluidHandler, IFilter { + private class QuantumFluidTank extends FluidTank implements IFilteredFluidHandler, IFilter { public QuantumFluidTank(int capacity) { super(capacity); From f1f12a28a7204aba8d035a7dd5dff63cda303e61 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 15:55:58 -0700 Subject: [PATCH 24/26] pull out inner interfaces --- ...ilteredFluidHandler.java => IFilteredFluidContainer.java} | 2 +- .../java/gregtech/api/capability/IFilteredItemContainer.java | 5 +++++ .../java/gregtech/api/capability/IFilteredItemHandler.java | 5 ----- .../java/gregtech/api/capability/MultipleTankHandler.java | 2 +- .../gregtech/api/capability/impl/FilteredFluidHandler.java | 4 ++-- .../api/capability/impl/GTFluidHandlerItemStack.java | 4 ++-- .../api/capability/impl/GTSimpleFluidHandlerItemStack.java | 4 ++-- .../gregtech/api/capability/impl/NotifiableFluidTank.java | 4 ++-- .../gregtech/api/items/itemhandlers/GTItemStackHandler.java | 4 ++-- src/main/java/gregtech/api/items/metaitem/MetaItem.java | 2 +- .../metatileentities/storage/MetaTileEntityQuantumTank.java | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-) rename src/main/java/gregtech/api/capability/{IFilteredFluidHandler.java => IFilteredFluidContainer.java} (56%) create mode 100644 src/main/java/gregtech/api/capability/IFilteredItemContainer.java delete mode 100644 src/main/java/gregtech/api/capability/IFilteredItemHandler.java diff --git a/src/main/java/gregtech/api/capability/IFilteredFluidHandler.java b/src/main/java/gregtech/api/capability/IFilteredFluidContainer.java similarity index 56% rename from src/main/java/gregtech/api/capability/IFilteredFluidHandler.java rename to src/main/java/gregtech/api/capability/IFilteredFluidContainer.java index 9fe92350d4b..a1f3aae6e75 100644 --- a/src/main/java/gregtech/api/capability/IFilteredFluidHandler.java +++ b/src/main/java/gregtech/api/capability/IFilteredFluidContainer.java @@ -3,4 +3,4 @@ import net.minecraftforge.fluids.FluidStack; // for type safe casting -public interface IFilteredFluidHandler extends IFilteredHandler {} +public interface IFilteredFluidContainer extends IFilteredHandler {} diff --git a/src/main/java/gregtech/api/capability/IFilteredItemContainer.java b/src/main/java/gregtech/api/capability/IFilteredItemContainer.java new file mode 100644 index 00000000000..3ad2c845c18 --- /dev/null +++ b/src/main/java/gregtech/api/capability/IFilteredItemContainer.java @@ -0,0 +1,5 @@ +package gregtech.api.capability; + +import net.minecraft.item.ItemStack; + +public interface IFilteredItemContainer extends IFilteredHandler {} diff --git a/src/main/java/gregtech/api/capability/IFilteredItemHandler.java b/src/main/java/gregtech/api/capability/IFilteredItemHandler.java deleted file mode 100644 index c99dd02d314..00000000000 --- a/src/main/java/gregtech/api/capability/IFilteredItemHandler.java +++ /dev/null @@ -1,5 +0,0 @@ -package gregtech.api.capability; - -import net.minecraft.item.ItemStack; - -public interface IFilteredItemHandler extends IFilteredHandler {} diff --git a/src/main/java/gregtech/api/capability/MultipleTankHandler.java b/src/main/java/gregtech/api/capability/MultipleTankHandler.java index 64ebac9b67a..312cf7971b4 100644 --- a/src/main/java/gregtech/api/capability/MultipleTankHandler.java +++ b/src/main/java/gregtech/api/capability/MultipleTankHandler.java @@ -134,7 +134,7 @@ public boolean allowSameFluidFill() { @Nullable @Override public IFilter getFilter() { - return getDelegate() instanceof IFilteredFluidHandler filter ? filter.getFilter() : null; + return getDelegate() instanceof IFilteredFluidContainer filter ? filter.getFilter() : null; } @Nullable diff --git a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java index 12b82f740ee..60b925c5094 100644 --- a/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java +++ b/src/main/java/gregtech/api/capability/impl/FilteredFluidHandler.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidHandler; +import gregtech.api.capability.IFilteredFluidContainer; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.Nullable; @Deprecated -public class FilteredFluidHandler extends FluidTank implements IFilteredFluidHandler { +public class FilteredFluidHandler extends FluidTank implements IFilteredFluidContainer { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java index fe329460edc..8e1d72cda81 100644 --- a/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidHandler; +import gregtech.api.capability.IFilteredFluidContainer; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredFluidHandler { +public class GTFluidHandlerItemStack extends FluidHandlerItemStack implements IFilteredFluidContainer { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java index b24fddec5d9..4bf71590c3d 100644 --- a/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java +++ b/src/main/java/gregtech/api/capability/impl/GTSimpleFluidHandlerItemStack.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidHandler; +import gregtech.api.capability.IFilteredFluidContainer; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -12,7 +12,7 @@ import org.jetbrains.annotations.Nullable; public class GTSimpleFluidHandlerItemStack extends FluidHandlerItemStackSimple - implements IFilteredFluidHandler { + implements IFilteredFluidContainer { @Nullable private IFilter filter; diff --git a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java index 25bf623ad58..9ddd1e05fd2 100644 --- a/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java +++ b/src/main/java/gregtech/api/capability/impl/NotifiableFluidTank.java @@ -1,7 +1,7 @@ package gregtech.api.capability.impl; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredFluidHandler; +import gregtech.api.capability.IFilteredFluidContainer; import gregtech.api.capability.INotifiableHandler; import gregtech.api.metatileentity.MetaTileEntity; @@ -14,7 +14,7 @@ import java.util.ArrayList; import java.util.List; -public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredFluidHandler { +public class NotifiableFluidTank extends FluidTank implements INotifiableHandler, IFilteredFluidContainer { List notifiableEntities = new ArrayList<>(); private final boolean isExport; diff --git a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java index 3503901fa29..b4e22bf2beb 100644 --- a/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java +++ b/src/main/java/gregtech/api/items/itemhandlers/GTItemStackHandler.java @@ -1,7 +1,7 @@ package gregtech.api.items.itemhandlers; import gregtech.api.capability.IFilter; -import gregtech.api.capability.IFilteredItemHandler; +import gregtech.api.capability.IFilteredItemContainer; import gregtech.api.metatileentity.MetaTileEntity; import net.minecraft.item.ItemStack; @@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class GTItemStackHandler extends ItemStackHandler implements IFilteredItemHandler { +public class GTItemStackHandler extends ItemStackHandler implements IFilteredItemContainer { private final MetaTileEntity metaTileEntity; private IFilter filter; diff --git a/src/main/java/gregtech/api/items/metaitem/MetaItem.java b/src/main/java/gregtech/api/items/metaitem/MetaItem.java index a7056d94ead..3801ce50a83 100644 --- a/src/main/java/gregtech/api/items/metaitem/MetaItem.java +++ b/src/main/java/gregtech/api/items/metaitem/MetaItem.java @@ -636,7 +636,7 @@ public void addInformation(@NotNull ItemStack itemStack, @Nullable World worldIn fluidTankProperties.getCapacity(), fluid == null ? "" : fluid.getLocalizedName())); - if (fluidHandler instanceof IFilteredFluidHandler filtered && + if (fluidHandler instanceof IFilteredFluidContainer filtered && filtered.getFilter() instanceof IPropertyFluidFilter propertyFilter) { propertyFilter.appendTooltips(lines, false, true); } diff --git a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java index 476b10541cb..8e52afeb07d 100644 --- a/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java +++ b/src/main/java/gregtech/common/metatileentities/storage/MetaTileEntityQuantumTank.java @@ -673,7 +673,7 @@ public int getLightOpacity() { return 0; } - private class QuantumFluidTank extends FluidTank implements IFilteredFluidHandler, IFilter { + private class QuantumFluidTank extends FluidTank implements IFilteredFluidContainer, IFilter { public QuantumFluidTank(int capacity) { super(capacity); From 590fa867c684b61e2861d04b977dcae689a70675 Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:10:07 -0700 Subject: [PATCH 25/26] we still need this, actually --- .../MetaTileEntityFluidHatch.java | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java index 2b5e876cf50..70f08d120e5 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java +++ b/src/main/java/gregtech/common/metatileentities/multi/multiblockpart/MetaTileEntityFluidHatch.java @@ -339,27 +339,11 @@ private void setLocked(boolean locked) { fluidTank.onContentsChanged(); } - // todo remove - @Deprecated - protected class HatchFluidTank extends NotifiableFluidTank { + protected class HatchFluidTank extends NotifiableFluidTank implements IFilter { public HatchFluidTank(int capacity, MetaTileEntity entityToNotify, boolean isExport) { super(capacity, entityToNotify, isExport); - setFilter(new IFilter<>() { - - @Override - public boolean test(@NotNull FluidStack fluidStack) { - if (!isExportHatch) return true; - return !locked || lockedFluid == null || fluidStack.isFluidEqual(lockedFluid); - } - - @Override - public int getPriority() { - if (!isExportHatch) return IFilter.noPriority(); - return !locked || lockedFluid == null ? IFilter.noPriority() : - IFilter.whitelistPriority(1); - } - }); + setFilter(this); } @Override @@ -378,5 +362,18 @@ public int fillInternal(FluidStack resource, boolean doFill) { public void onContentsChanged() { super.onContentsChanged(); } + + @Override + public boolean test(@NotNull FluidStack fluidStack) { + if (!isExportHatch) return true; + return !locked || lockedFluid == null || fluidStack.isFluidEqual(lockedFluid); + } + + @Override + public int getPriority() { + if (!isExportHatch) return IFilter.noPriority(); + return !locked || lockedFluid == null ? IFilter.noPriority() : + IFilter.whitelistPriority(1); + } } } From 809e4fba52ee91b4db5eb940a8cf364aa681011c Mon Sep 17 00:00:00 2001 From: Ghzdude <44148655+ghzdude@users.noreply.github.com> Date: Sat, 28 Dec 2024 17:11:37 -0700 Subject: [PATCH 26/26] merge fill loops --- .../api/capability/impl/FluidTankList.java | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main/java/gregtech/api/capability/impl/FluidTankList.java b/src/main/java/gregtech/api/capability/impl/FluidTankList.java index 6300105abbc..f68e3b2fd20 100644 --- a/src/main/java/gregtech/api/capability/impl/FluidTankList.java +++ b/src/main/java/gregtech/api/capability/impl/FluidTankList.java @@ -86,37 +86,31 @@ public int fill(FluidStack resource, boolean doFill) { Entry[] fluidTanks = this.tanks.clone(); Arrays.sort(fluidTanks, ENTRY_COMPARATOR); + boolean overflow = false; + // search for tanks with same fluid type first for (Entry tank : fluidTanks) { - // if empty or fluid doesn't match, skip - if (tank.getFluidAmount() == 0 || !resource.isFluidEqual(tank.getFluid())) - continue; - - // if the fluid to insert matches the tank, insert the fluid - int inserted = tank.fill(copy, doFill); - if (inserted <= 0) continue; - - totalInserted += inserted; - copy.amount -= inserted; - if (copy.amount <= 0) return totalInserted; - } + boolean empty = tank.getFluidAmount() == 0; - boolean overflow = false; + // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid + if (empty) { + // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had + // received the fluid, skip this tank + if (overflow && !tank.allowSameFluidFill()) continue; + if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(doFill ? copy : resource)) continue; - // if we still have fluid to insert, loop through empty tanks until we find one that can accept the fluid - for (Entry tank : fluidTanks) { - // if the tank uses distinct fluid fill (allowSameFluidFill disabled) and another distinct tank had - // received the fluid, skip this tank - if (overflow && !tank.allowSameFluidFill()) continue; - if (tank.getFluidAmount() > 0 || !tank.canFillFluidType(resource)) continue; + // if not empty fluid doesn't match, skip + } else if (!resource.isFluidEqual(tank.getFluid())) + continue; + // if the fluid to insert matches the tank, insert the fluid int inserted = tank.fill(copy, doFill); if (inserted <= 0) continue; totalInserted += inserted; copy.amount -= inserted; if (copy.amount <= 0) return totalInserted; - else overflow = true; + else if (empty) overflow = true; } // return the amount of fluid that was inserted