Skip to content

Commit 3f4db58

Browse files
committed
don't handle stack tag in constructor
implement tank interface instead of extending add todo
1 parent 52cdca5 commit 3f4db58

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

src/main/java/gregtech/common/covers/filter/readers/SimpleFluidFilterReader.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import net.minecraft.nbt.NBTTagList;
66
import net.minecraftforge.common.util.Constants;
77
import net.minecraftforge.fluids.FluidStack;
8-
import net.minecraftforge.fluids.FluidTank;
8+
import net.minecraftforge.fluids.FluidTankInfo;
9+
import net.minecraftforge.fluids.IFluidTank;
910

1011
import org.jetbrains.annotations.Nullable;
1112

@@ -19,10 +20,6 @@ public class SimpleFluidFilterReader extends BaseFilterReader {
1920
public SimpleFluidFilterReader(ItemStack container, int slots) {
2021
super(container, slots);
2122
fluidTanks = new WritableFluidTank[slots];
22-
for (int i = 0; i < fluidTanks.length; i++) {
23-
fluidTanks[i] = new WritableFluidTank(this, getInventoryNbt().getCompoundTagAt(i));
24-
}
25-
setCapacity(getStackTag().hasKey(CAPACITY) ? getCapacity() : 1000);
2623
}
2724

2825
public final boolean shouldShowAmount() {
@@ -40,10 +37,15 @@ public void setCapacity(int capacity) {
4037
}
4138

4239
public int getCapacity() {
40+
if (!getStackTag().hasKey(CAPACITY))
41+
getStackTag().setInteger(CAPACITY, 1000);
4342
return getStackTag().getInteger(CAPACITY);
4443
}
4544

4645
public WritableFluidTank getFluidTank(int i) {
46+
if (fluidTanks[i] == null) {
47+
fluidTanks[i] = new WritableFluidTank(i);
48+
}
4749
return fluidTanks[i];
4850
}
4951

@@ -78,41 +80,43 @@ public void handleLegacyNBT(NBTTagCompound tag) {
7880
}
7981
}
8082

81-
public class WritableFluidTank extends FluidTank {
83+
public class WritableFluidTank implements IFluidTank {
8284

83-
private final NBTTagCompound fluidTank;
84-
private final SimpleFluidFilterReader filterReader;
8585
protected static final String FLUID_AMOUNT = "Amount";
8686
protected static final String FLUID = "Fluid";
8787
protected static final String EMPTY = "Empty";
8888

89-
protected WritableFluidTank(SimpleFluidFilterReader filterReader, NBTTagCompound fluidTank) {
90-
super(0);
91-
this.filterReader = filterReader;
92-
this.fluidTank = fluidTank;
89+
private final int index;
90+
91+
public WritableFluidTank(int index) {
92+
this.index = index;
93+
}
94+
95+
private NBTTagCompound getTank() {
96+
return getInventoryNbt().getCompoundTagAt(this.index);
9397
}
9498

9599
public void setFluidAmount(int amount) {
96100
if (amount <= 0) {
97101
setFluid(null);
98-
} else if (this.fluidTank.hasKey(FLUID)) {
99-
this.fluidTank
102+
} else if (this.getTank().hasKey(FLUID)) {
103+
this.getTank()
100104
.getCompoundTag(FLUID)
101105
.setInteger(FLUID_AMOUNT, amount);
102106
markDirty();
103107
}
104108
}
105109

106110
public boolean isEmpty() {
107-
return !this.fluidTank.hasKey(FLUID);
111+
return !this.getTank().hasKey(FLUID);
108112
}
109113

110114
protected @Nullable NBTTagCompound getFluidTag() {
111115
if (isEmpty()) {
112116
return null;
113117
}
114118

115-
return this.fluidTank.getCompoundTag(FLUID);
119+
return this.getTank().getCompoundTag(FLUID);
116120
}
117121

118122
@Override
@@ -121,40 +125,43 @@ public boolean isEmpty() {
121125
}
122126

123127
@Override
128+
public FluidTankInfo getInfo() {
129+
return new FluidTankInfo(this);
130+
}
131+
132+
// @Override
124133
public void setFluid(@Nullable FluidStack stack) {
125134
if (stack == null) {
126-
this.fluidTank.removeTag(FLUID);
135+
this.getTank().removeTag(FLUID);
127136
} else {
128-
this.fluidTank.setTag(FLUID, stack.writeToNBT(new NBTTagCompound()));
137+
this.getTank().setTag(FLUID, stack.writeToNBT(new NBTTagCompound()));
129138
}
130139
markDirty();
131140
}
132141

133-
public boolean showAmount() {
134-
return this.filterReader.shouldShowAmount();
135-
}
136-
137142
@Override
138143
public int getFluidAmount() {
139-
return this.fluidTank
144+
return this.getTank()
140145
.getCompoundTag(FLUID)
141146
.getInteger(FLUID_AMOUNT);
142147
}
143148

144149
@Override
145150
public int getCapacity() {
146-
return this.filterReader.getCapacity();
151+
return SimpleFluidFilterReader.this.getCapacity();
147152
}
148153

149154
// getFluid() is checked for nullability, suppress
150155
@SuppressWarnings("DataFlowIssue")
151156
@Override
152157
public int fill(FluidStack resource, boolean doFill) {
158+
// todo this class amd filter readers really should not be handling show amount
159+
// in a future pr
153160
if (isEmpty() || !getFluid().isFluidEqual(resource)) {
154161
setFluid(resource);
155-
if (!showAmount()) setFluidAmount(1);
162+
if (!shouldShowAmount()) setFluidAmount(1);
156163
return resource.amount;
157-
} else if (showAmount()) {
164+
} else if (shouldShowAmount()) {
158165
var fluid = getFluid();
159166
int accepted = Math.min(resource.amount, getCapacity() - fluid.amount);
160167
fluid.amount += accepted;

0 commit comments

Comments
 (0)