Search in sources :

Example 1 with FluidBlockWrapper

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

the class FluidUtil method tryPlaceFluid.

/**
     * Tries to place a fluid in the world in block form and drains the container.
     * Makes a fluid emptying 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 net.minecraft.item.ItemBucket#tryPlaceContainedLiquid(EntityPlayer, World, BlockPos)}
     *
     * @param player    Player who places the fluid. May be null for blocks like dispensers.
     * @param world     World to place the fluid in
     * @param pos       The position in the world to place the fluid block
     * @param container The fluid container holding the fluidStack to place
     * @param resource  The fluidStack to place
     * @return the container's ItemStack with the remaining amount of fluid if the placement was successful, null otherwise
     */
@Nonnull
public static FluidActionResult tryPlaceFluid(@Nullable EntityPlayer player, World world, BlockPos pos, @Nonnull ItemStack container, FluidStack resource) {
    if (world == null || resource == null || pos == null) {
        return FluidActionResult.FAILURE;
    }
    Fluid fluid = resource.getFluid();
    if (fluid == null || !fluid.canBePlacedInWorld()) {
        return FluidActionResult.FAILURE;
    }
    // check that we can place the fluid at the destination
    IBlockState destBlockState = world.getBlockState(pos);
    Material destMaterial = destBlockState.getMaterial();
    boolean isDestNonSolid = !destMaterial.isSolid();
    boolean isDestReplaceable = destBlockState.getBlock().isReplaceable(world, pos);
    if (!world.isAirBlock(pos) && !isDestNonSolid && !isDestReplaceable) {
        // Non-air, solid, unreplacable block. We can't put fluid here.
        return FluidActionResult.FAILURE;
    }
    if (world.provider.doesWaterVaporize() && fluid.doesVaporize(resource)) {
        fluid.vaporize(player, world, pos, resource);
        return tryEmptyContainer(container, new VoidFluidHandler(), Integer.MAX_VALUE, player, true);
    } else {
        if (!world.isRemote && (isDestNonSolid || isDestReplaceable) && !destMaterial.isLiquid()) {
            world.destroyBlock(pos, true);
        }
        // Defer the placement to the fluid block
        // Instead of actually "filling", the fluid handler method replaces the block
        Block block = fluid.getBlock();
        IFluidHandler handler;
        if (block instanceof IFluidBlock) {
            handler = new FluidBlockWrapper((IFluidBlock) block, world, pos);
        } else if (block instanceof BlockLiquid) {
            handler = new BlockLiquidWrapper((BlockLiquid) block, world, pos);
        } else {
            handler = new BlockWrapper(block, world, pos);
        }
        FluidActionResult result = tryEmptyContainer(container, handler, Integer.MAX_VALUE, player, true);
        if (result.isSuccess()) {
            SoundEvent soundevent = fluid.getEmptySound(resource);
            world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
        }
        return result;
    }
}
Also used : VoidFluidHandler(net.minecraftforge.fluids.capability.templates.VoidFluidHandler) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) BlockWrapper(net.minecraftforge.fluids.capability.wrappers.BlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) Material(net.minecraft.block.material.Material) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler) SoundEvent(net.minecraft.util.SoundEvent) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nonnull(javax.annotation.Nonnull)

Example 2 with FluidBlockWrapper

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

the class FluidUtil method getFluidHandler.

/**
     * Helper method to get an IFluidHandler for at a block position.
     *
     * Returns null if there is no valid fluid handler.
     */
@Nullable
public static IFluidHandler getFluidHandler(World world, BlockPos blockPos, @Nullable EnumFacing side) {
    IBlockState state = world.getBlockState(blockPos);
    Block block = state.getBlock();
    if (block.hasTileEntity(state)) {
        TileEntity tileEntity = world.getTileEntity(blockPos);
        if (tileEntity != null && tileEntity.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side)) {
            return tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, side);
        }
    } else if (block instanceof IFluidBlock) {
        return new FluidBlockWrapper((IFluidBlock) block, world, blockPos);
    } else if (block instanceof BlockLiquid) {
        return new BlockLiquidWrapper((BlockLiquid) block, world, blockPos);
    }
    return null;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) BlockLiquid(net.minecraft.block.BlockLiquid) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) Block(net.minecraft.block.Block) Nullable(javax.annotation.Nullable)

Example 3 with FluidBlockWrapper

use of net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper 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)

Example 4 with FluidBlockWrapper

use of net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper in project BloodMagic by WayofTime.

the class RitualPump method performRitual.

@Override
public void performRitual(IMasterRitualStone masterRitualStone) {
    World world = masterRitualStone.getWorldObj();
    int currentEssence = masterRitualStone.getOwnerNetwork().getCurrentEssence();
    TileEntity tileEntity = world.getTileEntity(masterRitualStone.getBlockPos().up());
    if (currentEssence < getRefreshCost()) {
        masterRitualStone.getOwnerNetwork().causeNausea();
        return;
    }
    if (tileEntity != null && tileEntity.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN)) {
        IFluidHandler fluidHandler = tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, EnumFacing.DOWN);
        IBlockState tankState = world.getBlockState(masterRitualStone.getBlockPos().up());
        int maxDrain = fluidHandler.getTankProperties()[0].getCapacity();
        if (fluidHandler.getTankProperties()[0].getContents() != null && fluidHandler.getTankProperties()[0].getContents().amount >= maxDrain)
            return;
        for (BlockPos pos : getBlockRange(PUMP_RANGE).getContainedPositions(masterRitualStone.getBlockPos())) {
            IBlockState state = world.getBlockState(pos);
            IFluidHandler blockHandler = null;
            if (state.getBlock() instanceof BlockLiquid)
                blockHandler = new BlockLiquidWrapper((BlockLiquid) state.getBlock(), world, pos);
            else if (state.getBlock() instanceof IFluidHandler)
                blockHandler = new FluidBlockWrapper((IFluidBlock) state.getBlock(), world, pos);
            if (blockHandler != null) {
                FluidStack blockDrain = blockHandler.drain(maxDrain, false);
                if (blockDrain != null && fluidHandler.fill(blockDrain, false) == blockDrain.amount) {
                    Pair<BlockPos, FluidStack> posInfo = Pair.of(pos, blockHandler.drain(maxDrain, false));
                    if (!liquidsCache.contains(posInfo))
                        liquidsCache.add(posInfo);
                }
            }
        }
        blockPosIterator = liquidsCache.iterator();
        if (blockPosIterator.hasNext()) {
            Pair<BlockPos, FluidStack> posInfo = blockPosIterator.next();
            masterRitualStone.getOwnerNetwork().syphon(getRefreshCost());
            fluidHandler.fill(posInfo.getRight(), true);
            world.setBlockState(posInfo.getLeft(), Blocks.STONE.getDefaultState());
            world.notifyBlockUpdate(posInfo.getLeft(), tankState, tankState, 3);
            blockPosIterator.remove();
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) FluidBlockWrapper(net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper) IBlockState(net.minecraft.block.state.IBlockState) BlockLiquid(net.minecraft.block.BlockLiquid) FluidStack(net.minecraftforge.fluids.FluidStack) IFluidBlock(net.minecraftforge.fluids.IFluidBlock) BlockLiquidWrapper(net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) IFluidHandler(net.minecraftforge.fluids.capability.IFluidHandler)

Aggregations

FluidBlockWrapper (net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper)4 BlockLiquid (net.minecraft.block.BlockLiquid)3 IBlockState (net.minecraft.block.state.IBlockState)3 IFluidHandler (net.minecraftforge.fluids.capability.IFluidHandler)3 BlockLiquidWrapper (net.minecraftforge.fluids.capability.wrappers.BlockLiquidWrapper)3 Nonnull (javax.annotation.Nonnull)2 Block (net.minecraft.block.Block)2 TileEntity (net.minecraft.tileentity.TileEntity)2 Optional (java.util.Optional)1 Nullable (javax.annotation.Nullable)1 Material (net.minecraft.block.material.Material)1 SoundEvent (net.minecraft.util.SoundEvent)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)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 FluidStack (net.minecraftforge.fluids.FluidStack)1 IFluidBlock (net.minecraftforge.fluids.IFluidBlock)1