Skip to content

Commit 690ede7

Browse files
committed
Fix ME fluid output voiding excess
- MultiFluidTank is one-to-one if copying ME fluid output
1 parent 185120c commit 690ede7

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/main/java/github/kasuminova/mmce/common/util/AEFluidInventoryUpgradeable.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* From: <a href="https://github.com/PrototypeTrousers/Applied-Energistics-2/blob/AE2-Omnifactory/src/main/java/appeng/fluids/util/AEFluidInventory.java">...</a>
2222
*/
2323
@SuppressWarnings("unchecked")
24-
public class AEFluidInventoryUpgradeable implements IAEFluidTank, ReadWriteLockProvider {
24+
public class AEFluidInventoryUpgradeable implements IAEFluidTank, ReadWriteLockProvider, IOneToOneFluidHandler {
2525
private final ReadWriteLock rwLock = new ReentrantReadWriteLock();
2626

2727
private final AtomicReference<IAEFluidStack>[] fluids;
@@ -54,6 +54,7 @@ public void setCapacity(int capacity) {
5454
}
5555
}
5656

57+
@Override
5758
public boolean isOneFluidOneSlot() {
5859
return oneFluidOneSlot;
5960
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package github.kasuminova.mmce.common.util;
2+
3+
/**
4+
* Implement this interface for a multi-fluid tank that only allows one slot per type of fluid.
5+
*/
6+
public interface IOneToOneFluidHandler {
7+
8+
/**
9+
* Returns whether distinct slots map to distinct types of fluids.
10+
* @return true if the mapping is one-to-one
11+
*/
12+
boolean isOneFluidOneSlot();
13+
14+
}

src/main/java/github/kasuminova/mmce/common/util/MultiFluidTank.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class MultiFluidTank implements IFluidHandler {
1515
private final IFluidTankProperties[] props;
1616
private int capacity;
1717

18+
private boolean oneToOne;
19+
1820
public MultiFluidTank(int capacity, int tankCount) {
1921
this.capacity = capacity;
2022
this.contents = new FluidStack[tankCount];
@@ -49,6 +51,10 @@ public MultiFluidTank(IFluidHandler from) {
4951
contents[i] = stack.copy();
5052
}
5153
}
54+
55+
if (from instanceof IOneToOneFluidHandler oneToOneHandler) {
56+
this.oneToOne = oneToOneHandler.isOneFluidOneSlot();
57+
}
5258
}
5359

5460
public int getCapacity() {
@@ -73,6 +79,22 @@ public synchronized int fill(final FluidStack fluid, final boolean doFill) {
7379

7480
final FluidStack insert = fluid.copy();
7581

82+
if (oneToOne) {
83+
// Find the distinct slot
84+
int foundSlot = -1;
85+
for (int i = 0; i < props.length; i++) {
86+
FluidStack fluidInSlot = props[i].getContents();
87+
if (fluidInSlot != null && fluidInSlot.getFluid() == fluid.getFluid()) {
88+
foundSlot = i;
89+
break;
90+
}
91+
}
92+
if (foundSlot >= 0) {
93+
return fill(foundSlot, insert, doFill);
94+
}
95+
// Didn't find existing fluid, resume normal logic.
96+
}
97+
7698
int totalFillAmount = 0;
7799
for (int i = 0; i < contents.length; i++) {
78100
int filled = fill(i, insert, doFill);
@@ -151,7 +173,7 @@ public synchronized FluidStack drain(final FluidStack resource, final boolean do
151173
int drained = drainedStack.amount;
152174
totalDrainAmount += drained;
153175

154-
if (drained >= res.amount) {
176+
if (drained >= res.amount || oneToOne) {
155177
break;
156178
}
157179
res.amount -= drained;

0 commit comments

Comments
 (0)