-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileTank.java
254 lines (214 loc) · 6.57 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
/**
* 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.network.PacketPayload;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.proxy.CoreProxy;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import static net.minecraftforge.common.ForgeDirection.DOWN;
import net.minecraftforge.liquids.ILiquidTank;
import net.minecraftforge.liquids.ITankContainer;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidTank;
public class TileTank extends TileBuildCraft implements ITankContainer {
public final LiquidTank tank = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * 16);
public boolean hasUpdate = false;
public SafeTimeTracker tracker = new SafeTimeTracker();
/* UPDATING */
@Override
public void updateEntity() {
if (CoreProxy.proxy.isSimulating(worldObj) && hasUpdate && tracker.markTimeIfDelay(worldObj, 2 * BuildCraftCore.updateFactor)) {
sendNetworkUpdate();
hasUpdate = false;
}
if (CoreProxy.proxy.isRenderWorld(worldObj)) {
return;
}
// Have liquid flow down into tanks below if any.
if (tank.getLiquid() != null) {
moveLiquidBelow();
}
}
/* NETWORK */
@Override
public PacketPayload getPacketPayload() {
PacketPayload payload = new PacketPayload(3, 0, 0);
if (tank.getLiquid() != null) {
payload.intPayload[0] = tank.getLiquid().itemID;
payload.intPayload[1] = tank.getLiquid().itemMeta;
payload.intPayload[2] = tank.getLiquid().amount;
} else {
payload.intPayload[0] = 0;
payload.intPayload[1] = 0;
payload.intPayload[2] = 0;
}
return payload;
}
@Override
public void handleUpdatePacket(PacketUpdate packet) {
if (packet.payload.intPayload[0] > 0) {
LiquidStack liquid = new LiquidStack(packet.payload.intPayload[0], packet.payload.intPayload[2], packet.payload.intPayload[1]);
tank.setLiquid(liquid);
} else {
tank.setLiquid(null);
}
}
/* SAVING & LOADING */
@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if (data.hasKey("stored") && data.hasKey("liquidId")) {
LiquidStack liquid = new LiquidStack(data.getInteger("liquidId"), data.getInteger("stored"), 0);
tank.setLiquid(liquid);
} else {
LiquidStack liquid = new LiquidStack(0, 0, 0);
liquid.readFromNBT(data.getCompoundTag("tank"));
tank.setLiquid(liquid);
}
}
@Override
public void writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
if (tank.getLiquid() != null) {
data.setTag("tank", tank.getLiquid().writeToNBT(new NBTTagCompound()));
}
}
/* 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.worldObj.getBlockTileEntity(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.worldObj.getBlockTileEntity(tile.xCoord, tile.yCoord + 1, tile.zCoord);
if (above instanceof TileTank) {
return (TileTank) above;
} else {
return null;
}
}
public void moveLiquidBelow() {
TileTank below = getTankBelow(this);
if (below == null) {
return;
}
int used = below.tank.fill(tank.getLiquid(), true);
if (used > 0) {
hasUpdate = true; // not redundant because tank.drain operates on an ILiquidTank, not a tile
below.hasUpdate = true; // redundant because below.fill sets hasUpdate
tank.drain(used, true);
}
}
/* ITANKCONTAINER */
@Override
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill) {
return fill(0, resource, doFill);
}
@Override
public int fill(int tankIndex, LiquidStack resource, boolean doFill) {
if (tankIndex != 0 || resource == null) {
return 0;
}
resource = resource.copy();
int totalUsed = 0;
TileTank tankToFill = getBottomTank();
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);
}
if (totalUsed > 0) {
hasUpdate = true;
}
return totalUsed;
}
@Override
public LiquidStack drain(ForgeDirection from, int maxEmpty, boolean doDrain) {
return drain(0, maxEmpty, doDrain);
}
@Override
public LiquidStack drain(int tankIndex, int maxEmpty, boolean doDrain) {
TileTank bottom = getBottomTank();
bottom.hasUpdate = true;
return bottom.tank.drain(maxEmpty, doDrain);
}
@Override
public ILiquidTank[] getTanks(ForgeDirection direction) {
LiquidTank compositeTank = new LiquidTank(tank.getCapacity());
TileTank tile = getBottomTank();
int capacity = tank.getCapacity();
if (tile != null && tile.tank.getLiquid() != null) {
compositeTank.setLiquid(tile.tank.getLiquid().copy());
} else {
return new ILiquidTank[]{compositeTank};
}
tile = getTankAbove(tile);
while (tile != null) {
LiquidStack liquid = tile.tank.getLiquid();
if (liquid == null || liquid.amount == 0) {
// NOOP
} else if (!compositeTank.getLiquid().isLiquidEqual(liquid)) {
break;
} else {
compositeTank.getLiquid().amount += liquid.amount;
}
capacity += tile.tank.getCapacity();
tile = getTankAbove(tile);
}
compositeTank.setCapacity(capacity);
return new ILiquidTank[]{compositeTank};
}
@Override
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type) {
if (direction == DOWN && worldObj != null && worldObj.getBlockId(xCoord, yCoord - 1, zCoord) != BuildCraftFactory.tankBlock.blockID) {
return tank;
}
return null;
}
}