package net.minecraft.world.level.block;

import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.Level;
import java.util.Optional;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockBehaviour;

public abstract class GrowingPlantBodyBlock extends GrowingPlantBlock implements BonemealableBlock {
    protected GrowingPlantBodyBlock(final Properties c, final Direction fz, final VoxelShape dfg, final boolean boolean4) {
        super(c, fz, dfg, boolean4);
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.canSurvive(zd, fu)) {
            zd.destroyBlock(fu, true);
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz == this.growthDirection.getOpposite() && !cfj1.canSurvive(bqc, fu5)) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        final GrowingPlantHeadBlock byk8 = this.getHeadBlock();
        if (fz == this.growthDirection) {
            final Block bvr9 = cfj3.getBlock();
            if (bvr9 != this && bvr9 != byk8) {
                return byk8.getStateForPlacement(bqc);
            }
        }
        if (this.scheduleFluidTicks) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        return new ItemStack(this.getHeadBlock());
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        final Optional<BlockPos> optional6 = this.getHeadPos(bpg, fu, cfj);
        return optional6.isPresent() && this.getHeadBlock().canGrowInto(bpg.getBlockState(optional6.get().relative(this.growthDirection)));
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bqb, final Random random, final BlockPos fu, final BlockState cfj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        final Optional<BlockPos> optional6 = this.getHeadPos(zd, fu, cfj);
        if (optional6.isPresent()) {
            final BlockState cfj2 = zd.getBlockState(optional6.get());
            ((GrowingPlantHeadBlock)cfj2.getBlock()).performBonemeal(zd, random, optional6.get(), cfj2);
        }
    }
    
    private Optional<BlockPos> getHeadPos(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        BlockPos fu2 = fu;
        Block bvr6;
        do {
            fu2 = fu2.relative(this.growthDirection);
            bvr6 = bpg.getBlockState(fu2).getBlock();
        } while (bvr6 == cfj.getBlock());
        if (bvr6 == this.getHeadBlock()) {
            return Optional.<BlockPos>of(fu2);
        }
        return Optional.<BlockPos>empty();
    }
    
    @Override
    public boolean canBeReplaced(final BlockState cfj, final BlockPlaceContext bin) {
        final boolean boolean4 = super.canBeReplaced(cfj, bin);
        return (!boolean4 || bin.getItemInHand().getItem() != this.getHeadBlock().asItem()) && boolean4;
    }
    
    @Override
    protected Block getBodyBlock() {
        return this;
    }
}
