Class FluidStack

java.lang.Object
net.neoforged.neoforge.fluids.FluidStack
All Implemented Interfaces:
DataComponentGetter, DataComponentHolder, IDataComponentHolderExtension, MutableDataComponentHolder

public final class FluidStack extends Object implements MutableDataComponentHolder
ItemStack equivalent for fluids. The main difference is that a fluid stack is always required to have an amount, while an item stack defaults to 1. Another difference is that the component prototype of a fluid stack is currently always empty, while an item stack gets its component prototype from the item.

Most methods in this class are adapted from ItemStack.

  • Field Details

    • FLUID_NON_EMPTY_CODEC

      public static final com.mojang.serialization.Codec<Holder<Fluid>> FLUID_NON_EMPTY_CODEC
    • MAP_CODEC

      public static final com.mojang.serialization.MapCodec<FluidStack> MAP_CODEC
      A standard map codec for fluid stacks that does not accept empty stacks.
    • CODEC

      public static final com.mojang.serialization.Codec<FluidStack> CODEC
      A standard codec for fluid stacks that does not accept empty stacks.
    • OPTIONAL_CODEC

      public static final com.mojang.serialization.Codec<FluidStack> OPTIONAL_CODEC
      A standard codec for fluid stacks that accepts empty stacks, serializing them as {}.
    • OPTIONAL_STREAM_CODEC

      public static final StreamCodec<RegistryFriendlyByteBuf,FluidStack> OPTIONAL_STREAM_CODEC
      A stream codec for fluid stacks that accepts empty stacks.
    • STREAM_CODEC

      public static final StreamCodec<RegistryFriendlyByteBuf,FluidStack> STREAM_CODEC
      A stream codec for fluid stacks that does not accept empty stacks.
    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • EMPTY

      public static final FluidStack EMPTY
    • amount

      private int amount
    • fluid

      private final Fluid fluid
    • components

      private final PatchedDataComponentMap components
  • Constructor Details

  • Method Details

    • fixedAmountCodec

      public static com.mojang.serialization.Codec<FluidStack> fixedAmountCodec(int amount)
      A standard codec for fluid stacks that always deserializes with a fixed amount, and does not accept empty stacks.

      Fluid equivalent of ItemStack.SINGLE_ITEM_CODEC.

    • getComponents

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

      public DataComponentPatch getComponentsPatch()
    • immutableComponents

      public DataComponentMap immutableComponents()
    • isComponentsPatchEmpty

      public boolean isComponentsPatchEmpty()
    • isEmpty

      public boolean isEmpty()
      Checks if this fluid stack is empty.
    • split

      public FluidStack split(int amount)
      Splits off a stack of the given amount of this stack and reduces this stack by the amount.
    • copyAndClear

      public FluidStack copyAndClear()
      Creates a copy of this stack with 0 amount.
    • getFluid

      public Fluid getFluid()
      Returns the fluid in this stack, or Fluids.EMPTY if this stack is empty.
    • getFluidHolder

      public Holder<Fluid> getFluidHolder()
    • is

      public boolean is(TagKey<Fluid> tag)
    • is

      public boolean is(Fluid fluid)
    • is

      public boolean is(Predicate<Holder<Fluid>> holderPredicate)
    • is

      public boolean is(Holder<Fluid> holder)
    • is

      public boolean is(HolderSet<Fluid> holderSet)
    • getTags

      public Stream<TagKey<Fluid>> getTags()
    • copy

      public FluidStack copy()
      Creates a copy of this fluid stack.
    • copyWithAmount

      public FluidStack copyWithAmount(int amount)
      Creates a copy of this fluid stack with the given amount.
    • matches

      public static boolean matches(FluidStack first, FluidStack second)
      Checks if the two fluid stacks are equal. This checks the fluid, amount, and components.
      Returns:
      true if the two fluid stacks have equal fluid, amount, and components
    • isSameFluid

      public static boolean isSameFluid(FluidStack first, FluidStack second)
      Checks if the two fluid stacks have the same fluid. Ignores amount and components.
      Returns:
      true if the two fluid stacks have the same fluid
    • isSameFluidSameComponents

      public static boolean isSameFluidSameComponents(FluidStack first, FluidStack second)
      Checks if the two fluid stacks have the same fluid and components. Ignores amount.
      Returns:
      true if the two fluid stacks have the same fluid and components
    • lenientOptionalFieldOf

      public static com.mojang.serialization.MapCodec<FluidStack> lenientOptionalFieldOf(String fieldName)
    • hashFluidAndComponents

      public static int hashFluidAndComponents(@Nullable FluidStack stack)
      Hashes the fluid and components of this stack, ignoring the amount.
    • getDescriptionId

      public String getDescriptionId()
      Returns the description id of this stack.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getTooltipLines

      public List<Component> getTooltipLines(Item.TooltipContext context, @Nullable Player player, TooltipFlag flag)
      Builds the tooltip lines for this fluid stack, intended for use by mods rendering fluids in GUIs.

      This mirrors the behavior of ItemStack.getTooltipLines(Item.TooltipContext, Player, TooltipFlag) as closely as possible for fluids.

      The tooltip consists of:

      If tooltips are hidden via TooltipDisplay and the tooltip is not being rendered in creative mode, an empty list is returned.

      Parameters:
      context - the tooltip context
      player - the player viewing the tooltip, or null
      flag - controls tooltip verbosity and advanced information
      Returns:
      a list of tooltip components, possibly empty
    • set

      public <T> @Nullable T set(DataComponentType<T> type, @Nullable T component)
      Sets a data component.
      Specified by:
      set in interface MutableDataComponentHolder
    • remove

      public <T> @Nullable T remove(DataComponentType<? extends T> type)
      Removes a data component.
      Specified by:
      remove in interface MutableDataComponentHolder
    • applyComponents

      public void applyComponents(DataComponentPatch patch)
      Applies a set of component changes to this stack.
      Specified by:
      applyComponents in interface MutableDataComponentHolder
    • applyComponents

      public void applyComponents(DataComponentMap components)
      Applies a set of component changes to this stack.
      Specified by:
      applyComponents in interface MutableDataComponentHolder
    • getHoverName

      public Component getHoverName()
      Returns the hover name of this stack.
    • getAmount

      public int getAmount()
      Returns the amount of this stack.
    • setAmount

      public void setAmount(int amount)
      Sets the amount of this stack.
    • limitSize

      public void limitSize(int amount)
      Limits the amount of this stack is at most the given amount.
    • grow

      public void grow(int addedAmount)
      Adds the given amount to this stack.
    • shrink

      public void shrink(int removedAmount)
      Removes the given amount from this stack.
    • getFluidType

      public FluidType getFluidType()
      Returns the fluid type of this stack.
    • is

      public boolean is(FluidType fluidType)
      Check if the fluid type of this stack is equal to the given fluid type.