package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.util.ModernLifeUtil;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/TurntableBlock.class */
public class TurntableBlock extends StandardHorizontalBlock {
    protected static final VoxelShape SHAPE_NORTH = (VoxelShape) Stream.of((Object[]) new VoxelShape[]{Block.box(8.7d, 0.0d, 7.585786437626905d, 9.0d, 15.0d, 8.414213562373096d), Block.box(6.408701060962736d, 15.0d, 0.0d, 9.591298939037264d, 16.0d, 7.0d), Block.box(6.408701060962736d, 15.0d, 0.0d, 9.591298939037264d, 15.975000000000001d, 7.0d), Block.box(6.408701060962736d, 15.0d, 0.0d, 9.591298939037264d, 16.0d, 7.0d), Block.box(6.408701060962736d, 15.0d, 0.0d, 9.591298939037264d, 15.975000000000001d, 7.0d), Block.box(6.408701060962736d, 15.0d, 0.0d, 9.591298939037264d, 16.0d, 7.0d), Block.box(6.408701060962736d, 15.0d, 9.0d, 9.591298939037264d, 16.0d, 16.0d), Block.box(6.408701060962736d, 15.0d, 9.0d, 9.591298939037264d, 15.975000000000001d, 16.0d), Block.box(6.408701060962736d, 15.0d, 9.0d, 9.591298939037264d, 16.0d, 16.0d), Block.box(6.408701060962736d, 15.0d, 9.0d, 9.591298939037264d, 15.975000000000001d, 16.0d), Block.box(6.408701060962736d, 15.0d, 9.0d, 9.591298939037264d, 16.0d, 16.0d), Block.box(0.0d, 15.0d, 6.408701060962736d, 7.0d, 15.975000000000001d, 9.591298939037264d), Block.box(0.0d, 15.0d, 6.408701060962736d, 7.0d, 16.0d, 9.591298939037264d), Block.box(0.0d, 15.0d, 6.408701060962736d, 7.0d, 15.975000000000001d, 9.591298939037264d), Block.box(9.0d, 15.0d, 6.408701060962736d, 16.0d, 15.975000000000001d, 9.591298939037264d), Block.box(9.0d, 15.0d, 6.408701060962736d, 16.0d, 16.0d, 9.591298939037264d), Block.box(9.0d, 15.0d, 6.408701060962736d, 16.0d, 15.975000000000001d, 9.591298939037264d), Block.box(4.5d, 15.5d, 4.5d, 11.5d, 16.002d, 11.5d), Block.box(7.585786437626905d, 0.0d, 7.0d, 8.414213562373096d, 15.0d, 7.3d), Block.box(7.585786437626905d, 0.0d, 7.0d, 8.414213562373096d, 15.0d, 7.3d), Block.box(7.585786437626905d, 0.0d, 8.7d, 8.414213562373096d, 15.0d, 9.0d), Block.box(7.585786437626905d, 0.0d, 8.7d, 8.414213562373096d, 15.0d, 9.0d), Block.box(7.0d, 0.0d, 7.585786437626905d, 7.3d, 15.0d, 8.414213562373096d), Block.box(7.0d, 0.0d, 7.585786437626905d, 7.3d, 15.0d, 8.414213562373096d), Block.box(8.7d, 0.0d, 7.585786437626905d, 9.0d, 15.0d, 8.414213562373096d)}).reduce((v1, v2) -> {
        return Shapes.join((VoxelShape) v1, (VoxelShape) v2, BooleanOp.OR);
    }).get();
    protected static final VoxelShape SHAPE_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH);
    protected static final VoxelShape SHAPE_SOUTH = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_EAST);
    protected static final VoxelShape SHAPE_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH);
    public static final BooleanProperty POWERED = BlockStateProperties.POWERED;

    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack itemStack, @Nullable BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag) {
        list.add(new TextComponent("Rotates clockwise when powered by redstone"));
    }

    public TurntableBlock(Properties p_i48377_1_) {
        super(p_i48377_1_);
    }

    public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos neighborPos, boolean p_220069_6_) {
        Direction currentFacing;
        boolean signal = world.hasNeighborSignal(pos);
        Boolean currentPoweredState = (Boolean) state.getValue(POWERED);
        if (block != this && signal != currentPoweredState.booleanValue()) {
            BlockState newFacing = state;
            if (signal) {
                Direction turntableFacing = state.getValue(BlockStateProperties.HORIZONTAL_FACING);
                newFacing = (BlockState) state.setValue(BlockStateProperties.HORIZONTAL_FACING, turntableFacing.getClockWise());
                BlockPos abovePos = pos.above();
                BlockState aboveState = world.getBlockState(abovePos);
                if (aboveState.is(Blocks.PISTON)) {
                    world.setBlock(abovePos, (BlockState) aboveState.setValue(BlockStateProperties.EXTENDED, false), 2);
                }
                if (aboveState.getOptionalValue(BlockStateProperties.HORIZONTAL_FACING).isPresent()) {
                    aboveState = (BlockState) aboveState.setValue(BlockStateProperties.HORIZONTAL_FACING, aboveState.getValue(BlockStateProperties.HORIZONTAL_FACING).getClockWise());
                    world.setBlock(abovePos, aboveState, 2);
                }
                if (aboveState.getOptionalValue(BlockStateProperties.FACING).isPresent()) {
                    if (aboveState.getValue(BlockStateProperties.FACING) == Direction.UP) {
                        world.setBlock(abovePos, (BlockState) aboveState.setValue(BlockStateProperties.FACING, Direction.DOWN), 2);
                    } else if (aboveState.getValue(BlockStateProperties.FACING) == Direction.DOWN) {
                        world.setBlock(abovePos, (BlockState) aboveState.setValue(BlockStateProperties.FACING, Direction.UP), 2);
                    } else {
                        aboveState = (BlockState) aboveState.setValue(BlockStateProperties.FACING, aboveState.getValue(BlockStateProperties.FACING).getClockWise());
                        world.setBlock(abovePos, aboveState, 2);
                    }
                }
                if (aboveState.getOptionalValue(BlockStateProperties.FACING_HOPPER).isPresent() && (currentFacing = aboveState.getValue(BlockStateProperties.FACING_HOPPER)) != Direction.DOWN) {
                    world.setBlock(abovePos, (BlockState) aboveState.setValue(BlockStateProperties.FACING_HOPPER, currentFacing.getClockWise()), 2);
                }
                List<Entity> aboveEntities = world.getEntities((Entity) null, AABB.ofSize(new Vec3(abovePos.getX() + 0.5d, abovePos.getY() + 0.5d, abovePos.getZ() + 0.5d), 1.0d, 1.0d, 1.0d));
                if (aboveEntities != null && aboveEntities.size() > 0) {
                    for (Entity entity : aboveEntities) {
                        double centX = abovePos.getX() + 0.5d;
                        double centY = abovePos.getY();
                        double centZ = abovePos.getZ() + 0.5d;
                        double distToCenter = Math.sqrt(entity.distanceToSqr(centX, centY, centZ));
                        double xDiff = entity.getX() - centX;
                        double zDiff = entity.getZ() - centZ;
                        double entityAngle = (Math.toDegrees(Math.atan2(zDiff, xDiff)) + 90.0d) % 360.0d;
                        entity.setYRot(entity.rotate(Rotation.CLOCKWISE_90) % 360.0f);
                        double newX = centX + (distToCenter * Math.cos(Math.toRadians(entityAngle)));
                        double newZ = centZ + (distToCenter * Math.sin(Math.toRadians(entityAngle)));
                        entity.teleportTo(newX, entity.getY(), newZ);
                    }
                }
            }
            world.setBlock(pos, (BlockState) newFacing.setValue(POWERED, Boolean.valueOf(signal)), 2);
        }
    }

    @Override // com.dairymoose.modernlife.blocks.StandardHorizontalBlock
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(new Property[]{HorizontalDirectionalBlock.FACING, POWERED});
    }

    @Override // com.dairymoose.modernlife.blocks.StandardHorizontalBlock
    @Nullable
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return (BlockState) super.getStateForPlacement(context).setValue(POWERED, Boolean.valueOf(context.getLevel().hasNeighborSignal(context.getClickedPos())));
    }

    /* renamed from: com.dairymoose.modernlife.blocks.TurntableBlock$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/TurntableBlock$1.class */
    static /* synthetic */ class C00661 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];

        static {
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public VoxelShape getShape(BlockState blockState, BlockGetter p_220053_2_, BlockPos p_220053_3_, CollisionContext p_220053_4_) {
        switch (C00661.$SwitchMap$net$minecraft$core$Direction[blockState.getValue(HorizontalDirectionalBlock.FACING).ordinal()]) {
            case 1:
                return SHAPE_NORTH;
            case 2:
                return SHAPE_NORTH;
            case 3:
                return SHAPE_NORTH;
            case 4:
                return SHAPE_NORTH;
            default:
                return SHAPE_NORTH;
        }
    }
}
