package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.LeadItem;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.Direction;
import net.minecraft.world.level.pathfinder.PathComputationType;
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 net.minecraft.world.phys.shapes.VoxelShape;

public class FenceBlock extends CrossCollisionBlock {
    private final VoxelShape[] occlusionByIndex;
    
    public FenceBlock(final Properties c) {
        super(2.0f, 2.0f, 16.0f, 16.0f, 24.0f, c);
        this.registerDefaultState(((((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)FenceBlock.NORTH, false)).setValue((Property<Comparable>)FenceBlock.EAST, false)).setValue((Property<Comparable>)FenceBlock.SOUTH, false)).setValue((Property<Comparable>)FenceBlock.WEST, false)).<Comparable, Boolean>setValue((Property<Comparable>)FenceBlock.WATERLOGGED, false));
        this.occlusionByIndex = this.makeShapes(2.0f, 1.0f, 16.0f, 6.0f, 15.0f);
    }
    
    @Override
    public VoxelShape getOcclusionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return this.occlusionByIndex[this.getAABBIndex(byj)];
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        return false;
    }
    
    public boolean connectsTo(final BlockState byj, final boolean boolean2, final Direction fp) {
        final Block bph5 = byj.getBlock();
        final boolean boolean3 = bph5.is(BlockTags.FENCES) && byj.getMaterial() == this.material;
        final boolean boolean4 = bph5 instanceof FenceGateBlock && FenceGateBlock.connectsToDirection(byj, fp);
        return (!Block.isExceptionForConnection(bph5) && boolean2) || boolean3 || boolean4;
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (!bjw.isClientSide) {
            return LeadItem.bindPlayerMobs(ayj, bjw, fk);
        }
        final ItemStack ben8 = ayj.getItemInHand(ajk);
        if (ben8.getItem() == Items.LEAD) {
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockGetter bjg3 = bcq.getLevel();
        final BlockPos fk4 = bcq.getClickedPos();
        final FluidState coj5 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        final BlockPos fk5 = fk4.north();
        final BlockPos fk6 = fk4.east();
        final BlockPos fk7 = fk4.south();
        final BlockPos fk8 = fk4.west();
        final BlockState byj10 = bjg3.getBlockState(fk5);
        final BlockState byj11 = bjg3.getBlockState(fk6);
        final BlockState byj12 = bjg3.getBlockState(fk7);
        final BlockState byj13 = bjg3.getBlockState(fk8);
        return ((((((AbstractStateHolder<O, BlockState>)super.getStateForPlacement(bcq)).setValue((Property<Comparable>)FenceBlock.NORTH, this.connectsTo(byj10, byj10.isFaceSturdy(bjg3, fk5, Direction.SOUTH), Direction.SOUTH))).setValue((Property<Comparable>)FenceBlock.EAST, this.connectsTo(byj11, byj11.isFaceSturdy(bjg3, fk6, Direction.WEST), Direction.WEST))).setValue((Property<Comparable>)FenceBlock.SOUTH, this.connectsTo(byj12, byj12.isFaceSturdy(bjg3, fk7, Direction.NORTH), Direction.NORTH))).setValue((Property<Comparable>)FenceBlock.WEST, this.connectsTo(byj13, byj13.isFaceSturdy(bjg3, fk8, Direction.EAST), Direction.EAST))).<Comparable, Boolean>setValue((Property<Comparable>)FenceBlock.WATERLOGGED, coj5.getType() == Fluids.WATER);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Boolean>getValue((Property<Boolean>)FenceBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        if (fp.getAxis().getPlane() == Direction.Plane.HORIZONTAL) {
            return ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)FenceBlock.PROPERTY_BY_DIRECTION.get(fp), this.connectsTo(byj3, byj3.isFaceSturdy(bjx, fk6, fp.getOpposite()), fp.getOpposite()));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(FenceBlock.NORTH, FenceBlock.EAST, FenceBlock.WEST, FenceBlock.SOUTH, FenceBlock.WATERLOGGED);
    }
}
