aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/fluid/FluidTankGT.java
blob: 2102f725ae4ecc83f6ce0753e5697be4c5247c5b (plain)
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package gregtech.api.fluid;

import static com.google.common.primitives.Ints.saturatedCast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidTank;

import gregtech.api.util.GT_Utility;

public class FluidTankGT implements IFluidTank {

    public final FluidTankGT[] AS_ARRAY = new FluidTankGT[] { this };
    private FluidStack mFluid;
    private long mCapacity = 0, mAmount = 0;
    private boolean mPreventDraining = false, mVoidExcess = false, mChangedFluids = false;
    /** HashMap of adjustable Tank Sizes based on Fluids if needed. */
    private Map<String, Long> mAdjustableCapacity = null;

    private long mAdjustableMultiplier = 1;
    /** Gives you a Tank Index in case there is multiple Tanks on a TileEntity that cares. */
    public int mIndex = 0;

    public FluidTankGT() {
        mCapacity = Long.MAX_VALUE;
    }

    public FluidTankGT(long aCapacity) {
        mCapacity = aCapacity;
    }

    public FluidTankGT(FluidStack aFluid) {
        mFluid = aFluid;
        if (aFluid != null) {
            mCapacity = aFluid.amount;
            mAmount = aFluid.amount;
        }
    }

    public FluidTankGT(FluidStack aFluid, long aCapacity) {
        mFluid = aFluid;
        mCapacity = aCapacity;
        mAmount = (aFluid == null ? 0 : aFluid.amount);
    }

    public FluidTankGT(FluidStack aFluid, long aAmount, long aCapacity) {
        mFluid = aFluid;
        mCapacity = aCapacity;
        mAmount = (aFluid == null ? 0 : aAmount);
    }

    public FluidTankGT(Fluid aFluid, long aAmount) {
        this(new FluidStack(aFluid, saturatedCast(aAmount)));
        mAmount = aAmount;
    }

    public FluidTankGT(Fluid aFluid, long aAmount, long aCapacity) {
        this(new FluidStack(aFluid, saturatedCast(aAmount)), aCapacity);
        mAmount = aAmount;
    }

    public FluidTankGT(NBTTagCompound aNBT, String aKey, long aCapacity) {
        this(aNBT.hasKey(aKey) ? aNBT.getCompoundTag(aKey) : null, aCapacity);
    }

    public FluidTankGT(NBTTagCompound aNBT, long aCapacity) {
        mCapacity = aCapacity;
        if (aNBT != null && !aNBT.hasNoTags()) {
            mFluid = FluidStack.loadFluidStackFromNBT(aNBT);
            mAmount = (isEmpty() ? 0 : aNBT.hasKey("LAmount") ? aNBT.getLong("LAmount") : mFluid.amount);
        }
    }

    public FluidTankGT readFromNBT(NBTTagCompound aNBT, String aKey) {
        if (aNBT.hasKey(aKey)) {
            aNBT = aNBT.getCompoundTag(aKey);
            if (aNBT != null && !aNBT.hasNoTags()) {
                mFluid = FluidStack.loadFluidStackFromNBT(aNBT);
                mAmount = (isEmpty() ? 0 : aNBT.hasKey("LAmount") ? aNBT.getLong("LAmount") : mFluid.amount);
            }
        }
        return this;
    }

    public NBTTagCompound writeToNBT(NBTTagCompound aNBT, String aKey) {
        if (mFluid != null && (mPreventDraining || mAmount > 0)) {
            final NBTTagCompound tNBT = new NBTTagCompound();
            mFluid.amount = (int) mAmount;
            aNBT.setTag(aKey, mFluid.writeToNBT(tNBT));
            if (mAmount > Integer.MAX_VALUE) tNBT.setLong("LAmount", mAmount);
        } else {
            aNBT.removeTag(aKey);
        }
        return aNBT;
    }

    public FluidStack drain(int aDrained) {
        return drain(aDrained, true);
    }

    @Override
    public FluidStack drain(int aDrained, boolean aDoDrain) {
        if (isEmpty() || aDrained <= 0) return null;
        if (mAmount < aDrained) aDrained = (int) mAmount;
        final FluidStack rFluid = new FluidStack(mFluid, aDrained);
        if (aDoDrain) {
            mAmount -= aDrained;
            if (mAmount <= 0) {
                if (mPreventDraining) {
                    mAmount = 0;
                } else {
                    setEmpty();
                }
            }
        }
        return rFluid;
    }

    public boolean drainAll(long aDrained) {
        if (isEmpty() || mAmount < aDrained) return false;
        mAmount -= aDrained;
        if (mAmount <= 0) {
            if (mPreventDraining) {
                mAmount = 0;
            } else {
                setEmpty();
            }
        }
        return true;
    }

    public long remove(long aDrained) {
        if (isEmpty() || mAmount <= 0 || aDrained <= 0) return 0;
        if (mAmount < aDrained) aDrained = mAmount;
        mAmount -= aDrained;
        if (mAmount <= 0) {
            if (mPreventDraining) {
                mAmount = 0;
            } else {
                setEmpty();
            }
        }
        return aDrained;
    }

    public long add(long aFilled) {
        if (isEmpty() || aFilled <= 0) return 0;
        final long tCapacity = capacity();
        if (mAmount + aFilled > tCapacity) {
            if (!mVoidExcess) aFilled = tCapacity - mAmount;
            mAmount = tCapacity;
            return aFilled;
        }
        mAmount += aFilled;
        return aFilled;
    }

    public long add(long aFilled, FluidStack aFluid) {
        if (aFluid == null || aFilled <= 0) return 0;
        if (isEmpty()) {
            mFluid = aFluid.copy();
            mChangedFluids = true;
            mAmount = Math.min(capacity(aFluid), aFilled);
            return mVoidExcess ? aFilled : mAmount;
        }
        return contains(aFluid) ? add(aFilled) : 0;
    }

    public int fill(FluidStack aFluid) {
        return fill(aFluid, true);
    }

    @Override
    public int fill(FluidStack aFluid, boolean aDoFill) {
        if (aFluid == null) return 0;
        if (aDoFill) {
            if (isEmpty()) {
                mFluid = aFluid.copy();
                mChangedFluids = true;
                mAmount = Math.min(capacity(aFluid), aFluid.amount);
                return mVoidExcess ? aFluid.amount : (int) mAmount;
            }
            if (!contains(aFluid)) return 0;
            final long tCapacity = capacity(aFluid);
            long tFilled = tCapacity - mAmount;
            if (aFluid.amount < tFilled) {
                mAmount += aFluid.amount;
                tFilled = aFluid.amount;
            } else mAmount = tCapacity;
            return mVoidExcess ? aFluid.amount : (int) tFilled;
        }
        return saturatedCast(
            isEmpty() ? mVoidExcess ? aFluid.amount : Math.min(capacity(aFluid), aFluid.amount)
                : contains(aFluid) ? mVoidExcess ? aFluid.amount : Math.min(capacity(aFluid) - mAmount, aFluid.amount)
                    : 0);
    }

    public boolean canFillAll(FluidStack aFluid) {
        return aFluid == null || aFluid.amount <= 0
            || (isEmpty() ? mVoidExcess || aFluid.amount <= capacity(aFluid)
                : contains(aFluid) && (mVoidExcess || mAmount + aFluid.amount <= capacity(aFluid)));
    }

    public boolean canFillAll(long aAmount) {
        return aAmount <= 0 || mVoidExcess || mAmount + aAmount <= capacity();
    }

    public boolean fillAll(FluidStack aFluid) {
        if (aFluid == null || aFluid.amount <= 0) return true;
        if (isEmpty()) {
            final long tCapacity = capacity(aFluid);
            if (aFluid.amount <= tCapacity || mVoidExcess) {
                mFluid = aFluid.copy();
                mChangedFluids = true;
                mAmount = aFluid.amount;
                if (mAmount > tCapacity) mAmount = tCapacity;
                return true;
            }
            return false;
        }
        if (contains(aFluid)) {
            if (mAmount + aFluid.amount <= capacity()) {
                mAmount += aFluid.amount;
                return true;
            }
            if (mVoidExcess) {
                mAmount = capacity();
                return true;
            }
        }
        return false;
    }

    public boolean fillAll(FluidStack aFluid, long aMultiplier) {
        if (aMultiplier <= 0) return true;
        if (aMultiplier == 1) return fillAll(aFluid);
        if (aFluid == null || aFluid.amount <= 0) return true;
        if (isEmpty()) {
            final long tCapacity = capacity(aFluid);
            if (aFluid.amount * aMultiplier <= tCapacity || mVoidExcess) {
                mFluid = aFluid.copy();
                mChangedFluids = true;
                mAmount = aFluid.amount * aMultiplier;
                if (mAmount > tCapacity) mAmount = tCapacity;
                return true;
            }
            return false;
        }
        if (contains(aFluid)) {
            if (mAmount + aFluid.amount * aMultiplier <= capacity()) {
                mAmount += aFluid.amount * aMultiplier;
                return true;
            }
            if (mVoidExcess) {
                mAmount = capacity();
                return true;
            }
        }
        return false;
    }

    /** Resets Tank Contents entirely */
    public FluidTankGT setEmpty() {
        mFluid = null;
        mChangedFluids = true;
        mAmount = 0;
        return this;
    }

    /** Sets Fluid Content, taking Amount from the Fluid Parameter */
    public FluidTankGT setFluid(FluidStack aFluid) {
        mFluid = aFluid;
        mChangedFluids = true;
        mAmount = (aFluid == null ? 0 : aFluid.amount);
        return this;
    }

    /** Sets Fluid Content and Amount */
    public FluidTankGT setFluid(FluidStack aFluid, long aAmount) {
        mFluid = aFluid;
        mChangedFluids = true;
        mAmount = (aFluid == null ? 0 : aAmount);
        return this;
    }

    /** Sets Fluid Content, taking Amount from the Tank Parameter */
    public FluidTankGT setFluid(FluidTankGT aTank) {
        mFluid = new FluidStack(aTank.mFluid, saturatedCast(aTank.mAmount));
        mChangedFluids = true;
        mAmount = aTank.mAmount;
        return this;
    }

    /** Sets the Tank Index for easier Reverse Mapping. */
    public FluidTankGT setIndex(int aIndex) {
        mIndex = aIndex;
        return this;
    }

    /** Sets the Capacity */
    public FluidTankGT setCapacity(long aCapacity) {
        if (aCapacity >= 0) mCapacity = aCapacity;
        return this;
    }

    /** Sets the Capacity Multiplier */
    public FluidTankGT setCapacityMultiplier(long aCapacityMultiplier) {
        if (aCapacityMultiplier >= 0) mAdjustableMultiplier = aCapacityMultiplier;
        return this;
    }

    /** Sets Tank capacity Map, should it be needed. */
    public FluidTankGT setCapacity(Map<String, Long> aMap, long aCapacityMultiplier) {
        mAdjustableCapacity = aMap;
        mAdjustableMultiplier = aCapacityMultiplier;
        return this;
    }

    /** Always keeps at least 0 Liters of Fluid instead of setting it to null */
    public FluidTankGT setPreventDraining() {
        return setPreventDraining(true);
    }

    /** Always keeps at least 0 Liters of Fluid instead of setting it to null */
    public FluidTankGT setPreventDraining(boolean aPrevent) {
        mPreventDraining = aPrevent;
        return this;
    }

    /** Voids any Overlow */
    public FluidTankGT setVoidExcess() {
        return setVoidExcess(true);
    }

    /** Voids any Overlow */
    public FluidTankGT setVoidExcess(boolean aVoidExcess) {
        mVoidExcess = aVoidExcess;
        return this;
    }

    public boolean isFull() {
        return mFluid != null && mAmount >= capacity();
    }

    public long capacity() {
        return capacity(mFluid);
    }

    public long capacity(FluidStack aFluid) {
        if (mAdjustableCapacity == null || aFluid == null) return mCapacity;
        return capacity(aFluid.getFluid());
    }

    public long capacity(Fluid aFluid) {
        if (mAdjustableCapacity == null || aFluid == null) return mCapacity;
        return capacity(aFluid.getName());
    }

    public long capacity(String aFluid) {
        if (mAdjustableCapacity == null || aFluid == null) return mCapacity;

        final Long tSize = mAdjustableCapacity.get(aFluid);
        return tSize == null ? Math.max(mAmount, mCapacity)
            : Math.max(tSize * mAdjustableMultiplier, Math.max(mAmount, mCapacity));
    }

    public boolean isHalf() {
        return mFluid != null && mAmount * 2 >= capacity();
    }

    public boolean contains(Fluid aFluid) {
        return mFluid != null && mFluid.getFluid() == aFluid;
    }

    public boolean contains(FluidStack aFluid) {
        return GT_Utility.areFluidsEqual(mFluid, aFluid);
    }

    public boolean has(long aAmount) {
        return mAmount >= aAmount;
    }

    public boolean has() {
        return mAmount > 0;
    }

    public boolean check() {
        if (mChangedFluids) {
            mChangedFluids = false;
            return true;
        }
        return false;
    }

    public boolean update() {
        return mChangedFluids = true;
    }

    public boolean changed() {
        return mChangedFluids;
    }

    public long amount() {
        return isEmpty() ? 0 : mAmount;
    }

    public boolean isEmpty() {
        return mFluid == null;
    }

    public long amount(long aMax) {
        return isEmpty() || aMax <= 0 ? 0 : Math.min(mAmount, aMax);
    }

    public String name() {
        return mFluid == null ? null
            : mFluid.getFluid()
                .getName();
    }

    public FluidStack get() {
        return mFluid;
    }

    public FluidStack get(long aMax) {
        return isEmpty() || aMax <= 0 ? null : new FluidStack(mFluid, saturatedCast(Math.min(mAmount, aMax)));
    }

    @Override
    public FluidStack getFluid() {
        if (mFluid != null) mFluid.amount = saturatedCast(mAmount);
        return mFluid;
    }

    @Override
    public int getFluidAmount() {
        return saturatedCast(mAmount);
    }

    @Override
    public int getCapacity() {
        return saturatedCast(capacity());
    }

    public long getCapacityMultiplier() {
        return mAdjustableMultiplier;
    }

    @Override
    public FluidTankInfo getInfo() {
        return new FluidTankInfo(isEmpty() ? null : mFluid.copy(), saturatedCast(capacity()));
    }

    public static FluidStack[] getFluidsFromTanks(FluidTankGT[] tanks) {
        if (tanks == null) {
            return null;
        }
        List<FluidStack> fluidStacks = new ArrayList<>();
        for (FluidTankGT tank : tanks) {
            if (tank.getFluid() != null) {
                fluidStacks.add(tank.getFluid());
            }
        }
        return fluidStacks.toArray(new FluidStack[0]);
    }
}