Search in sources :

Example 1 with ILiquidContainer

use of net.minecraft.block.ILiquidContainer in project Ceramics by KnightMiner.

the class ClayBucketItem method onItemRightClick.

/* Bucket behavior */
@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
    ItemStack stack = player.getHeldItem(hand);
    Fluid fluid = this.getFluid(stack);
    BlockRayTraceResult trace = rayTrace(world, player, fluid == Fluids.EMPTY ? FluidMode.SOURCE_ONLY : RayTraceContext.FluidMode.NONE);
    // fire Forge event for bucket use
    ActionResult<ItemStack> ret = ForgeEventFactory.onBucketUse(player, world, stack, trace);
    if (ret != null) {
        return ret;
    }
    // if we missed, do nothing
    if (trace.getType() != Type.BLOCK) {
        return ActionResult.resultPass(stack);
    }
    // normal fluid logic
    BlockPos pos = trace.getPos();
    Direction direction = trace.getFace();
    BlockPos offset = pos.offset(direction);
    // ensure we can place a fluid there
    if (world.isBlockModifiable(player, pos) && player.canPlayerEdit(offset, direction, stack)) {
        BlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        if (block == Blocks.CAULDRON && !player.isCrouching()) {
            ActionResult<ItemStack> result = interactWithCauldron(world, pos, state, player, stack, fluid);
            if (result.getType() != ActionResultType.PASS) {
                return result;
            }
        }
        if (fluid == Fluids.EMPTY) {
            if (block instanceof IBucketPickupHandler) {
                Fluid newFluid = ((IBucketPickupHandler) block).pickupFluid(world, pos, state);
                if (newFluid != Fluids.EMPTY) {
                    player.addStat(Stats.ITEM_USED.get(this));
                    // play sound effect
                    SoundEvent sound = newFluid.getAttributes().getFillSound();
                    if (sound == null) {
                        sound = newFluid.isIn(FluidTags.LAVA) ? SoundEvents.ITEM_BUCKET_FILL_LAVA : SoundEvents.ITEM_BUCKET_FILL;
                    }
                    player.playSound(sound, 1.0F, 1.0F);
                    ItemStack newStack = updateBucket(stack, player, withFluid(newFluid));
                    if (!world.isRemote()) {
                        CriteriaTriggers.FILLED_BUCKET.trigger((ServerPlayerEntity) player, newStack.copy());
                    }
                    return ActionResult.resultSuccess(newStack);
                }
            }
        } else {
            BlockPos fluidPos = state.getBlock() instanceof ILiquidContainer && fluid == Fluids.WATER ? pos : offset;
            if (this.tryPlaceContainedLiquid(player, world, fluidPos, stack, trace)) {
                onLiquidPlaced(fluid, world, stack, fluidPos);
                if (player instanceof ServerPlayerEntity) {
                    CriteriaTriggers.PLACED_BLOCK.trigger((ServerPlayerEntity) player, fluidPos, stack);
                }
                player.addStat(Stats.ITEM_USED.get(this));
                return ActionResult.resultSuccess(emptyBucket(stack, player));
            }
        }
    }
    return ActionResult.resultFail(stack);
}
Also used : SoundEvent(net.minecraft.util.SoundEvent) IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) FlowingFluid(net.minecraft.fluid.FlowingFluid) Fluid(net.minecraft.fluid.Fluid) Block(net.minecraft.block.Block) CauldronBlock(net.minecraft.block.CauldronBlock) BlockPos(net.minecraft.util.math.BlockPos) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) ItemStack(net.minecraft.item.ItemStack) Direction(net.minecraft.util.Direction)

Example 2 with ILiquidContainer

use of net.minecraft.block.ILiquidContainer in project Ceramics by KnightMiner.

the class ClayBucketItem method tryPlaceContainedLiquid.

// TODO: possibly migrate to the Forge method
@SuppressWarnings("deprecation")
private boolean tryPlaceContainedLiquid(@Nullable PlayerEntity player, World world, BlockPos pos, ItemStack stack, @Nullable BlockRayTraceResult trace) {
    Fluid fluidStack = this.getFluid(stack);
    Fluid fluid = fluidStack.getFluid();
    if (!(fluid instanceof FlowingFluid)) {
        return false;
    }
    BlockState state = world.getBlockState(pos);
    Block block = state.getBlock();
    boolean replaceable = state.isReplaceable(fluid);
    if (state.isAir(world, pos) || replaceable || block instanceof ILiquidContainer && ((ILiquidContainer) block).canContainFluid(world, pos, state, fluid)) {
        if (world.getDimensionType().isUltrawarm() && fluid.isIn(FluidTags.WATER)) {
            world.playSound(player, pos, SoundEvents.BLOCK_FIRE_EXTINGUISH, SoundCategory.BLOCKS, 0.5F, 2.6F + (world.rand.nextFloat() - world.rand.nextFloat()) * 0.8F);
            for (int l = 0; l < 8; ++l) {
                world.addParticle(ParticleTypes.LARGE_SMOKE, pos.getX() + Math.random(), pos.getY() + Math.random(), pos.getZ() + Math.random(), 0.0D, 0.0D, 0.0D);
            }
        } else if (block instanceof ILiquidContainer && fluid == Fluids.WATER) {
            if (((ILiquidContainer) block).receiveFluid(world, pos, state, ((FlowingFluid) fluid).getStillFluidState(false))) {
                this.playEmptySound(fluid, player, world, pos);
            }
        } else {
            if (!world.isRemote() && replaceable && !state.getMaterial().isLiquid()) {
                world.destroyBlock(pos, true);
            }
            this.playEmptySound(fluid, player, world, pos);
            world.setBlockState(pos, fluid.getDefaultState().getBlockState(), 11);
        }
        return true;
    }
    if (trace == null) {
        return false;
    }
    return this.tryPlaceContainedLiquid(player, world, trace.getPos().offset(trace.getFace()), stack, null);
}
Also used : BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) FlowingFluid(net.minecraft.fluid.FlowingFluid) Fluid(net.minecraft.fluid.Fluid) FlowingFluid(net.minecraft.fluid.FlowingFluid) Block(net.minecraft.block.Block) CauldronBlock(net.minecraft.block.CauldronBlock)

Example 3 with ILiquidContainer

use of net.minecraft.block.ILiquidContainer in project Mekanism by mekanism.

the class WorldUtils method tryPlaceContainedLiquid.

public static boolean tryPlaceContainedLiquid(@Nullable PlayerEntity player, World world, BlockPos pos, @Nonnull FluidStack fluidStack, @Nullable Direction side) {
    Fluid fluid = fluidStack.getFluid();
    if (!fluid.getAttributes().canBePlacedInWorld(world, pos, fluidStack)) {
        // If there is no fluid, or it cannot be placed in the world just
        return false;
    }
    BlockState state = world.getBlockState(pos);
    boolean isReplaceable = state.canBeReplaced(fluid);
    boolean canContainFluid = state.getBlock() instanceof ILiquidContainer && ((ILiquidContainer) state.getBlock()).canPlaceLiquid(world, pos, state, fluid);
    if (state.isAir(world, pos) || isReplaceable || canContainFluid) {
        if (world.dimensionType().ultraWarm() && fluid.getAttributes().doesVaporize(world, pos, fluidStack)) {
            fluid.getAttributes().vaporize(player, world, pos, fluidStack);
        } else if (canContainFluid) {
            if (!((ILiquidContainer) state.getBlock()).placeLiquid(world, pos, state, fluid.getAttributes().getStateForPlacement(world, pos, fluidStack))) {
                // If something went wrong return that we couldn't actually place it
                return false;
            }
            playEmptySound(player, world, pos, fluidStack);
        } else {
            if (!world.isClientSide() && isReplaceable && !state.getMaterial().isLiquid()) {
                world.destroyBlock(pos, true);
            }
            playEmptySound(player, world, pos, fluidStack);
            world.setBlock(pos, fluid.defaultFluidState().createLegacyBlock(), BlockFlags.DEFAULT_AND_RERENDER);
        }
        return true;
    }
    return side != null && tryPlaceContainedLiquid(player, world, pos.relative(side), fluidStack, null);
}
Also used : BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) Fluid(net.minecraft.fluid.Fluid)

Example 4 with ILiquidContainer

use of net.minecraft.block.ILiquidContainer in project Mekanism by mekanism.

the class TileEntityFluidicPlenisher method canReplace.

private boolean canReplace(BlockPos pos, boolean checkNodes, boolean isPathfinding) {
    if (checkNodes && usedNodes.contains(pos)) {
        return false;
    }
    BlockState state = level.getBlockState(pos);
    if (state.isAir(level, pos)) {
        return true;
    }
    FluidState currentFluidState = state.getFluidState();
    if (!currentFluidState.isEmpty()) {
        // There is currently a fluid in the spot
        if (currentFluidState.isSource()) {
            // If it is a source return based on if we are path finding
            return isPathfinding;
        }
        // Always return true if it is not a source block
        return true;
    }
    FluidStack stack = fluidTank.getFluid();
    if (stack.isEmpty()) {
        // If we are empty, base it off of if it is replaceable in general or if it is a liquid container
        return WorldUtils.isValidReplaceableBlock(level, pos) || state.getBlock() instanceof ILiquidContainer;
    }
    Fluid fluid = stack.getFluid();
    if (state.canBeReplaced(fluid)) {
        // If we can replace the block then return so
        return true;
    }
    // Otherwise, just return if it is a liquid container that can support the type of fluid we are offering
    return state.getBlock() instanceof ILiquidContainer && ((ILiquidContainer) state.getBlock()).canPlaceLiquid(level, pos, state, fluid);
}
Also used : BlockState(net.minecraft.block.BlockState) FluidStack(net.minecraftforge.fluids.FluidStack) ILiquidContainer(net.minecraft.block.ILiquidContainer) Fluid(net.minecraft.fluid.Fluid) FluidState(net.minecraft.fluid.FluidState)

Example 5 with ILiquidContainer

use of net.minecraft.block.ILiquidContainer in project MCMOD-Industria by M-Marvin.

the class ItemFluidCannister method use.

@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
    ItemStack cannisterStack = player.getItemInHand(hand);
    FluidStack content = getContent(cannisterStack);
    RayTraceResult raytraceResultFill = getPlayerPOVHitResult(world, player, RayTraceContext.FluidMode.SOURCE_ONLY);
    RayTraceResult raytraceResultEmpty = getPlayerPOVHitResult(world, player, RayTraceContext.FluidMode.NONE);
    // Check for interacting with blocks
    // EmptyRaytrace -> Blocks have priority for filling cannister with sourceblocks
    ActionResult<ItemStack> blockInteractReturn = net.minecraftforge.event.ForgeEventFactory.onBucketUse(player, world, cannisterStack, raytraceResultEmpty);
    if (blockInteractReturn != null)
        return blockInteractReturn;
    // Check if the click has blocked or missed and the cannister can contain more fluid
    if (raytraceResultFill.getType() == Type.MISS) {
        return ActionResult.pass(cannisterStack);
    } else if (raytraceResultFill.getType() != Type.BLOCK) {
        return ActionResult.pass(cannisterStack);
    } else if (TileEntityFluidCannister.MAX_CONTENT - content.getAmount() >= 1000) {
        BlockRayTraceResult blockRaytraceResult = (BlockRayTraceResult) raytraceResultFill;
        BlockPos blockpos = blockRaytraceResult.getBlockPos();
        Direction direction = blockRaytraceResult.getDirection();
        BlockPos blockpos1 = blockpos.relative(direction);
        if (world.mayInteract(player, blockpos) && player.mayUseItemAt(blockpos1, direction, cannisterStack)) {
            BlockState state1 = world.getBlockState(blockpos);
            if (state1.getBlock() instanceof IBucketPickupHandler) {
                Fluid fluid = state1.getFluidState().getType();
                if (fluid != Fluids.EMPTY && (fluid == content.getFluid() || content.isEmpty())) {
                    fluid = ((IBucketPickupHandler) state1.getBlock()).takeLiquid(world, blockpos, state1);
                    if (content.isEmpty()) {
                        content = new FluidStack(fluid, 1000);
                    } else {
                        content.grow(1000);
                    }
                    setContent(cannisterStack, content);
                    SoundEvent fillSound = content.getFluid().getAttributes().getFillSound();
                    if (fillSound == null)
                        fillSound = SoundEvents.BUCKET_FILL;
                    player.playSound(fillSound, 1.0F, 1.0F);
                    return ActionResult.sidedSuccess(cannisterStack, world.isClientSide);
                }
            }
        }
    }
    // Check if the raytrace has blocked or missed and the cannister contains fluid
    if (raytraceResultEmpty.getType() == Type.MISS) {
        return ActionResult.consume(cannisterStack);
    } else if (raytraceResultEmpty.getType() != Type.BLOCK) {
        return ActionResult.consume(cannisterStack);
    } else if (content.getAmount() >= 1000) {
        BlockRayTraceResult blockRaytraceResult = (BlockRayTraceResult) raytraceResultEmpty;
        BlockPos blockpos = blockRaytraceResult.getBlockPos();
        Direction direction = blockRaytraceResult.getDirection();
        BlockPos blockpos1 = blockpos.relative(direction);
        BlockState blockstate = world.getBlockState(blockpos);
        boolean canBlockContain = blockstate.getBlock() instanceof ILiquidContainer && ((ILiquidContainer) blockstate.getBlock()).canPlaceLiquid(world, blockpos, blockstate, content.getFluid());
        BlockPos blockpos2 = canBlockContain ? blockpos : blockpos1;
        if (this.emptyBucket(player, world, blockpos2, blockRaytraceResult, content.getFluid())) {
            content.shrink(1000);
            setContent(cannisterStack, content);
            SoundEvent soundevent = content.getFluid().getAttributes().getEmptySound();
            if (soundevent == null)
                soundevent = SoundEvents.BUCKET_EMPTY;
            world.playSound(player, blockpos2, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
            return ActionResult.sidedSuccess(cannisterStack, world.isClientSide);
        }
    }
    return ActionResult.fail(cannisterStack);
}
Also used : IBucketPickupHandler(net.minecraft.block.IBucketPickupHandler) FluidStack(net.minecraftforge.fluids.FluidStack) FlowingFluid(net.minecraft.fluid.FlowingFluid) Fluid(net.minecraft.fluid.Fluid) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockRayTraceResult(net.minecraft.util.math.BlockRayTraceResult) Direction(net.minecraft.util.Direction) SoundEvent(net.minecraft.util.SoundEvent) BlockState(net.minecraft.block.BlockState) ILiquidContainer(net.minecraft.block.ILiquidContainer) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

BlockState (net.minecraft.block.BlockState)8 ILiquidContainer (net.minecraft.block.ILiquidContainer)8 Block (net.minecraft.block.Block)5 Fluid (net.minecraft.fluid.Fluid)5 BlockRayTraceResult (net.minecraft.util.math.BlockRayTraceResult)5 FlowingFluid (net.minecraft.fluid.FlowingFluid)4 Material (net.minecraft.block.material.Material)3 CauldronBlock (net.minecraft.block.CauldronBlock)2 IBucketPickupHandler (net.minecraft.block.IBucketPickupHandler)2 ItemStack (net.minecraft.item.ItemStack)2 Direction (net.minecraft.util.Direction)2 SoundEvent (net.minecraft.util.SoundEvent)2 BlockPos (net.minecraft.util.math.BlockPos)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 FluidState (net.minecraft.fluid.FluidState)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1