package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.tileentities.WinchBlockEntity;
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.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ChestMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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.ChainBlock;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.FaceAttachedHorizontalDirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
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.AttachFace;
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.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
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/WinchBlock.class */
public class WinchBlock extends FaceAttachedHorizontalDirectionalBlock implements EntityBlock {
    public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
    protected static final VoxelShape SHAPE_FLOOR_SOUTH = (VoxelShape) Stream.of((Object[]) new VoxelShape[]{Block.box(0.0d, 0.0d, 4.0d, 16.0d, 1.0d, 12.0d), Block.box(4.0d, 1.0d, 5.0d, 14.0d, 2.0d, 6.0d), Block.box(0.5d, 1.0d, 5.0d, 4.0d, 5.0d, 11.0d), Block.box(10.0d, 5.0d, 6.0d, 14.5d, 6.0d, 10.0d), Block.box(3.5d, 5.0d, 10.0d, 14.5d, 6.0d, 11.0d), Block.box(3.5d, 5.0d, 6.0d, 6.0d, 6.0d, 10.0d), Block.box(3.5d, 5.0d, 5.0d, 14.5d, 6.0d, 6.0d), Block.box(14.0d, 1.0d, 5.0d, 15.0d, 5.0d, 11.0d), Block.box(15.0d, 1.0d, 6.0d, 16.0d, 4.0d, 10.0d), Block.box(8.0d, 0.0d, 6.5d, 8.0d, 5.0d, 9.5d), Block.box(6.5d, 0.0d, 8.0d, 9.5d, 5.0d, 8.0d), Block.box(4.0d, 1.0d, 10.0d, 14.0d, 2.0d, 11.0d), Block.box(6.0d, 5.0d, 6.0d, 10.0d, 6.0d, 10.0d), Block.box(4.75d, 1.0d, 8.0d, 7.75d, 6.0d, 8.0d), Block.box(6.603553390593274d, 1.0d, 6.353553390593273d, 6.603553390593274d, 6.0d, 9.353553390593273d), Block.box(7.0732233047033635d, 1.0d, 12.75d, 10.073223304703363d, 6.0d, 12.75d), Block.box(8.926776695296637d, 1.0d, 11.103553390593273d, 8.926776695296637d, 6.0d, 14.103553390593273d), Block.box(9.75d, 1.0d, 13.0d, 12.75d, 6.0d, 13.0d), Block.box(11.603553390593273d, 1.0d, 11.353553390593273d, 11.603553390593273d, 6.0d, 14.353553390593273d)}).reduce((v1, v2) -> {
        return Shapes.join((VoxelShape) v1, (VoxelShape) v2, BooleanOp.OR);
    }).get();
    protected final VoxelShape SHAPE_FLOOR_WEST;
    protected final VoxelShape SHAPE_FLOOR_NORTH;
    protected final VoxelShape SHAPE_FLOOR_EAST;
    protected final VoxelShape SHAPE_WALL_SOUTH;
    protected final VoxelShape SHAPE_WALL_WEST;
    protected final VoxelShape SHAPE_WALL_NORTH;
    protected final VoxelShape SHAPE_WALL_EAST;
    protected final VoxelShape SHAPE_CEILING_SOUTH;
    protected final VoxelShape SHAPE_CEILING_WEST;
    protected final VoxelShape SHAPE_CEILING_NORTH;
    protected final VoxelShape SHAPE_CEILING_EAST;

    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack itemStack, @Nullable BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag) {
        list.add(new TextComponent("Requires redstone power to operate"));
    }

    public WinchBlock(Properties props) {
        super(props);
        this.SHAPE_FLOOR_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_FLOOR_SOUTH);
        this.SHAPE_FLOOR_NORTH = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_FLOOR_WEST);
        this.SHAPE_FLOOR_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_FLOOR_NORTH);
        this.SHAPE_WALL_SOUTH = ModernLifeUtil.RotateVoxelShapeXAxis(SHAPE_FLOOR_SOUTH);
        this.SHAPE_WALL_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_WALL_SOUTH);
        this.SHAPE_WALL_NORTH = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_WALL_WEST);
        this.SHAPE_WALL_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_WALL_NORTH);
        this.SHAPE_CEILING_SOUTH = ModernLifeUtil.RotateVoxelShapeXAxis(this.SHAPE_WALL_SOUTH);
        this.SHAPE_CEILING_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_CEILING_SOUTH);
        this.SHAPE_CEILING_NORTH = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_CEILING_WEST);
        this.SHAPE_CEILING_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(this.SHAPE_CEILING_NORTH);
        registerDefaultState((BlockState) ((BlockState) ((BlockState) this.stateDefinition.any().setValue(FaceAttachedHorizontalDirectionalBlock.FACE, AttachFace.CEILING)).setValue(HorizontalDirectionalBlock.FACING, Direction.NORTH)).setValue(POWERED, false));
    }

    public BlockState rotate(BlockState state, Rotation rotation) {
        return state;
    }

    public BlockState mirror(BlockState state, Mirror mirror) {
        return state;
    }

    public static int getLightLevel(BlockState state) {
        if (((Boolean) state.getValue(POWERED)).booleanValue()) {
            return 15;
        }
        return 0;
    }

    public BlockState getStateForPlacement(BlockPlaceContext context) {
        BlockPos pos = context.getClickedPos();
        boolean signal = context.getLevel().hasNeighborSignal(pos);
        BlockState superState = super.getStateForPlacement(context);
        if (superState == null) {
            superState = defaultBlockState();
        }
        return (BlockState) superState.setValue(POWERED, Boolean.valueOf(signal));
    }

    public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos neighborPos, boolean p_220069_6_) {
        boolean signal = world.hasNeighborSignal(pos);
        if (block != this && signal != ((Boolean) state.getValue(POWERED)).booleanValue()) {
            world.setBlock(pos, (BlockState) state.setValue(POWERED, Boolean.valueOf(signal)), 2);
        }
    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(new Property[]{FaceAttachedHorizontalDirectionalBlock.FACE, HorizontalDirectionalBlock.FACING, POWERED});
    }

    public MenuProvider getMenuProvider(BlockState state, Level level, BlockPos blockPos) {
        return new MenuProvider() { // from class: com.dairymoose.modernlife.blocks.WinchBlock.1
            public AbstractContainerMenu createMenu(int paramInt, Inventory paramInventory, Player paramPlayer) {
                BlockEntity tileEntity = level.getBlockEntity(blockPos);
                if (tileEntity instanceof WinchBlockEntity) {
                    WinchBlockEntity winchBlockEntity = (WinchBlockEntity) tileEntity;
                    ChestMenu chest = new ChestMenu(MenuType.GENERIC_9x1, paramInt, paramInventory, winchBlockEntity, 1);
                    if (!paramPlayer.level.isClientSide) {
                    }
                    return chest;
                }
                return null;
            }

            public Component getDisplayName() {
                return new TextComponent("Winch");
            }
        };
    }

    public void onRemove(BlockState p_51538_, Level p_51539_, BlockPos p_51540_, BlockState p_51541_, boolean p_51542_) {
        if (!p_51538_.is(p_51541_.getBlock())) {
            Container blockEntity = (Container) p_51539_.getBlockEntity(p_51540_);
            if (blockEntity != null) {
                Containers.dropContents(p_51539_, p_51540_, blockEntity);
                p_51539_.updateNeighbourForOutputSignal(p_51540_, this);
            }
            super.onRemove(p_51538_, p_51539_, p_51540_, p_51541_, p_51542_);
        }
    }

    public InteractionResult use(BlockState blockState, Level world, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult rayTrace) {
        int chainSlot;
        if (!world.isClientSide) {
            ItemStack itemStack = player.getItemInHand(hand);
            if (itemStack.is(Items.CHAIN)) {
                BlockEntity blockEntity = world.getBlockEntity(blockPos);
                if (blockEntity instanceof WinchBlockEntity) {
                    WinchBlockEntity winch = (WinchBlockEntity) blockEntity;
                    if (winch.canExpandChain()) {
                        BlockPos pos = winch.getExpandedChainBlockPos();
                        if (world.getBlockState(pos).isAir()) {
                            itemStack.shrink(1);
                            AttachFace face = blockEntity.getBlockState().getValue(FaceAttachedHorizontalDirectionalBlock.FACE);
                            Direction facing = blockEntity.getBlockState().getValue(HorizontalDirectionalBlock.FACING);
                            if (face == AttachFace.CEILING || face == AttachFace.FLOOR) {
                                world.setBlock(pos, (BlockState) Blocks.CHAIN.defaultBlockState().setValue(ChainBlock.AXIS, Direction.Axis.Y), 2);
                            } else if (face == AttachFace.WALL && (facing == Direction.EAST || facing == Direction.WEST)) {
                                world.setBlock(pos, (BlockState) Blocks.CHAIN.defaultBlockState().setValue(ChainBlock.AXIS, Direction.Axis.X), 2);
                            } else {
                                world.setBlock(pos, (BlockState) Blocks.CHAIN.defaultBlockState().setValue(ChainBlock.AXIS, Direction.Axis.Z), 2);
                            }
                        }
                        world.setBlockAndUpdate(blockPos, blockState);
                        ((ServerLevel) world).sendBlockUpdated(blockPos, blockState, blockState, 2);
                    }
                }
            } else if (player.isSteppingCarefully()) {
                BlockEntity blockEntity2 = world.getBlockEntity(blockPos);
                if (blockEntity2 instanceof WinchBlockEntity) {
                    WinchBlockEntity winch2 = (WinchBlockEntity) blockEntity2;
                    if (winch2.getInventoryChainCount() > 0 && (chainSlot = winch2.getNextChainSlot()) != -1) {
                        ItemStack chainItem = winch2.getItem(chainSlot);
                        ItemStack copiedChain = chainItem.copy();
                        copiedChain.setCount(1);
                        chainItem.shrink(1);
                        player.addItem(copiedChain);
                    }
                }
            }
        }
        return InteractionResult.CONSUME;
    }

    /* renamed from: com.dairymoose.modernlife.blocks.WinchBlock$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/WinchBlock$2.class */
    static /* synthetic */ class C00712 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction;

        /* renamed from: $SwitchMap$net$minecraft$world$level$block$state$properties$AttachFace */
        static final /* synthetic */ int[] f5x6a19ab39 = new int[AttachFace.values().length];

        static {
            try {
                f5x6a19ab39[AttachFace.CEILING.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                f5x6a19ab39[AttachFace.WALL.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                f5x6a19ab39[AttachFace.FLOOR.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 1;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 2;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 3;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 4;
            } catch (NoSuchFieldError e7) {
            }
        }
    }

    /*  JADX ERROR: JadxRuntimeException in pass: RegionMakerVisitor
        jadx.core.utils.exceptions.JadxRuntimeException: Failed to find switch 'out' block (already processed)
        	at jadx.core.dex.visitors.regions.RegionMaker.calcSwitchOut(RegionMaker.java:923)
        	at jadx.core.dex.visitors.regions.RegionMaker.processSwitch(RegionMaker.java:797)
        	at jadx.core.dex.visitors.regions.RegionMaker.traverse(RegionMaker.java:157)
        	at jadx.core.dex.visitors.regions.RegionMaker.makeRegion(RegionMaker.java:91)
        	at jadx.core.dex.visitors.regions.RegionMaker.processFallThroughCases(RegionMaker.java:841)
        	at jadx.core.dex.visitors.regions.RegionMaker.processSwitch(RegionMaker.java:800)
        	at jadx.core.dex.visitors.regions.RegionMaker.traverse(RegionMaker.java:157)
        	at jadx.core.dex.visitors.regions.RegionMaker.makeRegion(RegionMaker.java:91)
        	at jadx.core.dex.visitors.regions.RegionMakerVisitor.visit(RegionMakerVisitor.java:52)
        */
    /* JADX WARN: Failed to find 'out' block for switch in B:2:0x0011. Please report as an issue. */
    public VoxelShape getShape(BlockState r5, BlockGetter r6, BlockPos r7, net.minecraft.world.phys.shapes.CollisionContext r8) {
        /*
            r4 = this;
            int[] r0 = com.dairymoose.modernlife.blocks.WinchBlock.C00712.f5x6a19ab39
            r1 = r5
            net.minecraft.world.level.block.state.properties.EnumProperty r2 = com.dairymoose.modernlife.blocks.WinchBlock.FACE
            java.lang.Comparable r1 = r1.getValue(r2)
            net.minecraft.world.level.block.state.properties.AttachFace r1 = (net.minecraft.world.level.block.state.properties.AttachFace) r1
            int r1 = r1.ordinal()
            r0 = r0[r1]
            switch(r0) {
                case 1: goto L2c;
                case 2: goto L70;
                case 3: goto Lb4;
                default: goto Lf7;
            }
        L2c:
            int[] r0 = com.dairymoose.modernlife.blocks.WinchBlock.C00712.$SwitchMap$net$minecraft$core$Direction
            r1 = r5
            net.minecraft.world.level.block.state.properties.DirectionProperty r2 = com.dairymoose.modernlife.blocks.WinchBlock.FACING
            java.lang.Comparable r1 = r1.getValue(r2)
            net.minecraft.core.Direction r1 = (net.minecraft.core.Direction) r1
            int r1 = r1.ordinal()
            r0 = r0[r1]
            switch(r0) {
                case 1: goto L5c;
                case 2: goto L61;
                case 3: goto L66;
                case 4: goto L6b;
                default: goto L70;
            }
        L5c:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_CEILING_NORTH
            return r0
        L61:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_CEILING_EAST
            return r0
        L66:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_CEILING_WEST
            return r0
        L6b:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_CEILING_SOUTH
            return r0
        L70:
            int[] r0 = com.dairymoose.modernlife.blocks.WinchBlock.C00712.$SwitchMap$net$minecraft$core$Direction
            r1 = r5
            net.minecraft.world.level.block.state.properties.DirectionProperty r2 = com.dairymoose.modernlife.blocks.WinchBlock.FACING
            java.lang.Comparable r1 = r1.getValue(r2)
            net.minecraft.core.Direction r1 = (net.minecraft.core.Direction) r1
            int r1 = r1.ordinal()
            r0 = r0[r1]
            switch(r0) {
                case 1: goto La0;
                case 2: goto La5;
                case 3: goto Laa;
                case 4: goto Laf;
                default: goto Lb4;
            }
        La0:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_WALL_NORTH
            return r0
        La5:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_WALL_EAST
            return r0
        Laa:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_WALL_WEST
            return r0
        Laf:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_WALL_SOUTH
            return r0
        Lb4:
            int[] r0 = com.dairymoose.modernlife.blocks.WinchBlock.C00712.$SwitchMap$net$minecraft$core$Direction
            r1 = r5
            net.minecraft.world.level.block.state.properties.DirectionProperty r2 = com.dairymoose.modernlife.blocks.WinchBlock.FACING
            java.lang.Comparable r1 = r1.getValue(r2)
            net.minecraft.core.Direction r1 = (net.minecraft.core.Direction) r1
            int r1 = r1.ordinal()
            r0 = r0[r1]
            switch(r0) {
                case 1: goto Le4;
                case 2: goto Le9;
                case 3: goto Lee;
                case 4: goto Lf3;
                default: goto Lf7;
            }
        Le4:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_FLOOR_NORTH
            return r0
        Le9:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_FLOOR_EAST
            return r0
        Lee:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_FLOOR_WEST
            return r0
        Lf3:
            net.minecraft.world.phys.shapes.VoxelShape r0 = com.dairymoose.modernlife.blocks.WinchBlock.SHAPE_FLOOR_SOUTH
            return r0
        Lf7:
            r0 = r4
            net.minecraft.world.phys.shapes.VoxelShape r0 = r0.SHAPE_FLOOR_NORTH
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.dairymoose.modernlife.blocks.WinchBlock.getShape(net.minecraft.world.level.block.state.BlockState, net.minecraft.world.level.BlockGetter, net.minecraft.core.BlockPos, net.minecraft.world.phys.shapes.CollisionContext):net.minecraft.world.phys.shapes.VoxelShape");
    }

    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> type) {
        return ModernLifeUtil.createTickerHelper(type, WinchBlockEntity.WINCH, WinchBlockEntity::tick);
    }

    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        WinchBlockEntity toReturn = (WinchBlockEntity) WinchBlockEntity.WINCH.create(pos, state);
        return toReturn;
    }
}
