-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileTank.java
261 lines (217 loc) · 6.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
/**
* Copyright (c) SpaceToad, 2011 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 buildcraft.BuildCraftCore;
import buildcraft.BuildCraftFactory;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.core.TileBuildCraft;
import buildcraft.core.fluids.Tank;
import buildcraft.core.fluids.TankManager;
import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketPayloadStream;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.proxy.CoreProxy;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
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;
public class TileTank extends TileBuildCraft implements IFluidHandler {
public final Tank tank = new Tank("tank", FluidContainerRegistry.BUCKET_VOLUME * 16, this);
public final TankManager tankManager = new TankManager(tank);
public boolean hasUpdate = false;
public SafeTimeTracker tracker = new SafeTimeTracker();
private int prevLightValue = 0;
/* UPDATING */
@Override
public void updateEntity() {
if (CoreProxy.proxy.isRenderWorld(worldObj)) {
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 && tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) {
sendNetworkUpdate(BuildCraftFactory.instance);
hasUpdate = false;
}
}
/* NETWORK */
@Override
public PacketPayload getPacketPayload() {
PacketPayload payload = new PacketPayloadStream(new PacketPayloadStream.StreamWriter() {
@Override
public void writeData(DataOutputStream data) throws IOException {
tankManager.writeData(data);
}
});
return payload;
}
@Override
public void handleUpdatePacket(PacketUpdate packet) throws IOException {
DataInputStream stream = ((PacketPayloadStream) packet.payload).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.getWorldObj().getTileEntity(tile.xCoord, tile.yCoord - 1, tile.zCoord);
if (below instanceof TileTank) {
return (TileTank) below;
} else {
return null;
}
}
public static TileTank getTankAbove(TileTank tile) {
TileEntity above = tile.getWorldObj().getTileEntity(tile.xCoord, tile.yCoord + 1, tile.zCoord);
if (above instanceof TileTank) {
return (TileTank) above;
} else {
return null;
}
}
public void moveFluidBelow() {
TileTank below = getTankBelow(this);
if (below == null) {
return;
}
int used = below.tank.fill(tank.getFluid(), true);
if (used > 0) {
hasUpdate = true; // not redundant because tank.drain operates on an IFluidTank, not a tile
below.hasUpdate = true; // redundant because below.fill sets hasUpdate
tank.drain(used, true);
}
}
/* ITANKCONTAINER */
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
if (resource == null) {
return 0;
}
resource = resource.copy();
int totalUsed = 0;
TileTank tankToFill = getBottomTank();
FluidStack liquid = tankToFill.tank.getFluid();
if (liquid != null && liquid.amount > 0 && !liquid.isFluidEqual(resource)) {
return 0;
}
while (tankToFill != null && resource.amount > 0) {
int used = tankToFill.tank.fill(resource, doFill);
resource.amount -= used;
if (used > 0) {
tankToFill.hasUpdate = true;
}
totalUsed += used;
tankToFill = getTankAbove(tankToFill);
}
return totalUsed;
}
@Override
public FluidStack drain(ForgeDirection from, int maxEmpty, boolean doDrain) {
TileTank bottom = getBottomTank();
bottom.hasUpdate = true;
return bottom.tank.drain(maxEmpty, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
if (resource == null)
return null;
if (!resource.isFluidEqual(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) {
return true;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return false;
}
public int getFluidLightLevel() {
FluidStack tankFluid = tank.getFluid();
return tankFluid == null ? 0 : tankFluid.getFluid().getLuminosity(tankFluid);
}
}