package net.minecraft.world.level.block.piston;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
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.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.PistonType;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.DirectionalBlock;

public class PistonHeadBlock extends DirectionalBlock {
    public static final EnumProperty<PistonType> TYPE;
    public static final BooleanProperty SHORT;
    protected static final VoxelShape EAST_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape SOUTH_AABB;
    protected static final VoxelShape NORTH_AABB;
    protected static final VoxelShape UP_AABB;
    protected static final VoxelShape DOWN_AABB;
    protected static final VoxelShape UP_ARM_AABB;
    protected static final VoxelShape DOWN_ARM_AABB;
    protected static final VoxelShape SOUTH_ARM_AABB;
    protected static final VoxelShape NORTH_ARM_AABB;
    protected static final VoxelShape EAST_ARM_AABB;
    protected static final VoxelShape WEST_ARM_AABB;
    protected static final VoxelShape SHORT_UP_ARM_AABB;
    protected static final VoxelShape SHORT_DOWN_ARM_AABB;
    protected static final VoxelShape SHORT_SOUTH_ARM_AABB;
    protected static final VoxelShape SHORT_NORTH_ARM_AABB;
    protected static final VoxelShape SHORT_EAST_ARM_AABB;
    protected static final VoxelShape SHORT_WEST_ARM_AABB;
    
    public PistonHeadBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)PistonHeadBlock.FACING, Direction.NORTH)).setValue(PistonHeadBlock.TYPE, PistonType.DEFAULT)).<Comparable, Boolean>setValue((Property<Comparable>)PistonHeadBlock.SHORT, false));
    }
    
    private VoxelShape getBaseShape(final BlockState cfj) {
        switch (cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING)) {
            default: {
                return PistonHeadBlock.DOWN_AABB;
            }
            case UP: {
                return PistonHeadBlock.UP_AABB;
            }
            case NORTH: {
                return PistonHeadBlock.NORTH_AABB;
            }
            case SOUTH: {
                return PistonHeadBlock.SOUTH_AABB;
            }
            case WEST: {
                return PistonHeadBlock.WEST_AABB;
            }
            case EAST: {
                return PistonHeadBlock.EAST_AABB;
            }
        }
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState cfj) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return Shapes.or(this.getBaseShape(cfj), this.getArmShape(cfj));
    }
    
    private VoxelShape getArmShape(final BlockState cfj) {
        final boolean boolean3 = cfj.<Boolean>getValue((Property<Boolean>)PistonHeadBlock.SHORT);
        switch (cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING)) {
            default: {
                return boolean3 ? PistonHeadBlock.SHORT_DOWN_ARM_AABB : PistonHeadBlock.DOWN_ARM_AABB;
            }
            case UP: {
                return boolean3 ? PistonHeadBlock.SHORT_UP_ARM_AABB : PistonHeadBlock.UP_ARM_AABB;
            }
            case NORTH: {
                return boolean3 ? PistonHeadBlock.SHORT_NORTH_ARM_AABB : PistonHeadBlock.NORTH_ARM_AABB;
            }
            case SOUTH: {
                return boolean3 ? PistonHeadBlock.SHORT_SOUTH_ARM_AABB : PistonHeadBlock.SOUTH_ARM_AABB;
            }
            case WEST: {
                return boolean3 ? PistonHeadBlock.SHORT_WEST_ARM_AABB : PistonHeadBlock.WEST_ARM_AABB;
            }
            case EAST: {
                return boolean3 ? PistonHeadBlock.SHORT_EAST_ARM_AABB : PistonHeadBlock.EAST_ARM_AABB;
            }
        }
    }
    
    private boolean isFittingBase(final BlockState cfj1, final BlockState cfj2) {
        final Block bvr4 = (cfj1.<PistonType>getValue(PistonHeadBlock.TYPE) == PistonType.DEFAULT) ? Blocks.PISTON : Blocks.STICKY_PISTON;
        return cfj2.is(bvr4) && cfj2.<Boolean>getValue((Property<Boolean>)PistonBaseBlock.EXTENDED) && cfj2.<Comparable>getValue((Property<Comparable>)PistonHeadBlock.FACING) == cfj1.<Comparable>getValue((Property<Comparable>)PistonHeadBlock.FACING);
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        if (!bqb.isClientSide && bec.abilities.instabuild) {
            final BlockPos fu2 = fu.relative(cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite());
            if (this.isFittingBase(cfj, bqb.getBlockState(fu2))) {
                bqb.destroyBlock(fu2, false);
            }
        }
        super.playerWillDestroy(bqb, fu, cfj, bec);
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj1.is(cfj4.getBlock())) {
            return;
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
        final BlockPos fu2 = fu.relative(cfj1.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite());
        if (this.isFittingBase(cfj1, bqb.getBlockState(fu2))) {
            bqb.destroyBlock(fu2, 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.getOpposite() == cfj1.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING) && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockState cfj2 = bqd.getBlockState(fu.relative(cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite()));
        return this.isFittingBase(cfj, cfj2) || (cfj2.is(Blocks.MOVING_PISTON) && cfj2.<Comparable>getValue((Property<Comparable>)PistonHeadBlock.FACING) == cfj.<Comparable>getValue((Property<Comparable>)PistonHeadBlock.FACING));
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (cfj.canSurvive(bqb, fu3)) {
            final BlockPos fu6 = fu3.relative(cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite());
            bqb.getBlockState(fu6).neighborChanged(bqb, fu6, bvr, fu5, false);
        }
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        return new ItemStack((cfj.<PistonType>getValue(PistonHeadBlock.TYPE) == PistonType.STICKY) ? Blocks.STICKY_PISTON : Blocks.PISTON);
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)PistonHeadBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(PistonHeadBlock.FACING, PistonHeadBlock.TYPE, PistonHeadBlock.SHORT);
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        TYPE = BlockStateProperties.PISTON_TYPE;
        SHORT = BlockStateProperties.SHORT;
        EAST_AABB = Block.box(12.0, 0.0, 0.0, 16.0, 16.0, 16.0);
        WEST_AABB = Block.box(0.0, 0.0, 0.0, 4.0, 16.0, 16.0);
        SOUTH_AABB = Block.box(0.0, 0.0, 12.0, 16.0, 16.0, 16.0);
        NORTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 4.0);
        UP_AABB = Block.box(0.0, 12.0, 0.0, 16.0, 16.0, 16.0);
        DOWN_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 4.0, 16.0);
        UP_ARM_AABB = Block.box(6.0, -4.0, 6.0, 10.0, 12.0, 10.0);
        DOWN_ARM_AABB = Block.box(6.0, 4.0, 6.0, 10.0, 20.0, 10.0);
        SOUTH_ARM_AABB = Block.box(6.0, 6.0, -4.0, 10.0, 10.0, 12.0);
        NORTH_ARM_AABB = Block.box(6.0, 6.0, 4.0, 10.0, 10.0, 20.0);
        EAST_ARM_AABB = Block.box(-4.0, 6.0, 6.0, 12.0, 10.0, 10.0);
        WEST_ARM_AABB = Block.box(4.0, 6.0, 6.0, 20.0, 10.0, 10.0);
        SHORT_UP_ARM_AABB = Block.box(6.0, 0.0, 6.0, 10.0, 12.0, 10.0);
        SHORT_DOWN_ARM_AABB = Block.box(6.0, 4.0, 6.0, 10.0, 16.0, 10.0);
        SHORT_SOUTH_ARM_AABB = Block.box(6.0, 6.0, 0.0, 10.0, 10.0, 12.0);
        SHORT_NORTH_ARM_AABB = Block.box(6.0, 6.0, 4.0, 10.0, 10.0, 16.0);
        SHORT_EAST_ARM_AABB = Block.box(0.0, 6.0, 6.0, 12.0, 10.0, 10.0);
        SHORT_WEST_ARM_AABB = Block.box(4.0, 6.0, 6.0, 16.0, 10.0, 10.0);
    }
}
