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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
package kekztech;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MultiFluidHandler {
private final FluidStack[] fluids;
private final int maxDistinctFluids;
private final int capacityPerFluid;
private boolean locked = true;
private boolean doVoidExcess = false;
private byte fluidSelector = -1;
public MultiFluidHandler(int maxDistinctFluids, int capacityPerFluid, FluidStack[] fluidsToAdd) {
this.maxDistinctFluids = maxDistinctFluids;
this.fluids = new FluidStack[maxDistinctFluids];
if(fluidsToAdd != null) {
int tFluidLengt = (maxDistinctFluids <fluidsToAdd.length) ? maxDistinctFluids:fluidsToAdd.length;
for (int i = 0; i < tFluidLengt; i++) {
this.fluids[i] = fluidsToAdd[i];
}
}
this.capacityPerFluid = capacityPerFluid;
}
/**
* Initialize a new MultiFluidHandler object with the given parameters
* @param maxDistinctFluids
* How many different fluids can be stored
* @param capacityPerFluid
* How much capacity each fluid should have
* @param fluidsToAdd
* Fluids to add immediately
* @return
* A new instance
*/
public static MultiFluidHandler newInstance(int maxDistinctFluids, int capacityPerFluid, FluidStack...fluidsToAdd) {
return new MultiFluidHandler(maxDistinctFluids, capacityPerFluid, fluidsToAdd);
}
/**
* Deep copy a MultiFluidHandler instance with a new capacity
* @param toCopy
* The MultiFluidHandler that should be copied
* @param capacityPerFluid
* How much capacity each fluid should have
* @return
* A new instance
*/
public static MultiFluidHandler newAdjustedInstance(MultiFluidHandler toCopy, int capacityPerFluid) {
return new MultiFluidHandler(toCopy.maxDistinctFluids, capacityPerFluid, toCopy.fluids);
}
/**
* Lock internal tanks in case T.F.F.T is not running.
*
* @param state
* Lock state.
*/
public void setLock(boolean state) {
locked = state;
}
public void setDoVoidExcess(boolean doVoidExcess) { this.doVoidExcess = doVoidExcess; }
/**
* Used to tell the MFH if a fluid is selected by
* an Integrated Circuit in the controller.
* If the Integrate Circuit configuration exceeds
* the number of stored fluid, the configuration will be ignored.
*
* @param fluidSelector
* Selected fluid or -1 if no fluid is selected
*/
public void setFluidSelector(byte fluidSelector) {
this.fluidSelector = fluidSelector < fluids.length ? fluidSelector : -1;
}
/**
*
* @return
* Selected fluid or -1 if no fluid is selected
*/
public byte getSelectedFluid() {
return fluidSelector;
}
public FluidStack[] getAllFluids() {
return fluids;
}
public int getFluidPosistion(FluidStack aFluid) {
for (int i = 0; i < fluids.length; i++)
{
FluidStack tFluid = fluids[i];
if (tFluid != null && tFluid.isFluidEqual(aFluid))
return i;
}
return -1;
}
public boolean contains(FluidStack aFluid) {
if (locked)
return false;
return getFluidPosistion(aFluid)>=0;
}
public int countFluids()
{
int tCount = 0;
for (int i = 0; i < fluids.length; i++) {
if (fluids[i] != null)
tCount++;
}
return tCount;
}
public int getCapacity() {
return capacityPerFluid;
}
public int getMaxDistinctFluids() {
return maxDistinctFluids;
}
/**
* Returns a deep copy of the the FluidStack in the requested slot
* @param slot
* requested slot
* @return
* deep copy of the requested FluidStack
*/
public FluidStack getFluidCopy(int slot) {
if (slot >= fluids.length)
return null;
if (!locked
&& fluids.length > 0
&& slot >= 0
&& slot < maxDistinctFluids)
{
FluidStack tFluid = fluids[slot];
if (tFluid != null)
return tFluid.copy();
}
return null;
}
/**
* Returns the amount of different fluids currently stored.
* @return
* amount of different fluids currently stored (0-25)
*/
public int getDistinctFluids() {
int distinctFluids = 0;
for (FluidStack f : fluids) {
if (f != null)
distinctFluids++;
}
return distinctFluids;
}
/**
* Helper method to save a MultiFluidHandler to NBT data
* @param nbt
* The NBT Tag to write to
* @return
* Updated NBT Tag
*/
public NBTTagCompound saveNBTData(NBTTagCompound nbt) {
nbt = (nbt == null) ? new NBTTagCompound() : nbt;
nbt.setInteger("capacityPerFluid", getCapacity());
nbt.setInteger("maxDistinctFluids",this.maxDistinctFluids);
int c = 0;
for(FluidStack f : fluids) {
if (f == null)
{
c++;
continue;
}
nbt.setTag( String.valueOf(c), f.writeToNBT(new NBTTagCompound()));
c++;
}
return nbt;
}
/**
* Helper method to initialize a MultiFluidHandler from NBT data
* @param nbt
* The NBT Tag to read from
* @return
* A new Instance
*/
static public MultiFluidHandler loadNBTData(NBTTagCompound nbt) {
nbt = (nbt == null) ? new NBTTagCompound() : nbt;
final int capacityPerFluid = nbt.getInteger("capacityPerFluid");
final NBTTagCompound fluidsTag = (NBTTagCompound) nbt.getTag("fluids");
int distinctFluids = nbt.getInteger("maxDistinctFluids");
if (!nbt.hasKey("maxDistinctFluids"))
distinctFluids = 25;// adding it so it doesent break on upgrading
final FluidStack[] loadedFluids = new FluidStack[distinctFluids];
if (fluidsTag != null)
{
for (int i = 0; i < distinctFluids; i++) {
final NBTTagCompound fluidNBT = (NBTTagCompound) fluidsTag.getTag("" + i);
if(fluidNBT == null) {
loadedFluids[i] = null;
} else {
loadedFluids[i] = FluidStack.loadFluidStackFromNBT(fluidNBT);
}
}
}
return new MultiFluidHandler(distinctFluids, capacityPerFluid, loadedFluids);
}
public ArrayList<String> getInfoData() {
final ArrayList<String> lines = new ArrayList<>(fluids.length);
lines.add(EnumChatFormatting.YELLOW + "Stored Fluids:" + EnumChatFormatting.RESET);
for(int i = 0; i < fluids.length; i++) {
FluidStack tFluid = fluids[i];
if (tFluid == null) {
lines.add(i + " - " + "null" + ": "
+ "0" + "L ("
+ "0" + "%)");
} else {
lines.add(i + " - " + tFluid.getLocalizedName() + ": "
+ tFluid.amount + "L ("
+ (Math.round(100.0f * tFluid.amount / getCapacity())) + "%)");
}
}
return lines;
}
/**
* Fill fluid into a tank.
*
* @param push
* Fluid type and quantity to be inserted.
* @param doPush
* If false, fill will only be simulated.
* @return Amount of fluid that was (or would have been, if simulated) filled.
*/
public int pushFluid(FluidStack push, boolean doPush) {
if(locked) {
return 0;
}
int empty = getNullSlot();
int fluidCount = countFluids();
if(fluidCount >= maxDistinctFluids && !contains(push)) {
// Already contains 25 fluids and this isn't one of them
return 0;
} else if (empty < maxDistinctFluids && !contains(push)) {
// Add new fluid
final int fit = Math.min(getCapacity(), push.amount);
if(doPush) {
if (empty == -1)
return 0;
else
fluids[empty] = new FluidStack(push.getFluid(), fit);
}
// If doVoidExcess, pretend all of it fit
return doVoidExcess ? push.amount : fit;
} else {
// Add to existing fluids
int index = getFluidPosistion(push);
if (index < 0)
return 0;
final FluidStack existing = fluids[index];
final int fit = Math.min(getCapacity() - existing.amount, push.amount);
if(doPush) {
existing.amount += fit;
}
// If doVoidExcess, pretend all of it fit
return doVoidExcess ? push.amount : fit;
}
}
public int getNullSlot()
{
for (int i = 0; i < fluids.length; i++) {
if (fluids[i] == null)
return i;
}
return -1;
}
/**
* Fill fluid into the specified tank.
*
* @param push
* Fluid type and quantity to be inserted.
* @param slot
* Tank the fluid should go into.
* @param doPush
* If false, fill will only be simulated.
* @return Amount of fluid that was (or would have been, if simulated) filled.
*/
public int pushFluid(FluidStack push, int slot, boolean doPush) {
if(locked) {
return 0;
}
FluidStack tFluid = fluids[slot];
if(slot < 0 || slot >= maxDistinctFluids) {
// Invalid slot
return 0;
}
if((tFluid != null) && !tFluid.equals(push)) {
// Selected slot is taken by a non-matching fluid
return 0;
} else {
int fit = 0;
// Add to existing fluid
if (tFluid == null) {
fit = Math.min(getCapacity(),push.amount);
fluids[slot] = new FluidStack(push.getFluid(), fit);
} else {
fit = Math.min(getCapacity() - tFluid.amount, push.amount);
if(doPush) {
tFluid.amount += fit;
}
}
// If doVoidExcess, pretend all of it fit
return doVoidExcess ? push.amount : fit;
}
}
/**
* Drains fluid out of the internal tanks.
*
* @param pull
* Fluid type and quantity to be pulled.
* @param doPull
* If false, drain will only be simulated.
* @return Amount of fluid that was (or would have been, if simulated) pulled.
*/
public int pullFluid(FluidStack pull, boolean doPull) {
if (locked) {
return 0;
} else {
int tIndex = getFluidPosistion(pull);
if (tIndex < 0)
return 0;
FluidStack src = fluids[tIndex];
final int rec = Math.min(pull.amount, src.amount);
if (doPull) {
src.amount -= rec;
}
if (src.amount == 0) {
fluids[tIndex]= null;
}
return rec;
}
}
/**
* Drains fluid out of the specified internal tank.
*
* @param pull
* Fluid type and quantity to be pulled.
* @param slot
* Tank fluid should be drained from.
* @param doPull
* If false, drain will only be simulated.
* @return Amount of fluid that was (or would have been, if simulated) pulled.
*/
public int pullFluid(FluidStack pull, int slot, boolean doPull) {
if(locked || slot >= fluids.length) {
return 0;
}
if(slot < 0 || slot >= maxDistinctFluids) {
return 0;
}
FluidStack tFluid = fluids[slot];
if(tFluid == null && tFluid.equals(pull)) {
return 0;
} else {
final int rec = Math.min(pull.amount, tFluid.amount);
if(doPull) {
tFluid.amount -= rec;
}
if(tFluid.amount == 0) {
fluids[slot] = null;
}
return rec;
}
}
/**
* Test whether the given fluid type and quantity can be inserted into the internal tanks.
* @param push
* Fluid type and quantity to be tested
* @return True if there is sufficient space
*/
public boolean couldPush(FluidStack push) {
if(locked) {
return false;
}
int tFluidIndex = getFluidPosistion(push);
int fluidCount = countFluids();
if(fluidCount >= maxDistinctFluids && !contains(push)) {
return false;
} else if (fluidCount < maxDistinctFluids && !contains(push)) {
return Math.min(getCapacity(), push.amount) > 0;
} else {
final int remcap = getCapacity() - fluids[tFluidIndex].amount;
return doVoidExcess || (Math.min(remcap, push.amount) > 0);
}
}
}
|