FluidStack.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * This file is part of architectury.
  3. * Copyright (C) 2020, 2021 architectury
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 3 of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package me.shedaniel.architectury.fluid;
  20. import me.shedaniel.architectury.hooks.FluidStackHooks;
  21. import me.shedaniel.architectury.utils.Fraction;
  22. import me.shedaniel.architectury.utils.NbtType;
  23. import net.minecraft.nbt.CompoundTag;
  24. import net.minecraft.network.FriendlyByteBuf;
  25. import net.minecraft.network.chat.Component;
  26. import net.minecraft.world.level.material.Fluid;
  27. import net.minecraft.world.level.material.Fluids;
  28. import org.jetbrains.annotations.Nullable;
  29. import java.util.Objects;
  30. import java.util.function.Supplier;
  31. public final class FluidStack {
  32. private static final FluidStack EMPTY = create(Fluids.EMPTY, Fraction.zero());
  33. private Fraction amount;
  34. @Nullable
  35. private CompoundTag tag;
  36. private Supplier<Fluid> fluid;
  37. private FluidStack(Supplier<Fluid> fluid, Fraction amount, CompoundTag tag) {
  38. this.fluid = Objects.requireNonNull(fluid);
  39. this.amount = Objects.requireNonNull(amount);
  40. this.tag = tag == null ? null : tag.copy();
  41. }
  42. public static FluidStack empty() {
  43. return EMPTY;
  44. }
  45. public static FluidStack create(Fluid fluid, Fraction amount, @Nullable CompoundTag tag) {
  46. return create(() -> fluid, amount, tag);
  47. }
  48. public static FluidStack create(Fluid fluid, Fraction amount) {
  49. return create(fluid, amount, null);
  50. }
  51. public static FluidStack create(Supplier<Fluid> fluid, Fraction amount, @Nullable CompoundTag tag) {
  52. return new FluidStack(fluid, amount, tag);
  53. }
  54. public static FluidStack create(Supplier<Fluid> fluid, Fraction amount) {
  55. return create(fluid, amount, null);
  56. }
  57. public static FluidStack create(FluidStack stack, Fraction amount) {
  58. return create(stack.getRawFluidSupplier(), amount, stack.getTag());
  59. }
  60. public static Fraction bucketAmount() {
  61. return FluidStackHooks.bucketAmount();
  62. }
  63. public final Fluid getFluid() {
  64. return isEmpty() ? Fluids.EMPTY : getRawFluid();
  65. }
  66. @Nullable
  67. public final Fluid getRawFluid() {
  68. return fluid.get();
  69. }
  70. public final Supplier<Fluid> getRawFluidSupplier() {
  71. return fluid;
  72. }
  73. public boolean isEmpty() {
  74. return getRawFluid() == Fluids.EMPTY || !amount.isGreaterThan(Fraction.zero());
  75. }
  76. public Fraction getAmount() {
  77. return isEmpty() ? Fraction.zero() : amount;
  78. }
  79. public void setAmount(Fraction amount) {
  80. this.amount = Objects.requireNonNull(amount);
  81. }
  82. public void grow(Fraction amount) {
  83. setAmount(this.amount.add(amount));
  84. }
  85. public void shrink(Fraction amount) {
  86. setAmount(this.amount.minus(amount));
  87. }
  88. public boolean hasTag() {
  89. return tag != null;
  90. }
  91. @Nullable
  92. public CompoundTag getTag() {
  93. return tag;
  94. }
  95. public void setTag(@Nullable CompoundTag tag) {
  96. this.tag = tag;
  97. }
  98. public CompoundTag getOrCreateTag() {
  99. if (tag == null)
  100. setTag(new CompoundTag());
  101. return tag;
  102. }
  103. @Nullable
  104. public CompoundTag getChildTag(String childName) {
  105. if (tag == null)
  106. return null;
  107. return tag.getCompound(childName);
  108. }
  109. public CompoundTag getOrCreateChildTag(String childName) {
  110. getOrCreateTag();
  111. CompoundTag child = tag.getCompound(childName);
  112. if (!tag.contains(childName, NbtType.COMPOUND)) {
  113. tag.put(childName, child);
  114. }
  115. return child;
  116. }
  117. public void removeChildTag(String childName) {
  118. if (tag != null)
  119. tag.remove(childName);
  120. }
  121. public Component getName() {
  122. return FluidStackHooks.getName(this);
  123. }
  124. public String getTranslationKey() {
  125. return FluidStackHooks.getTranslationKey(this);
  126. }
  127. public FluidStack copy() {
  128. return new FluidStack(fluid, amount, tag);
  129. }
  130. @Override
  131. public final int hashCode() {
  132. int code = 1;
  133. code = 31 * code + getFluid().hashCode();
  134. code = 31 * code + amount.hashCode();
  135. if (tag != null)
  136. code = 31 * code + tag.hashCode();
  137. return code;
  138. }
  139. @Override
  140. public final boolean equals(Object o) {
  141. if (!(o instanceof FluidStack)) {
  142. return false;
  143. }
  144. return isFluidStackEqual((FluidStack) o);
  145. }
  146. public boolean isFluidStackEqual(FluidStack other) {
  147. return getFluid() == other.getFluid() && getAmount().equals(other.getAmount()) && isTagEqual(other);
  148. }
  149. private boolean isTagEqual(FluidStack other) {
  150. return tag == null ? other.tag == null : other.tag != null && tag.equals(other.tag);
  151. }
  152. public static FluidStack read(FriendlyByteBuf buf) {
  153. return FluidStackHooks.read(buf);
  154. }
  155. public static FluidStack read(CompoundTag tag) {
  156. return FluidStackHooks.read(tag);
  157. }
  158. public void write(FriendlyByteBuf buf) {
  159. FluidStackHooks.write(this, buf);
  160. }
  161. public CompoundTag write(CompoundTag tag) {
  162. return FluidStackHooks.write(this, tag);
  163. }
  164. }