Search in sources :

Example 1 with BucketPickupHandlerWrapper

use of net.minecraftforge.fluids.capability.wrappers.BucketPickupHandlerWrapper in project MinecraftForge by MinecraftForge.

the class FluidUtil method tryPickUpFluid.

/**
 * Attempts to pick up a fluid in the world and put it in an empty container item.
 *
 * @param emptyContainer The empty container to fill.
 *                       Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
 * @param playerIn       The player filling the container. Optional.
 * @param worldIn        The world the fluid is in.
 * @param pos            The position of the fluid in the world.
 * @param side           The side of the fluid that is being drained.
 * @return a {@link FluidActionResult} holding the result and the resulting container.
 */
@Nonnull
public static FluidActionResult tryPickUpFluid(@Nonnull ItemStack emptyContainer, @Nullable Player playerIn, Level worldIn, BlockPos pos, Direction side) {
    if (emptyContainer.isEmpty() || worldIn == null || pos == null) {
        return FluidActionResult.FAILURE;
    }
    BlockState state = worldIn.getBlockState(pos);
    Block block = state.getBlock();
    IFluidHandler targetFluidHandler;
    if (block instanceof IFluidBlock) {
        targetFluidHandler = new FluidBlockWrapper((IFluidBlock) block, worldIn, pos);
    } else if (block instanceof BucketPickup) {
        targetFluidHandler = new BucketPickupHandlerWrapper((BucketPickup) block, worldIn, pos);
    } else {
        Optional<IFluidHandler> fluidHandler = getFluidHandler(worldIn, pos, side).resolve();
        if (!fluidHandler.isPresent()) {
            return FluidActionResult.FAILURE;
        }
        targetFluidHandler = fluidHandler.get();
    }
    return tryFillContainer(emptyContainer, targetFluidHandler, Integer.MAX_VALUE, playerIn, true);
}
Also used : FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BucketPickup(net.minecraft.world.level.block.BucketPickup) BucketPickupHandlerWrapper(net.minecraftforge.fluids.capability.wrappers.BucketPickupHandlerWrapper) BlockState(net.minecraft.world.level.block.state.BlockState) LazyOptional(net.minecraftforge.common.util.LazyOptional) Optional(java.util.Optional) Block(net.minecraft.world.level.block.Block) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) Nonnull(javax.annotation.Nonnull)

Aggregations

Optional (java.util.Optional)1 Nonnull (javax.annotation.Nonnull)1 Block (net.minecraft.world.level.block.Block)1 BucketPickup (net.minecraft.world.level.block.BucketPickup)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 LazyOptional (net.minecraftforge.common.util.LazyOptional)1 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)1 BucketPickupHandlerWrapper (net.minecraftforge.fluids.capability.wrappers.BucketPickupHandlerWrapper)1 FluidBlockWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper)1