Class FluidResource

java.lang.Object
net.neoforged.neoforge.transfer.fluid.FluidResource
All Implemented Interfaces:
DataComponentGetter, DataComponentHolder, IDataComponentHolderExtension, DataComponentHolderResource<Fluid>, RegisteredResource<Fluid>, Resource

public final class FluidResource extends Object implements DataComponentHolderResource<Fluid>
Immutable combination of a Fluid and data components. Similar to a FluidStack, but immutable and without amount information.
  • Field Details

    • EMPTY

      public static final FluidResource EMPTY
      The empty resource instance of a FluidResource
    • CODEC

      public static final com.mojang.serialization.Codec<FluidResource> CODEC
      Codec for a fluid resource. Same format as FluidStack.fixedAmountCodec(int). Does not accept empty resources.
    • OPTIONAL_CODEC

      public static final com.mojang.serialization.Codec<FluidResource> OPTIONAL_CODEC
      Codec for a fluid resource. Same format as CODEC, and also accepts empty resources.
    • STREAM_CODEC

      public static final StreamCodec<RegistryFriendlyByteBuf,FluidResource> STREAM_CODEC
      Stream codec for a fluid resource. Accepts empty resources.
    • innerStack

      private final FluidStack innerStack
      A wrapped FluidStack which must never be modified or exposed. This will be a size of 1 so that we can make use of the fact it is already an instance with a data component map.
  • Constructor Details

    • FluidResource

      private FluidResource(FluidStack stack)
  • Method Details

    • of

      public static FluidResource of(FluidStack stack)
      Creates a FluidResource for the given fluid stack. Note the amount is lost.
      Parameters:
      stack - stack to copy with a size of 1
      Returns:
      If there were no patches on the stack's data components, the fluid's default resource will be returned, otherwise a new instance with the copied stack.
    • of

      public static FluidResource of(Fluid fluid)
      Note: This cannot be called before your fluid is registered
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static FluidResource of(Fluid fluid, DataComponentPatch patch)
      Note: This cannot be called before your fluid is registered
      Parameters:
      fluid - Fluid to create the resource with.
      patch - Data components that should be on the resource instance.
      Returns:
      a new FluidResource. If the fluid is empty, then EMPTY will be returned; If the patch matches the default values the default instance of that fluid will be provided.
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static FluidResource of(Holder<Fluid> fluid)
      Note: This cannot be called before your fluid is registered
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • of

      public static FluidResource of(Holder<Fluid> holder, DataComponentPatch patch)
      Note: This cannot be called before your fluid is registered
      Parameters:
      holder - Fluid holder to create the resource with.
      patch - Data components that should be on the resource instance.
      Returns:
      a new FluidResource. If the fluid is empty, then EMPTY will be returned; If the patch matches the default values the default instance of that fluid will be provided.
      Throws:
      IllegalStateException - If the backing registry is unavailable or not yet ready.
      NullPointerException - If the underlying Holder has not been populated (the target object is not registered).
    • value

      public Fluid value()
      Description copied from interface: RegisteredResource
      Returns the backing instance of the resource.
      Specified by:
      value in interface RegisteredResource<Fluid>
      Returns:
      the backing instance of the resource
    • getFluid

      public Fluid getFluid()
      Returns:
      The Fluid of this resource from the inner FluidStack
    • getHolder

      public Holder<Fluid> getHolder()
      Description copied from interface: RegisteredResource
      Returns The registered holder of the backing resource.
      Specified by:
      getHolder in interface RegisteredResource<Fluid>
      Returns:
      the fluid holder of this resource
    • getFluidType

      public FluidType getFluidType()
      Returns:
      the fluid type of this resource
    • isEmpty

      public boolean isEmpty()
      Checks if this resource is empty. The resource will be empty if the fluid is Fluids.EMPTY.
      Specified by:
      isEmpty in interface Resource
      Returns:
      if this resource is empty
    • withMergedPatch

      public FluidResource withMergedPatch(DataComponentPatch patch)
      Description copied from interface: DataComponentHolderResource
      Returns an instance of the resource with the desired patch applied on top of the existing one.
      Specified by:
      withMergedPatch in interface DataComponentHolderResource<Fluid>
      Parameters:
      patch - The patch added to the new resource instance.
      Returns:
      an instance of the resource with the desired patch applied on top of the existing one
    • with

      public <D> FluidResource with(DataComponentType<D> type, D data)
      Description copied from interface: DataComponentHolderResource
      Returns a resource with the data component set to the given value.
      Specified by:
      with in interface DataComponentHolderResource<Fluid>
      Type Parameters:
      D - the type of data component
      Parameters:
      type - the type of data component
      data - the data to set
      Returns:
      a resource with the data component set to the given value
    • with

      public <D> FluidResource with(Supplier<? extends DataComponentType<D>> type, D data)
      Description copied from interface: DataComponentHolderResource
      Returns a resource with the data component set to the given value.
      Specified by:
      with in interface DataComponentHolderResource<Fluid>
      Type Parameters:
      D - the type of data component
      Parameters:
      type - the supplier for the type of data component
      data - the data to set
      Returns:
      a resource with the data component set to the given value
    • without

      public FluidResource without(DataComponentType<?> type)
      Description copied from interface: DataComponentHolderResource
      Returns a resource without the data component, i.e. with the data component explicitly removed.
      Specified by:
      without in interface DataComponentHolderResource<Fluid>
      Parameters:
      type - the type of data component
      Returns:
      a resource without the data component, i.e. with the data component explicitly removed
    • without

      public FluidResource without(Supplier<? extends DataComponentType<?>> type)
      Description copied from interface: DataComponentHolderResource
      Returns a resource without the data component, i.e. with the data component explicitly removed.
      Specified by:
      without in interface DataComponentHolderResource<Fluid>
      Parameters:
      type - the supplier for the type of data component
      Returns:
      a resource without the data component, i.e. with the data component explicitly removed
    • getComponents

      public DataComponentMap getComponents()
      Specified by:
      getComponents in interface DataComponentHolder
    • getComponentsPatch

      public DataComponentPatch getComponentsPatch()
      Description copied from interface: DataComponentHolderResource
      Patches currently applied to the resource's data component holder.
      Specified by:
      getComponentsPatch in interface DataComponentHolderResource<Fluid>
    • toStack

      public FluidStack toStack(int amount)
      Creates a FluidStack of the specified amount.
      Parameters:
      amount - The amount of the fluid the stack should have. Must be non-negative.
      Throws:
      IllegalArgumentException - when amount is negative.
    • isComponentsPatchEmpty

      public boolean isComponentsPatchEmpty()
      Description copied from interface: DataComponentHolderResource
      Checks if the resource's data component holder has no patches applied to it. Equivalent to checking if the patch is empty, but more efficient.
      Specified by:
      isComponentsPatchEmpty in interface DataComponentHolderResource<Fluid>
    • is

      public boolean is(FluidType fluidType)
      Parameters:
      fluidType - Fluid type to check
      Returns:
      true if the inner stack's fluid type is the same as the specified fluid type.
    • matches

      public boolean matches(FluidStack stack)
      Returns true if this resource matches the fluid and components of the passed stack.
      Parameters:
      stack - the fluid stack to check
      Returns:
      true if this resource matches the fluid and components of the passed stack
    • test

      public boolean test(Predicate<FluidStack> predicate)
      Tests a FluidStack predicate with the inner stack.
      Parameters:
      predicate - Predicate to perform the test with
      Returns:
      true if the test passed
    • getHoverName

      public Component getHoverName()
      Returns:
      The hover name of the FluidStack
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object