package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.tileentities.StoveBlockEntity;
import com.dairymoose.modernlife.util.ModernLifeUtil;
import java.util.Optional;
import java.util.stream.Stream;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
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.EntityBlock;
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.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.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StoveBlock.class */
public class StoveBlock extends StandardHorizontalBlock implements EntityBlock {
    protected static final VoxelShape SHAPE_NORTH = (VoxelShape) Stream.of((Object[]) new VoxelShape[]{Block.box(0.0d, 12.75d, 0.0d, 16.0d, 16.0d, 1.5d), Block.box(0.0d, 0.0d, 0.0d, 16.0d, 12.75d, 15.5d)}).reduce((v1, v2) -> {
        return Shapes.join(v1, 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 OPEN_DOOR = BooleanProperty.create("open_door");

    public StoveBlock(Properties p_i48377_1_) {
        super(p_i48377_1_);
        registerDefaultState((BlockState) super.defaultBlockState().setValue(OPEN_DOOR, false));
    }

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

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

    public void onRemove(BlockState newBlockState, Level world, BlockPos pos, BlockState oldBlockState, boolean b) {
        if (!newBlockState.is(oldBlockState.getBlock())) {
            BlockEntity tileEntity = world.getBlockEntity(pos);
            if (tileEntity instanceof StoveBlockEntity) {
                Containers.dropContents(world, pos, (StoveBlockEntity) tileEntity);
                world.updateNeighbourForOutputSignal(pos, this);
            }
            super.onRemove(newBlockState, world, pos, oldBlockState, b);
        }
    }

    public static int calculateTime(Optional<CampfireCookingRecipe> optionalCookingRecipe) {
        if (optionalCookingRecipe.isPresent()) {
            return (int) (optionalCookingRecipe.get().m_43753_() / 1.5f);
        }
        return 0;
    }

    public InteractionResult use(BlockState blockState, Level world, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult rayTrace) {
        int slot;
        if (!world.isClientSide) {
            if (blockState.is(CustomBlocks.BLOCK_STOVE.get()) && !((Boolean) blockState.getValue(OPEN_DOOR)).booleanValue()) {
                world.setBlockAndUpdate(blockPos, (BlockState) blockState.setValue(OPEN_DOOR, true));
            } else {
                ItemStack itemStack = player.getItemInHand(hand);
                BlockEntity tileEntity = world.getBlockEntity(blockPos);
                StoveBlockEntity stoveBlockEntity = null;
                if (tileEntity instanceof StoveBlockEntity) {
                    stoveBlockEntity = (StoveBlockEntity) tileEntity;
                }
                boolean tookItem = false;
                boolean placedItem = false;
                if (!itemStack.isEmpty()) {
                    Optional<CampfireCookingRecipe> optional = world.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SimpleContainer(new ItemStack[]{itemStack}), world);
                    if (optional.isPresent() && stoveBlockEntity != null && !stoveBlockEntity.isFull()) {
                        ItemStack oneItem = itemStack.split(1);
                        stoveBlockEntity.cookItem(oneItem, calculateTime(optional));
                        placedItem = true;
                    }
                } else if (stoveBlockEntity != null && !stoveBlockEntity.isEmpty() && player.isSteppingCarefully() && (slot = stoveBlockEntity.getNextOccupiedSlot()) != -1) {
                    player.addItem(stoveBlockEntity.getItem(slot).copy());
                    stoveBlockEntity.setItem(slot, ItemStack.EMPTY);
                    tookItem = true;
                }
                if (!tookItem && !placedItem) {
                    world.setBlockAndUpdate(blockPos, (BlockState) blockState.setValue(OPEN_DOOR, false));
                } else {
                    stoveBlockEntity.setChanged();
                    if (!tookItem && stoveBlockEntity.isFull()) {
                        world.setBlockAndUpdate(blockPos, (BlockState) blockState.setValue(OPEN_DOOR, false));
                    }
                }
                world.sendBlockUpdated(blockPos, blockState, blockState, 2);
            }
        }
        return InteractionResult.CONSUME;
    }

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

    /* renamed from: com.dairymoose.modernlife.blocks.StoveBlock$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StoveBlock$1.class */
    static /* synthetic */ class C00581 {
        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 bs, BlockGetter reader, BlockPos pos, CollisionContext sel) {
        switch (C00581.$SwitchMap$net$minecraft$core$Direction[bs.getValue(HorizontalDirectionalBlock.FACING).ordinal()]) {
            case 1:
                return SHAPE_NORTH;
            case 2:
                return SHAPE_SOUTH;
            case 3:
                return SHAPE_EAST;
            case 4:
                return SHAPE_WEST;
            default:
                return SHAPE_NORTH;
        }
    }

    @Override // com.dairymoose.modernlife.blocks.StandardHorizontalBlock
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return (BlockState) defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, context.getHorizontalDirection());
    }
}
