Search in sources :

Example 1 with LiquidBlockContainer

use of net.minecraft.world.level.block.LiquidBlockContainer in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPlaceFluid.

/**
 * Tries to place a fluid resource into the world as a block and drains the fluidSource.
 * 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 {@link BucketItem#emptyContents(Player, Level, BlockPos, BlockHitResult)}
 *
 * @param player      Player who places the fluid. May be null for blocks like dispensers.
 * @param world       Level to place the fluid in
 * @param hand
 * @param pos         The position in the world to place the fluid block
 * @param fluidSource The fluid source holding the fluidStack to place
 * @param resource    The fluidStack to place.
 * @return true if the placement was successful, false otherwise
 */
public static boolean tryPlaceFluid(@Nullable Player player, Level world, InteractionHand hand, BlockPos pos, IFluidHandler fluidSource, FluidStack resource) {
    if (world == null || pos == null) {
        return false;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == Fluids.EMPTY || !fluid.getAttributes().canBePlacedInWorld(world, pos, resource)) {
        return false;
    }
    if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE).isEmpty()) {
        return false;
    }
    BlockPlaceContext context = new BlockPlaceContext(world, player, hand, player == null ? ItemStack.EMPTY : player.getItemInHand(hand), new BlockHitResult(Vec3.ZERO, Direction.UP, pos, false));
    // check that we can place the fluid at the destination
    BlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.canBeReplaced(context);
    boolean canDestContainFluid = destBlockState.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer) destBlockState.getBlock()).canPlaceLiquid(world, pos, destBlockState, fluid);
    if (!world.isEmptyBlock(pos) && !isDestNonSolid && !isDestReplaceable && !canDestContainFluid) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return false;
    }
    if (world.dimensionType().ultraWarm() && fluid.getAttributes().doesVaporize(world, pos, resource)) {
        FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE);
        if (!result.isEmpty()) {
            result.getFluid().getAttributes().vaporize(player, world, pos, result);
            return true;
        }
    } else {
        // This fluid handler places the fluid block when filled
        IFluidHandler handler;
        if (canDestContainFluid) {
            handler = new BlockWrapper.LiquidContainerBlockWrapper((LiquidBlockContainer) destBlockState.getBlock(), world, pos);
        } else {
            handler = getFluidBlockHandler(fluid, world, pos);
        }
        FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true);
        if (!result.isEmpty()) {
            SoundEvent soundevent = resource.getFluid().getAttributes().getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F);
            return true;
        }
    }
    return false;
}
Also used : LiquidBlockContainer(net.minecraft.world.level.block.LiquidBlockContainer) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) SoundEvent(net.minecraft.sounds.SoundEvent) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPlaceContext(net.minecraft.world.item.context.BlockPlaceContext) Fluid(net.minecraft.world.level.material.Fluid) Material(net.minecraft.world.level.material.Material) BlockHitResult(net.minecraft.world.phys.BlockHitResult) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

SoundEvent (net.minecraft.sounds.SoundEvent)1 BlockPlaceContext (net.minecraft.world.item.context.BlockPlaceContext)1 LiquidBlockContainer (net.minecraft.world.level.block.LiquidBlockContainer)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 Fluid (net.minecraft.world.level.material.Fluid)1 Material (net.minecraft.world.level.material.Material)1 BlockHitResult (net.minecraft.world.phys.BlockHitResult)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1 BlockWrapper (net.minecraftforge.fluids.capability.wrappers.BlockWrapper)1 FluidBlockWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper)1