Class FluidUtil

java.lang.Object
net.neoforged.neoforge.transfer.fluid.FluidUtil

public final class FluidUtil extends Object
Helper functions to work with ResourceHandlers of FluidResources.
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
  • Constructor Details

    • FluidUtil

      private FluidUtil()
  • Method Details

    • getStack

      public static FluidStack getStack(ResourceHandler<FluidResource> handler, int index)
      Returns a new fluid stack with the contents of the handler at the given index.
    • getFirstStackContained

      public static FluidStack getFirstStackContained(ItemStack stack)
      Returns a new fluid stack with the first fluid contents of the given item stack, ignoring the count of the stack.

      The contents from the first non-empty index are returned. As such the returned contents might not be extractable, and the stack might contain additional contents.

    • interactWithFluidHandler

      public static boolean interactWithFluidHandler(Player player, InteractionHand hand, Level level, BlockPos pos, @Nullable @Nullable Direction side)
      Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler block. First it tries to fill the item from the block, if that action fails then it tries to drain the item into the block. Automatically updates the item in the player's hand and stashes any extra items created.
      Parameters:
      player - The player doing the interaction between the item and fluid handler block.
      hand - The player's hand that is holding an item that should interact with the fluid handler block.
      level - The level that contains the fluid handler block.
      pos - The position of the fluid handler block in the level.
      side - The side of the block to interact with. May be null.
      Returns:
      true if the interaction succeeded, false otherwise.
    • interactWithFluidHandler

      public static boolean interactWithFluidHandler(Player player, InteractionHand hand, @Nullable @Nullable BlockPos pos, ResourceHandler<FluidResource> handler)
      Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler. First it tries to fill the item from the handler, if that action fails then it tries to drain the item into the handler. Automatically updates the item in the player's hand and stashes any extra items created.
      Parameters:
      player - The player doing the interaction between the item and fluid handler.
      hand - The player's hand that is holding an item that should interact with the fluid handler.
      pos - The position at which to send game events and play sounds. If null, the player's position will be used.
      handler - The fluid handler.
      Returns:
      true if the interaction succeeded, false otherwise.
    • moveWithSound

      private static FluidStack moveWithSound(ResourceHandler<FluidResource> from, ResourceHandler<FluidResource> to, Level level, @Nullable @Nullable BlockPos pos, @Nullable @Nullable Player player, boolean pickup)
    • playSoundAndGameEvent

      private static void playSoundAndGameEvent(FluidStack stack, Level level, @Nullable @Nullable BlockPos blockPos, @Nullable @Nullable Player player, boolean pickup)
    • tryPickupFluid

      public static FluidStack tryPickupFluid(@Nullable @Nullable ResourceHandler<FluidResource> destination, @Nullable @Nullable Player player, Level level, BlockPos pos, @Nullable @Nullable Direction side)
      Attempts to pick up a fluid in the level and put it into a fluid handler, first from a BucketPickup block (such as fluid sources and waterlogged blocks), or second from a Capabilities.Fluid.BLOCK capability instance.
      Parameters:
      destination - The destination for the picked up fluid. May be null.
      player - The player filling the container. Optional.
      level - The level the fluid is in.
      pos - The position of the fluid in the level.
      side - The side of the fluid that is being drained.
      Returns:
      a FluidStack holding a copy of the fluid stack that was picked up, or FluidStack.EMPTY if nothing was picked up
    • tryPlaceFluid

      public static FluidStack tryPlaceFluid(@Nullable @Nullable ResourceHandler<FluidResource> source, @Nullable @Nullable Player player, Level level, InteractionHand hand, BlockPos pos)
      Tries to extract one bucket of a fluid resource from a resource handler and place it into the level as a block. Unlike tryPlaceFluid(FluidResource, Player, Level, InteractionHand, BlockPos), this function will modify the source handler directly and return what was placed.

      Makes a fluid emptying or vaporization sound when successful. Honors the amount of fluid contained by the used container. Checks if water-like fluids should vaporize like in the nether.

      Parameters:
      source - The source for the placed fluid. May be null.
      player - Player who places the fluid. May be null for blocks like dispensers.
      level - Level to place the fluid in
      hand - Hand of the player to place the fluid with
      pos - The position in the level to place the fluid block
      Returns:
      a FluidStack holding a copy of the fluid stack that was placed, or FluidStack.EMPTY if nothing was placed
    • tryPlaceFluid

      public static boolean tryPlaceFluid(FluidResource resource, @Nullable @Nullable Player player, Level level, InteractionHand hand, BlockPos pos)
      Tries to place one bucket of a fluid resource into the level as a block. Note that e.g. extracting it from a handler on successful placement is the responsibility of the caller. See also tryPlaceFluid(ResourceHandler, Player, Level, InteractionHand, BlockPos) to modify a source handler directly.

      Makes a fluid emptying or vaporization sound when successful. Honors the amount of fluid contained by the used container. Checks if water-like fluids should vaporize like in the nether.

      Modeled after BucketItem.emptyContents(LivingEntity, Level, BlockPos, BlockHitResult, ItemStack).

      Parameters:
      resource - The fluid resource to place
      player - Player who places the fluid. May be null for blocks like dispensers.
      level - Level to place the fluid in
      hand - Hand of the player to place the fluid with
      pos - The position in the level to place the fluid block
      Returns:
      true if the placement was successful, false otherwise