package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import java.util.Collections;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.core.Direction;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import com.google.common.collect.Lists;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.material.FluidState;
import java.util.List;
import net.minecraft.world.level.material.FlowingFluid;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class LiquidBlock extends Block implements BucketPickup {
    public static final IntegerProperty LEVEL;
    protected final FlowingFluid fluid;
    private final List<FluidState> stateCache;
    public static final VoxelShape STABLE_SHAPE;
    
    protected LiquidBlock(final FlowingFluid cwy, final Properties c) {
        super(c);
        this.fluid = cwy;
        (this.stateCache = Lists.newArrayList()).add(cwy.getSource(false));
        for (int integer4 = 1; integer4 < 8; ++integer4) {
            this.stateCache.add(cwy.getFlowing(8 - integer4, false));
        }
        this.stateCache.add(cwy.getFlowing(8, true));
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)LiquidBlock.LEVEL, 0));
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (der.isAbove(LiquidBlock.STABLE_SHAPE, fu, true) && cfj.<Integer>getValue((Property<Integer>)LiquidBlock.LEVEL) == 0 && der.canStandOnFluid(bpg.getFluidState(fu.above()), this.fluid)) {
            return LiquidBlock.STABLE_SHAPE;
        }
        return Shapes.empty();
    }
    
    @Override
    public boolean isRandomlyTicking(final BlockState cfj) {
        return cfj.getFluidState().isRandomlyTicking();
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        cfj.getFluidState().randomTick(zd, fu, random);
    }
    
    @Override
    public boolean propagatesSkylightDown(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return false;
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return !this.fluid.is(FluidTags.LAVA);
    }
    
    @Override
    public FluidState getFluidState(final BlockState cfj) {
        final int integer3 = cfj.<Integer>getValue((Property<Integer>)LiquidBlock.LEVEL);
        return this.stateCache.get(Math.min(integer3, 8));
    }
    
    @Override
    public boolean skipRendering(final BlockState cfj1, final BlockState cfj2, final Direction fz) {
        return cfj2.getFluidState().getType().isSame(this.fluid);
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.INVISIBLE;
    }
    
    @Override
    public List<ItemStack> getDrops(final BlockState cfj, final LootContext.Builder a) {
        return Collections.<ItemStack>emptyList();
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return Shapes.empty();
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (this.shouldSpreadLiquid(bqb, fu, cfj1)) {
            bqb.getLiquidTicks().scheduleTick(fu, cfj1.getFluidState().getType(), this.fluid.getTickDelay(bqb));
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.getFluidState().isSource() || cfj3.getFluidState().isSource()) {
            bqc.getLiquidTicks().scheduleTick(fu5, cfj1.getFluidState().getType(), this.fluid.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (this.shouldSpreadLiquid(bqb, fu3, cfj)) {
            bqb.getLiquidTicks().scheduleTick(fu3, cfj.getFluidState().getType(), this.fluid.getTickDelay(bqb));
        }
    }
    
    private boolean shouldSpreadLiquid(final Level bqb, final BlockPos fu, final BlockState cfj) {
        if (this.fluid.is(FluidTags.LAVA)) {
            final boolean boolean5 = bqb.getBlockState(fu.below()).is(Blocks.SOUL_SOIL);
            for (final Direction fz9 : Direction.values()) {
                if (fz9 != Direction.DOWN) {
                    final BlockPos fu2 = fu.relative(fz9);
                    if (bqb.getFluidState(fu2).is(FluidTags.WATER)) {
                        final Block bvr11 = bqb.getFluidState(fu).isSource() ? Blocks.OBSIDIAN : Blocks.COBBLESTONE;
                        bqb.setBlockAndUpdate(fu, bvr11.defaultBlockState());
                        this.fizz(bqb, fu);
                        return false;
                    }
                    if (boolean5 && bqb.getBlockState(fu2).is(Blocks.BLUE_ICE)) {
                        bqb.setBlockAndUpdate(fu, Blocks.BASALT.defaultBlockState());
                        this.fizz(bqb, fu);
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    private void fizz(final LevelAccessor bqc, final BlockPos fu) {
        bqc.levelEvent(1501, fu, 0);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LiquidBlock.LEVEL);
    }
    
    @Override
    public Fluid takeLiquid(final LevelAccessor bqc, final BlockPos fu, final BlockState cfj) {
        if (cfj.<Integer>getValue((Property<Integer>)LiquidBlock.LEVEL) == 0) {
            bqc.setBlock(fu, Blocks.AIR.defaultBlockState(), 11);
            return this.fluid;
        }
        return Fluids.EMPTY;
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        if (this.fluid.is(FluidTags.LAVA)) {
            final float float6 = fu.getY() + cfj.getFluidState().getHeight(bqb, fu);
            final AABB deg7 = aom.getBoundingBox();
            if (deg7.minY < float6 || float6 > deg7.maxY) {
                aom.setInLava();
            }
        }
    }
    
    static {
        LEVEL = BlockStateProperties.LEVEL;
        STABLE_SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0);
    }
}
