-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileTank.java
308 lines (254 loc) · 7.64 KB
/
TileTank.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.factory;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.EnumSkyBlock;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.core.lib.block.TileBuildCraft;
import buildcraft.core.lib.fluids.Tank;
import buildcraft.core.lib.fluids.TankManager;
public class TileTank extends TileBuildCraft implements IFluidHandler {
public final Tank tank = new Tank("tank", FluidContainerRegistry.BUCKET_VOLUME * 16, this);
public final TankManager<Tank> tankManager = new TankManager<Tank>(tank);
public boolean hasUpdate = false;
public boolean hasNetworkUpdate = false;
public SafeTimeTracker tracker = new SafeTimeTracker(2 * BuildCraftCore.updateFactor);
private int prevLightValue = 0;
private int cachedComparatorOverride = 0;
@Override
public void initialize() {
super.initialize();
updateComparators();
}
protected void updateComparators() {
int co = calculateComparatorInputOverride();
TileTank tank = getBottomTank();
while (tank != null) {
tank.cachedComparatorOverride = co;
tank.hasUpdate = true;
tank = getTankAbove(tank);
}
}
/* UPDATING */
@Override
public void updateEntity() {
super.updateEntity();
if (worldObj.isRemote) {
int lightValue = getFluidLightLevel();
if (prevLightValue != lightValue) {
prevLightValue = lightValue;
worldObj.updateLightByType(EnumSkyBlock.Block, xCoord, yCoord, zCoord);
}
return;
}
// Have liquid flow down into tanks below if any.
if (tank.getFluid() != null) {
moveFluidBelow();
}
if (hasUpdate) {
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, blockType);
hasUpdate = false;
}
if (hasNetworkUpdate && tracker.markTimeIfDelay(worldObj)) {
sendNetworkUpdate();
hasNetworkUpdate = false;
}
}
/* NETWORK */
@Override
public void writeData(ByteBuf data) {
tankManager.writeData(data);
}
@Override
public void readData(ByteBuf stream) {
tankManager.readData(stream);
}
/* SAVING & LOADING */
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
tankManager.readFromNBT(data);
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
tankManager.writeToNBT(data);
}
/* HELPER FUNCTIONS */
/**
* @return Last tank block below this one or this one if it is the last.
*/
public TileTank getBottomTank() {
TileTank lastTank = this;
while (true) {
TileTank below = getTankBelow(lastTank);
if (below != null) {
lastTank = below;
} else {
break;
}
}
return lastTank;
}
public TileTank getTopTank() {
TileTank lastTank = this;
while (true) {
TileTank above = getTankAbove(lastTank);
if (above != null) {
lastTank = above;
} else {
break;
}
}
return lastTank;
}
public static TileTank getTankBelow(TileTank tile) {
TileEntity below = tile.getTile(ForgeDirection.DOWN);
if (below instanceof TileTank) {
return (TileTank) below;
} else {
return null;
}
}
public static TileTank getTankAbove(TileTank tile) {
TileEntity above = tile.getTile(ForgeDirection.UP);
if (above instanceof TileTank) {
return (TileTank) above;
} else {
return null;
}
}
public void moveFluidBelow() {
TileTank below = getTankBelow(this);
if (below == null) {
return;
}
int oldComparator = getComparatorInputOverride();
int used = below.tank.fill(tank.getFluid(), true);
if (used > 0) {
hasNetworkUpdate = true; // not redundant because tank.drain operates on an IFluidTank, not a tile
below.hasNetworkUpdate = true; // redundant because below.fill sets hasUpdate
if (oldComparator != calculateComparatorInputOverride()) {
updateComparators();
}
tank.drain(used, true);
}
}
/* ITANKCONTAINER */
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
if (resource == null) {
return 0;
}
FluidStack resourceCopy = resource.copy();
int totalUsed = 0;
TileTank tankToFill = getBottomTank();
FluidStack liquid = tankToFill.tank.getFluid();
if (liquid != null && liquid.amount > 0 && !liquid.isFluidEqual(resourceCopy)) {
return 0;
}
int oldComparator = getComparatorInputOverride();
while (tankToFill != null && resourceCopy.amount > 0) {
int used = tankToFill.tank.fill(resourceCopy, doFill);
resourceCopy.amount -= used;
if (used > 0) {
tankToFill.hasNetworkUpdate = true;
}
totalUsed += used;
tankToFill = getTankAbove(tankToFill);
}
if (oldComparator != calculateComparatorInputOverride()) {
updateComparators();
}
return totalUsed;
}
@Override
public FluidStack drain(ForgeDirection from, int maxEmpty, boolean doDrain) {
TileTank bottom = getBottomTank();
bottom.hasNetworkUpdate = true;
int oldComparator = getComparatorInputOverride();
FluidStack output = bottom.tank.drain(maxEmpty, doDrain);
if (oldComparator != calculateComparatorInputOverride()) {
updateComparators();
}
return output;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
if (resource == null) {
return null;
}
TileTank bottom = getBottomTank();
if (!resource.isFluidEqual(bottom.tank.getFluid())) {
return null;
}
return drain(from, resource.amount, doDrain);
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection direction) {
FluidTank compositeTank = new FluidTank(tank.getCapacity());
TileTank tile = getBottomTank();
int capacity = tank.getCapacity();
if (tile != null && tile.tank.getFluid() != null) {
compositeTank.setFluid(tile.tank.getFluid().copy());
} else {
return new FluidTankInfo[]{compositeTank.getInfo()};
}
tile = getTankAbove(tile);
while (tile != null) {
FluidStack liquid = tile.tank.getFluid();
if (liquid == null || liquid.amount == 0) {
// NOOP
} else if (!compositeTank.getFluid().isFluidEqual(liquid)) {
break;
} else {
compositeTank.getFluid().amount += liquid.amount;
}
capacity += tile.tank.getCapacity();
tile = getTankAbove(tile);
}
compositeTank.setCapacity(capacity);
return new FluidTankInfo[]{compositeTank.getInfo()};
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
Fluid tankFluid = getBottomTank().tank.getFluidType();
return tankFluid == null || tankFluid == fluid;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
Fluid tankFluid = getBottomTank().tank.getFluidType();
return tankFluid != null && tankFluid == fluid;
}
public int getFluidLightLevel() {
FluidStack tankFluid = tank.getFluid();
return tankFluid == null ? 0 : tankFluid.getFluid().getLuminosity(tankFluid);
}
public int calculateComparatorInputOverride() {
FluidTankInfo[] info = getTankInfo(ForgeDirection.UNKNOWN);
if (info.length > 0 && info[0] != null && info[0].fluid != null) {
return (info[0].fluid.amount * 15 / info[0].capacity);
} else {
return 0;
}
}
public int getComparatorInputOverride() {
return cachedComparatorOverride;
}
}