package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
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.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.Direction;
import net.minecraft.world.level.pathfinder.PathComputationType;
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 net.minecraft.world.level.block.state.BlockBehaviour;
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(((((((StateHolder<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 cfj, final BlockGetter bpg, final BlockPos fu) {
        return this.occlusionByIndex[this.getAABBIndex(cfj)];
    }
    
    @Override
    public VoxelShape getVisualShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return this.getShape(cfj, bpg, fu, der);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    public boolean connectsTo(final BlockState cfj, final boolean boolean2, final Direction fz) {
        final Block bvr5 = cfj.getBlock();
        final boolean boolean3 = this.isSameFence(bvr5);
        final boolean boolean4 = bvr5 instanceof FenceGateBlock && FenceGateBlock.connectsToDirection(cfj, fz);
        return (!Block.isExceptionForConnection(bvr5) && boolean2) || boolean3 || boolean4;
    }
    
    private boolean isSameFence(final Block bvr) {
        return bvr.is(BlockTags.FENCES) && bvr.is(BlockTags.WOODEN_FENCES) == this.defaultBlockState().is(BlockTags.WOODEN_FENCES);
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (!bqb.isClientSide) {
            return LeadItem.bindPlayerMobs(bec, bqb, fu);
        }
        final ItemStack bki8 = bec.getItemInHand(anf);
        if (bki8.getItem() == Items.LEAD) {
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockGetter bpg3 = bin.getLevel();
        final BlockPos fu4 = bin.getClickedPos();
        final FluidState cxa5 = bin.getLevel().getFluidState(bin.getClickedPos());
        final BlockPos fu5 = fu4.north();
        final BlockPos fu6 = fu4.east();
        final BlockPos fu7 = fu4.south();
        final BlockPos fu8 = fu4.west();
        final BlockState cfj10 = bpg3.getBlockState(fu5);
        final BlockState cfj11 = bpg3.getBlockState(fu6);
        final BlockState cfj12 = bpg3.getBlockState(fu7);
        final BlockState cfj13 = bpg3.getBlockState(fu8);
        return ((((((StateHolder<O, BlockState>)super.getStateForPlacement(bin)).setValue((Property<Comparable>)FenceBlock.NORTH, this.connectsTo(cfj10, cfj10.isFaceSturdy(bpg3, fu5, Direction.SOUTH), Direction.SOUTH))).setValue((Property<Comparable>)FenceBlock.EAST, this.connectsTo(cfj11, cfj11.isFaceSturdy(bpg3, fu6, Direction.WEST), Direction.WEST))).setValue((Property<Comparable>)FenceBlock.SOUTH, this.connectsTo(cfj12, cfj12.isFaceSturdy(bpg3, fu7, Direction.NORTH), Direction.NORTH))).setValue((Property<Comparable>)FenceBlock.WEST, this.connectsTo(cfj13, cfj13.isFaceSturdy(bpg3, fu8, Direction.EAST), Direction.EAST))).<Comparable, Boolean>setValue((Property<Comparable>)FenceBlock.WATERLOGGED, cxa5.getType() == Fluids.WATER);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.<Boolean>getValue((Property<Boolean>)FenceBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        if (fz.getAxis().getPlane() == Direction.Plane.HORIZONTAL) {
            return ((StateHolder<O, BlockState>)cfj1).<Comparable, Boolean>setValue((Property<Comparable>)FenceBlock.PROPERTY_BY_DIRECTION.get(fz), this.connectsTo(cfj3, cfj3.isFaceSturdy(bqc, fu6, fz.getOpposite()), fz.getOpposite()));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(FenceBlock.NORTH, FenceBlock.EAST, FenceBlock.WEST, FenceBlock.SOUTH, FenceBlock.WATERLOGGED);
    }
}
