ItemStack substitute for Fluids. NOTE: Equality is based on the Fluid, not the amount. Use #isFluidStackIdentical(FluidStack) to determine if FluidID, Amount and NBT Tag are all equal.
| 50 | * |
| 51 | */ |
| 52 | public class FluidStack |
| 53 | { |
| 54 | private static final Logger LOGGER = LogManager.getLogger(); |
| 55 | |
| 56 | public static final FluidStack EMPTY = new FluidStack(Fluids.EMPTY, 0); |
| 57 | |
| 58 | public static final Codec<FluidStack> CODEC = RecordCodecBuilder.create( |
| 59 | instance -> instance.group( |
| 60 | Registry.FLUID.fieldOf("FluidName").forGetter(FluidStack::getFluid), |
| 61 | Codec.INT.fieldOf("Amount").forGetter(FluidStack::getAmount), |
| 62 | CompoundNBT.CODEC.optionalFieldOf("Tag").forGetter(stack -> Optional.ofNullable(stack.getTag())) |
| 63 | ).apply(instance, (fluid, amount, tag) -> { |
| 64 | FluidStack stack = new FluidStack(fluid, amount); |
| 65 | tag.ifPresent(stack::setTag); |
| 66 | return stack; |
| 67 | }) |
| 68 | ); |
| 69 | |
| 70 | private boolean isEmpty; |
| 71 | private int amount; |
| 72 | private CompoundNBT tag; |
| 73 | private IRegistryDelegate<Fluid> fluidDelegate; |
| 74 | |
| 75 | public FluidStack(Fluid fluid, int amount) |
| 76 | { |
| 77 | if (fluid == null) |
| 78 | { |
| 79 | LOGGER.fatal("Null fluid supplied to fluidstack. Did you try and create a stack for an unregistered fluid?"); |
| 80 | throw new IllegalArgumentException("Cannot create a fluidstack from a null fluid"); |
| 81 | } |
| 82 | else if (ForgeRegistries.FLUIDS.getKey(fluid) == null) |
| 83 | { |
| 84 | LOGGER.fatal("Failed attempt to create a FluidStack for an unregistered Fluid {} (type {})", fluid.getRegistryName(), fluid.getClass().getName()); |
| 85 | throw new IllegalArgumentException("Cannot create a fluidstack from an unregistered fluid"); |
| 86 | } |
| 87 | this.fluidDelegate = fluid.delegate; |
| 88 | this.amount = amount; |
| 89 | |
| 90 | updateEmpty(); |
| 91 | } |
| 92 | |
| 93 | public FluidStack(Fluid fluid, int amount, CompoundNBT nbt) |
| 94 | { |
| 95 | this(fluid, amount); |
| 96 | |
| 97 | if (nbt != null) |
| 98 | { |
| 99 | tag = nbt.copy(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | public FluidStack(FluidStack stack, int amount) |
| 104 | { |
| 105 | this(stack.getFluid(), amount, stack.tag); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * This provides a safe method for retrieving a FluidStack - if the Fluid is invalid, the stack |