-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoftFluidHolder.java
935 lines (826 loc) · 30.4 KB
/
SoftFluidHolder.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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
package net.mehvahdjukaar.selene.fluids;
import net.mehvahdjukaar.selene.fluids.client.FluidParticleColors;
import net.mehvahdjukaar.selene.util.PotionNBTHelper;
import net.mehvahdjukaar.selene.util.Utils;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.*;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.alchemy.*;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import net.minecraftforge.fluids.FluidAttributes;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.IFluidHandler;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* instance this fluid tank in your tile entity
*/
@SuppressWarnings("unused")
public class SoftFluidHolder {
public static final int BOTTLE_COUNT = 1;
public static final int BOWL_COUNT = 2;
public static final int BUCKET_COUNT = 4;
//count in bottles
private int count = 0;
private final int capacity;
@Nullable
private CompoundTag nbt = null;
private SoftFluid fluid = SoftFluidRegistry.EMPTY;
//special tint color. Used for dynamic tint fluids like water and potions
private int specialColor = 0;
private boolean needColorRefresh = true;
private net.minecraft.world.InteractionHand InteractionHand;
public SoftFluidHolder(int capacity) {
this.capacity = capacity;
}
/**
* call this method from your block when player interacts. tries to fill or empty current held item in tank
*
* @param player player
* @param hand hand
* @return interaction successful
*/
public boolean interactWithPlayer(Player player, InteractionHand handIn, @Nullable Level level, @Nullable BlockPos pos) {
ItemStack handStack = player.getItemInHand(InteractionHand);
ItemStack returnStack = this.interactWithItem(InteractionHand, level, pos, false);
//for items that have no bottle
if (returnStack != null) {
Utils.swapItem(player, InteractionHand, returnStack);
if (!handStack.isEmpty()) player.awardStat(Stats.ITEM_USED.get(handStack.getItem()));
return true;
}
return false;
}
/**
* makes current item interact with fluid tank. returns empty stack if
*
* @param stack ItemStack to be interacted with
* @param Level level. null if no sound is to be played
* @param pos position. null if no sound is to be played
* @return resulting ItemStack: empty for empty hand return, null if it failed
*/
@Nullable
public ItemStack interactWithItem( InteractionHand stack, @Nullable Level level, @Nullable BlockPos pos, boolean simulate) {
ItemStack returnStack;
//try filling
returnStack = this.tryFillingItem(stack.getItem(), level, pos, simulate);
if (returnStack != null) return returnStack;
//try emptying
returnStack = this.tryDrainItem(stack, level, pos, simulate);
return returnStack;
}
//handles special nbt items such as potions or soups
private void applyNBTtoItemStack(ItemStack stack) {
String[] nbtKey = this.fluid.getNbtKeyFromItem();
if (this.nbt != null && !this.nbt.isEmpty() && nbtKey != null) {
CompoundTag newCom = new CompoundTag();
for (String s : nbtKey) {
//ignores bottle tag, handled separately since it's a diff item
if (this.nbt.contains(s) && !s.equals("Bottle")) {
newCom.put(s, this.nbt.get(s));
}
}
if(!newCom.isEmpty())stack.setTag(newCom);
}
}
//same syntax as create
private void addPotionTag(Item i, CompoundTag com) {
String type = "REGULAR";
if (i instanceof SplashPotionItem) type = "SPLASH";
else if (i instanceof LingeringPotionItem) type = "LINGERING";
com.putString("Bottle", type);
}
/**
* tries pouring the content of provided item in the tank
* also plays sound
*
* @return empty container item, null if it failed
*/
@Nullable
public ItemStack tryDrainItem(ItemStack filledContainerStack, @Nullable Level level, @Nullable BlockPos pos, boolean simulate) {
//TODO: generalize this adding a function list that converts items in compounts and fluid pair or a compound whitelist
//TODO: all of this is horrible
Item filledContainer = filledContainerStack.getItem();
/*
if(filledContainer instanceof ISoftFluidContainerItem){
ISoftFluidContainerItem p = ((ISoftFluidContainerItem) filledContainer);
ResourceLocation r = p.getSoftFluid();
SoftFluid s = SoftFluidRegistry.get(r.toString());
if(!s.isEmpty()) {
CompoundTag nbt = p.getFluidNBT();
int am = p.getAmount();
if (this.isEmpty()) {
this.setFluid(s, nbt);
}
if (this.canAddSoftFluid(s, am, nbt)) {
this.grow(am);
SoundEvent sound = p.getEmptySound();
if (sound != null && world != null && pos != null)
world.playSound(null, pos, sound, SoundSource.BLOCKS, 1, 1);
return p.getEmptyContainer();
}
}
return null
}*/
SoftFluid s = SoftFluidRegistry.fromItem(filledContainer);
if (s.isEmpty()) return null;
CompoundTag com = filledContainerStack.getTag();
CompoundTag newCom = new CompoundTag();
//convert potions to water bottles
Potion potion = PotionUtils.getPotion(filledContainerStack);
boolean hasCustomPot = (com != null && com.contains("CustomPotionEffects"));
if (potion == Potions.WATER && !hasCustomPot){
s = SoftFluidRegistry.WATER;
}
//add tags to splash and lingering potions
else if (potion != Potions.EMPTY || hasCustomPot) {
addPotionTag(filledContainer, newCom);
}
//copy nbt from item
String[] nbtKey = s.getNbtKeyFromItem();
if (com != null && nbtKey != null) {
for (String k : nbtKey) {
if (com.contains(k)) {
newCom.put(k, com.get(k));
}
}
}
//set new fluid if empty
if (this.isEmpty()) {
this.setFluid(s, newCom.isEmpty() ? null : newCom);
}
Item empty = s.tryGettingEmptyItem(filledContainer);
SoftFluid.FilledContainerCategory category = s.tryGettingFilledItems(empty);
if (category != null && empty != null) {
int amount = category.getAmount();
if (this.canAddSoftFluid(s, amount, newCom)) {
if (simulate) return ItemStack.EMPTY;
this.grow(amount);
SoundEvent sound = category.getEmptySound();
if (sound != null && world != null && pos != null)
world.playSound(null, pos, sound, SoundSource.BLOCKS, 1, 1);
return new ItemStack(empty);
}
}
return null;
}
/**
* tries removing said amount of fluid and returns filled item
* also plays sound
*
* @return filled bottle item. null if it failed or if simulated is true and failed
*/
@Nullable
public ItemStack tryFillingItem(Item emptyContainer, @Nullable Level level, @Nullable BlockPos pos, boolean simulate) {
SoftFluid.FilledContainerCategory category = fluid.tryGettingFilledItems(emptyContainer);
if (category != null) {
int amount = category.getAmount();
if (this.canRemove(amount)) {
if (simulate) return ItemStack.EMPTY;
ItemStack stack = new ItemStack(category.getFirstFilled());
//case for lingering potions
if (this.fluid == SoftFluidRegistry.POTION) {
if (this.nbt != null && this.nbt.contains("Bottle") && !emptyContainer.getRegistryName().getNamespace().equals("inspirations")) {
String bottle = this.nbt.getString("Bottle");
if (bottle.equals("SPLASH")) stack = new ItemStack(Items.SPLASH_POTION);
else if (bottle.equals("LINGERING")) stack = new ItemStack(Items.LINGERING_POTION);
}
}
//converts water bottles into potions
if (emptyContainer == Items.GLASS_BOTTLE && fluid == SoftFluidRegistry.WATER)
stack = PotionUtils.setPotion(new ItemStack(Items.POTION), Potions.WATER);
this.applyNBTtoItemStack(stack);
this.shrink(amount);
SoundEvent sound = category.getEmptySound();
if (sound != null && world != null && pos != null)
world.playSound(null, pos, sound, SoundSource.BLOCKS, 1, 1);
return stack;
}
}
return null;
}
/**
* tries removing bottle amount and returns filled bottle
*
* @return filled bottle item. null if it failed
*/
@Nullable
public ItemStack tryFillingBottle(Level level, BlockPos pos) {
return tryFillingItem(Items.GLASS_BOTTLE, world, pos, false);
}
/**
* tries removing bucket amount and returns filled bucket
*
* @return filled bucket item. null if it failed
*/
@Nullable
public ItemStack tryFillingBucket(Level level, BlockPos pos) {
return tryFillingItem(Items.BUCKET, world, pos, false);
}
/**
* tries removing bowl amount and returns filled bowl
*
* @return filled bowl item. null if it failed
*/
@Nullable
public ItemStack tryFillingBowl(Level level, BlockPos pos) {
return tryFillingItem(Items.BOWL, world, pos, false);
}
/**
* checks if current tank holds equivalent fluid as provided forge fluids stack & nbt
*
* @param fluidStack forge fluid stack
* @param com fluid nbt
* @return is same
*/
public boolean isSameFluidAs(FluidStack fluidStack, CompoundTag com) {
return this.fluid.isEquivalent(fluidStack.getFluid()) && areNbtEquals(com,this.nbt);
}
/**
* checks if current tank holds equivalent fluid as provided forge fluids stack
*
* @param fluidStack forge fluid stack
* @return is same
*/
//might be wrong
public boolean isSameFluidAs(FluidStack fluidStack) {
return isSameFluidAs(fluidStack,fluidStack.getTag());
}
private boolean areNbtEquals(CompoundTag nbt, CompoundTag nbt1) {
return (((nbt == null || nbt.isEmpty()) && (nbt1 == null || nbt1.isEmpty())) || nbt1.equals(nbt));
}
/**
* checks if current tank holds equivalent fluid as provided soft fluid
*
* @param other soft fluid
* @return is same
*/
public boolean isSameFluidAs(SoftFluid other) {
return isSameFluidAs(other, null);
}
/**
* checks if current tank holds equivalent fluid as provided soft fluid
*
* @param other soft fluid
* @param com fluid nbt
* @return is same
*/
public boolean isSameFluidAs(SoftFluid other, @Nullable CompoundTag com) {
return this.fluid.equals(other) && areNbtEquals(this.nbt, com);
}
/**
* try adding provided forge fluid to the tank
*
* @param fluidStack forge fluid stack
* @return success
*/
public boolean tryAddingFluid(FluidStack fluidStack) {
int count = fluidStack.getAmount();
SoftFluid s = SoftFluidRegistry.fromForgeFluid(fluid.getForgeFluid());
return tryAddingFluid(s, count, fluidStack.getTag());
}
/**
* try adding provided soft fluid to the tank
*
* @param s soft fluid to add
* @param count count to add
* @param com fluid nbt
* @return success
*/
public boolean tryAddingFluid(SoftFluid s, int count, @Nullable CompoundTag com) {
if (this.canAdd(count)) {
if (this.isEmpty()) {
this.setFluid(s, com);
this.setCount(count);
return true;
} else if (this.isSameFluidAs(s, com)) {
this.grow(count);
return true;
}
}
return false;
}
/**
* try adding provided soft fluid to the tank
*
* @param s soft fluid to add
* @param count count to add
* @return success
*/
public boolean tryAddingFluid(SoftFluid s, int count) {
return tryAddingFluid(s, count, null);
}
/**
* try adding 1 bottle of provided soft fluid to the tank
*
* @param s soft fluid to add
* @return success
*/
public boolean tryAddingFluid(SoftFluid s) {
return this.tryAddingFluid(s, 1);
}
public boolean tryTransferFluid(SoftFluidHolder destination) {
return this.tryTransferFluid(destination, BOTTLE_COUNT);
}
//transfers between two fluid holders
public boolean tryTransferFluid(SoftFluidHolder destination, int amount) {
if (destination.canAdd(amount) && this.canRemove(amount)) {
if (destination.isEmpty()) {
destination.setFluid(this.getFluid(), this.getNbt());
this.shrink(amount);
destination.grow(amount);
return true;
} else if (this.isSameFluidAs(destination.getFluid(), destination.nbt)) {
this.shrink(amount);
destination.grow(amount);
return true;
}
}
return false;
}
/**
* empties n bottle of content into said forge fluid tank
*
* @param fluidDestination forge fluid tank handler
* @param bottles number of bottles to empty (1blt = 250mb)
* @return success
*/
public boolean tryTransferToFluidTank(IFluidHandler fluidDestination, int bottles) {
if (!this.canRemove(bottles)) return false;
int milliBuckets = bottles * 250;
FluidStack stack = this.toEquivalentForgeFluid(milliBuckets);
if (!stack.isEmpty()) {
int fillableAmount = fluidDestination.fill(stack, IFluidHandler.FluidAction.SIMULATE);
if (fillableAmount == milliBuckets) {
fluidDestination.fill(stack, IFluidHandler.FluidAction.EXECUTE);
this.shrink(bottles);
return true;
}
}
return false;
}
public boolean tryTransferToFluidTank(IFluidHandler fluidDestination) {
return this.tryTransferToFluidTank(fluidDestination, BOTTLE_COUNT);
}
//drains said fluid tank of 250mb (1 bottle) of fluid
public boolean drainFluidTank(IFluidHandler fluidSource, int bottles) {
if (!this.canAdd(bottles)) return false;
int milliBuckets = bottles * 250;
FluidStack drainable = fluidSource.drain(milliBuckets, IFluidHandler.FluidAction.SIMULATE);
if (!drainable.isEmpty() && drainable.getAmount() == milliBuckets) {
boolean transfer = false;
CompoundTag fsTag = drainable.getTag();
if (this.fluid.isEmpty()) {
this.setFluid(SoftFluidRegistry.fromForgeFluid(drainable.getFluid()), fsTag);
transfer = true;
} else if (this.isSameFluidAs(drainable, fsTag)) {
transfer = true;
}
if (transfer) {
fluidSource.drain(milliBuckets, IFluidHandler.FluidAction.EXECUTE);
this.grow(bottles);
return true;
}
}
return false;
}
public boolean drainFluidTank(IFluidHandler fluidSource) {
return this.drainFluidTank(fluidSource, BOTTLE_COUNT);
}
/**
* gets the equivalent forge fluid without draining the tank. returned stack might be empty
*
* @param mb forge minecraft buckets
* @return forge fluid stacks
*/
public FluidStack toEquivalentForgeFluid(int mb) {
FluidStack stack = new FluidStack(this.fluid.getForgeFluid(), mb);
this.applyNBTtoFluidStack(stack);
return stack;
}
private void applyNBTtoFluidStack(FluidStack fluidStack) {
String[] nbtKey = this.fluid.getNbtKeyFromItem();
if (this.nbt != null && !this.nbt.isEmpty() && !fluidStack.isEmpty() && nbtKey != null) {
CompoundTag newCom = new CompoundTag();
for (String k : nbtKey) {
if (this.nbt.contains(k)) {
newCom.put(k, this.nbt.get(k));
}
}
if(!newCom.isEmpty()) fluidStack.setTag(newCom);
}
}
/**
* can I remove n bottles of fluid
*
* @param n bottles amount
* @return can remove
*/
public boolean canRemove(int n) {
return this.count >= n && !this.isEmpty();
}
/**
* can I add n bottles of fluid
*
* @param n bottles amount
* @return can add
*/
public boolean canAdd(int n) {
return this.count + n <= this.capacity;
}
/**
* can provided soft fluid be added to tank
*
* @param s soft fluid to add
* @param count bottles amount
* @return can add
*/
public boolean canAddSoftFluid(SoftFluid s, int count) {
return canAddSoftFluid(s, count, null);
}
/**
* can provided soft fluid be added to tank
*
* @param s soft fluid to add
* @param count bottles amount
* @param nbt soft fluid nbt
* @return can add
*/
public boolean canAddSoftFluid(SoftFluid s, int count, @Nullable CompoundTag nbt) {
return this.canAdd(count) && this.isSameFluidAs(s, nbt);
}
public boolean isFull() {
return this.count == this.capacity;
}
public boolean isEmpty() {
//count 0 should always = to fluid.empty
return this.fluid.isEmpty() || this.count <= 0;
}
/**
* grows contained fluid by at most inc bottles. doesn't need checking
*
* @param inc maximum increment
*/
public void lossyAdd(int inc) {
this.count = Math.min(this.capacity, this.count + inc);
}
/**
* unchecked sets the tank fluid count
*
* @param count bottles count
*/
public void setCount(int count) {
this.count = count;
}
/**
* fills out tank to the maximum capacity
*/
public void fillCount() {
this.setCount(capacity);
}
/**
* unchecked grows the tank by inc bottles. Check with canAdd()
*
* @param inc bottles increment
*/
public void grow(int inc) {
this.setCount(this.count + inc);
}
/**
* unchecked shrinks the tank by inc bottles
*
* @param inc bottles increment
*/
public void shrink(int inc) {
this.grow(-inc);
if (this.count == 0) this.fluid = SoftFluidRegistry.EMPTY;
}
/**
* gets liquid height for renderer
*
* @param maxHeight maximum height in blocks
* @return fluid height
*/
public float getHeight(float maxHeight) {
return maxHeight * (float) this.count / (float) this.capacity;
}
/**
* @return comparator block redstone power
*/
public int getComparatorOutput() {
float f = this.count / (float) this.capacity;
return Mth.floor(f * 14.0F) + 1;
}
public int getCount() {
return count;
}
@Nonnull
public SoftFluid getFluid() {
return fluid;
}
@Nullable
public CompoundTag getNbt() {
return nbt;
}
public void setNbt(@Nullable CompoundTag nbt) {
this.nbt = nbt;
}
/**
* resets & clears the tank
*/
public void clear() {
this.fluid = SoftFluidRegistry.EMPTY;
this.setCount(0);
this.nbt = null;
this.specialColor = 0;
}
/**
* copies the content of a fluid tank into this
*
* @param other other tank
*/
public void copy(SoftFluidHolder other) {
this.setFluid(other.getFluid(), other.getNbt());
this.setCount(Math.min(this.capacity, other.getCount()));
}
/**
* copies the content of a fluid tank into this
*
* @param other forge fluid tank
*/
public void copy(IFluidHandler other) {
FluidStack drainable = other.getFluidInTank(0).copy();// 250, IFluidHandler.FluidAction.SIMULATE);
CompoundTag nbt = drainable.isEmpty() ? null : drainable.getTag();
this.setFluid(SoftFluidRegistry.fromForgeFluid(drainable.getFluid()), nbt);
this.setCount(Math.min(this.capacity, other.getTankCapacity(0)));
}
/**
* fills to max capacity with provided forge fluid
*
* @param fluidStack forge fluid
*/
public void fill(FluidStack fluidStack) {
this.setFluid(fluidStack);
this.fillCount();
}
/**
* fills to max capacity with provided soft fluid
*
* @param fluid forge fluid
*/
public void fill(SoftFluid fluid) {
this.fill(fluid, null);
}
/**
* fills to max capacity with provided soft fluid
*
* @param fluid soft fluid
* @param nbt soft fluid nbt
*/
public void fill(SoftFluid fluid, @Nullable CompoundTag nbt) {
this.setFluid(fluid, nbt);
this.fillCount();
}
/**
* sets current fluid to provided forge fluid equivalent
*
* @param fluidStack forge fluid
*/
public void setFluid(FluidStack fluidStack) {
SoftFluid s = SoftFluidRegistry.fromForgeFluid(fluidStack.getFluid());
this.setFluid(s, fluidStack.getTag());
}
/**
* sets current fluid to provided soft fluid equivalent
*
* @param fluid soft fluid
*/
public void setFluid(SoftFluid fluid) {
this.setFluid(fluid, null);
}
//called when it goes from empty to full
public void setFluid(SoftFluid fluid, @Nullable CompoundTag nbt) {
this.fluid = fluid;
this.nbt = nbt;
this.specialColor = 0;
if (this.fluid.isEmpty()) this.setCount(0);
this.needColorRefresh = true;
}
/**
* @return tint color to be applied on the fuid texture
*/
public int getTintColor(@Nullable LevelAccessor world, @Nullable BlockPos pos) {
SoftFluid.TintMethod method = this.fluid.getTintMethod();
if (method == SoftFluid.TintMethod.NO_TINT) return -1;
if (this.needColorRefresh) {
this.refreshSpecialColor(world, pos);
this.needColorRefresh = false;
}
if (this.specialColor != 0) return this.specialColor;
return this.fluid.getTintColor();
}
/**
* @return tint color to be applied on the fluid texture
*/
public int getFlowingTint(@Nullable LevelAccessor world, @Nullable BlockPos pos) {
SoftFluid.TintMethod method = this.fluid.getTintMethod();
if (method == SoftFluid.TintMethod.FLOWING) return this.getParticleColor(world, pos);
else return this.getTintColor(world, pos);
}
/**
* @return tint color to be used on particle. Differs from getTintColor since it returns an average color extrapolated from their fluid textures
*/
public int getParticleColor(@Nullable LevelAccessor world, @Nullable BlockPos pos) {
if (this.isEmpty()) return -1;
int tintColor = this.getTintColor(world, pos);
//if tint color is white gets averaged color
if (tintColor == -1)
return FluidParticleColors.get(this.fluid.getID());
return tintColor;
}
//grabs world/ fluidstack dependednt tint color if fluid has associated forge fluid. overrides normal tint color
private void refreshSpecialColor(@Nullable LevelAccessor world, @Nullable BlockPos pos) {
if (fluid == SoftFluidRegistry.POTION) {
this.specialColor = PotionNBTHelper.getColorFromNBT(this.nbt);
} else {
Fluid f = this.getFluid().getForgeFluid();
if (f != Fluids.EMPTY) {
FluidAttributes att = f.getAttributes();
//world accessor
int w = -1;
if (world != null && pos != null) w = att.getColor(world, pos);
//stack accessor
if (w == -1) w = att.getColor(this.toEquivalentForgeFluid(1));
if (w != -1) this.specialColor = w;
}
}
}
/**
* @return true if contained fluid has associated food
*/
public boolean containsFood() {
return this.fluid.isFood();
}
/**
* returns associated food items
*
* @return food
*/
public ItemStack getFood() {
ItemStack stack = new ItemStack(this.fluid.getFoodItem());
this.applyNBTtoItemStack(stack);
return stack;
}
/**
* call from tile entity. loads tank from nbt
*
* @param compound nbt
*/
public void load(CompoundTag compound) {
if (compound.contains("FluidHolder")) {
CompoundTag cmp = compound.getCompound("FluidHolder");
this.count = cmp.getInt("Count");
this.nbt = cmp.getCompound("NBT");
String id = cmp.getString("Fluid");
SoftFluid sf = SoftFluidRegistry.get(id);
//TODO: remove
//supplementaries backwards compat
if (sf.isEmpty()) {
sf = SoftFluidRegistry.get(id.replace("supplementaries", "minecraft"));
if (sf.isEmpty()) {
sf = SoftFluidRegistry.get(id.replace("minecraft", "supplementaries"));
}
}
this.setFluid(sf, this.nbt);
}
}
/**
* call from tile entity. saves to nbt
*
* @param compound nbt
* @return nbt
*/
public CompoundTag save(CompoundTag compound) {
CompoundTag cmp = new CompoundTag();
cmp.putInt("Count", this.count);
cmp.putString("Fluid", this.fluid.getID());
//for item render. needed for potion colors
cmp.putInt("CachedColor", this.getTintColor(null, null));
if (this.nbt != null && !this.nbt.isEmpty()) cmp.put("NBT", this.nbt);
compound.put("FluidHolder", cmp);
return compound;
}
/**
* makes player drink 1 bottle and removes it from the tank
*
* @param player player
* @param world world
* @return success
*/
public boolean tryDrinkUpFluid(Player player, Level level) {
if (this.isEmpty()) return false;
ItemStack stack = this.getFood();
Item item = stack.getItem();
//case for xp
if (this.fluid == SoftFluidRegistry.XP) {
player.giveExperiencePoints(Utils.getXPinaBottle(1, world.random));
if (world.isClientSide) return true;
this.shrink(1);
player.playNotifySound(SoundEvents.EXPERIENCE_ORB_PICKUP, SoundSource.PLAYERS, 1, 1);
return true;
}
//food
else if (this.containsFood()) {
Food food = item.getFoodProperties();
int div = this.fluid.getFoodDivider();
//single items are handled by items themselves
if (div == 1) {
stack.getItem().finishUsingItem(stack.copy(), world, player);
if (world.isClientSide) return true;
this.shrink(1);
if (food == null || stack.getItem().isEdible()) {
player.playNotifySound(item.getDrinkingSound(), SoundSource.PLAYERS, 1, 1);
}
//player already plays sound
return true;
}
boolean success = false;
//stew & bucket case
if (food != null && player.canEat(false)) {
if (item instanceof SuspiciousStewItem) susStewBehavior(player, stack, div);
if (world.isClientSide) return true;
player.getFoodData().eat(food.getNutrition() / div, food.getSaturationModifier() / (float) div);
success = true;
} else if (item instanceof MilkBucketItem) {
if (world.isClientSide) return true;
milkBottleBehavior(player, stack);
success = true;
}
if (success) {
this.shrink(1);
player.playNotifySound(item.getDrinkingSound(), SoundSource.PLAYERS, 1, 1);
return true;
}
}
return false;
}
//vanilla fluids special behaviors
//stew code
private static void susStewBehavior(Player player, ItemStack stack, int div) {
CompoundTag CompoundTag = stack.getTag();
if (CompoundTag != null && CompoundTag.contains("Effects", 9)) {
ListTag listtag = CompoundTag.getList("Effects", 10);
for (int i = 0; i < listtag.size(); ++i) {
int j = 160;
CompoundTag CompoundTag1 = listtag.getCompound(i);
if (CompoundTag1.contains("EffectDuration", 3))
j = CompoundTag1.getInt("EffectDuration") / div;
Effect effect = Effect.byId(CompoundTag1.getByte("EffectId"));
if (effect != null) {
player.addEffect(new EffectInstance(effect, j));
}
}
}
}
//removes just 1 effect
private static boolean milkBottleBehavior(Player player, ItemStack stack) {
for (EffectInstance effect : player.getActiveEffectsMap().values()) {
if (effect.isCurativeItem(stack)) {
player.removeEffect(effect.getEffect());
return true;
}
}
return false;
}
//util functions
public static int getLiquidCountFromItem(Item i) {
if (i == Items.GLASS_BOTTLE) {
return BOTTLE_COUNT;
} else if (i == Items.BOWL) {
return BOWL_COUNT;
} else if (i == Items.BUCKET) {
return BUCKET_COUNT;
}
return 0;
}
public static ItemStack getEmptyBottle() {
return new ItemStack(Items.GLASS_BOTTLE);
}
public static ItemStack getEmptyBucket() {
return new ItemStack(Items.BUCKET);
}
public static ItemStack getEmptyBowl() {
return new ItemStack(Items.BOWL);
}
}