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

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.level.block.Blocks;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
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.Block;
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(((((AbstractStateHolder<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 byj) {
        switch (byj.<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 byj) {
        return true;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return Shapes.or(this.getBaseShape(byj), this.getArmShape(byj));
    }
    
    private VoxelShape getArmShape(final BlockState byj) {
        final boolean boolean3 = byj.<Boolean>getValue((Property<Boolean>)PistonHeadBlock.SHORT);
        switch (byj.<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;
            }
        }
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        if (!bjw.isClientSide && ayj.abilities.instabuild) {
            final BlockPos fk2 = fk.relative(byj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite());
            final Block bph7 = bjw.getBlockState(fk2).getBlock();
            if (bph7 == Blocks.PISTON || bph7 == Blocks.STICKY_PISTON) {
                bjw.removeBlock(fk2, false);
            }
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
        final Direction fp7 = byj1.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite();
        fk = fk.relative(fp7);
        final BlockState byj5 = bjw.getBlockState(fk);
        if ((byj5.getBlock() == Blocks.PISTON || byj5.getBlock() == Blocks.STICKY_PISTON) && byj5.<Boolean>getValue((Property<Boolean>)PistonBaseBlock.EXTENDED)) {
            Block.dropResources(byj5, bjw, fk);
            bjw.removeBlock(fk, false);
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp.getOpposite() == byj1.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING) && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final Block bph5 = bjz.getBlockState(fk.relative(byj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite())).getBlock();
        return bph5 == Blocks.PISTON || bph5 == Blocks.STICKY_PISTON || bph5 == Blocks.MOVING_PISTON;
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (byj.canSurvive(bjw, fk3)) {
            final BlockPos fk6 = fk3.relative(byj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING).getOpposite());
            bjw.getBlockState(fk6).neighborChanged(bjw, fk6, bph, fk5, false);
        }
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        return new ItemStack((byj.<PistonType>getValue(PistonHeadBlock.TYPE) == PistonType.STICKY) ? Blocks.STICKY_PISTON : Blocks.PISTON);
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)PistonHeadBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)PistonHeadBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<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 byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        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);
    }
}
