package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.entity.Hopper;
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.DirectionProperty;

public class HopperBlock extends BaseEntityBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty ENABLED;
    private static final VoxelShape TOP;
    private static final VoxelShape FUNNEL;
    private static final VoxelShape CONVEX_BASE;
    private static final VoxelShape BASE;
    private static final VoxelShape DOWN_SHAPE;
    private static final VoxelShape EAST_SHAPE;
    private static final VoxelShape NORTH_SHAPE;
    private static final VoxelShape SOUTH_SHAPE;
    private static final VoxelShape WEST_SHAPE;
    private static final VoxelShape DOWN_INTERACTION_SHAPE;
    private static final VoxelShape EAST_INTERACTION_SHAPE;
    private static final VoxelShape NORTH_INTERACTION_SHAPE;
    private static final VoxelShape SOUTH_INTERACTION_SHAPE;
    private static final VoxelShape WEST_INTERACTION_SHAPE;
    
    public HopperBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)HopperBlock.FACING, Direction.DOWN)).<Comparable, Boolean>setValue((Property<Comparable>)HopperBlock.ENABLED, true));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<Direction>getValue((Property<Direction>)HopperBlock.FACING)) {
            case DOWN: {
                return HopperBlock.DOWN_SHAPE;
            }
            case NORTH: {
                return HopperBlock.NORTH_SHAPE;
            }
            case SOUTH: {
                return HopperBlock.SOUTH_SHAPE;
            }
            case WEST: {
                return HopperBlock.WEST_SHAPE;
            }
            case EAST: {
                return HopperBlock.EAST_SHAPE;
            }
            default: {
                return HopperBlock.BASE;
            }
        }
    }
    
    @Override
    public VoxelShape getInteractionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        switch (cfj.<Direction>getValue((Property<Direction>)HopperBlock.FACING)) {
            case DOWN: {
                return HopperBlock.DOWN_INTERACTION_SHAPE;
            }
            case NORTH: {
                return HopperBlock.NORTH_INTERACTION_SHAPE;
            }
            case SOUTH: {
                return HopperBlock.SOUTH_INTERACTION_SHAPE;
            }
            case WEST: {
                return HopperBlock.WEST_INTERACTION_SHAPE;
            }
            case EAST: {
                return HopperBlock.EAST_INTERACTION_SHAPE;
            }
            default: {
                return Hopper.INSIDE;
            }
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final Direction fz3 = bin.getClickedFace().getOpposite();
        return (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)HopperBlock.FACING, (fz3.getAxis() == Direction.Axis.Y) ? Direction.DOWN : fz3)).<Comparable, Boolean>setValue((Property<Comparable>)HopperBlock.ENABLED, true);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new HopperBlockEntity();
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        if (bki.hasCustomHoverName()) {
            final BlockEntity cdl7 = bqb.getBlockEntity(fu);
            if (cdl7 instanceof HopperBlockEntity) {
                ((HopperBlockEntity)cdl7).setCustomName(bki.getHoverName());
            }
        }
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        this.checkPoweredState(bqb, fu, cfj1);
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (bqb.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockEntity cdl8 = bqb.getBlockEntity(fu);
        if (cdl8 instanceof HopperBlockEntity) {
            bec.openMenu((MenuProvider)cdl8);
            bec.awardStat(Stats.INSPECT_HOPPER);
        }
        return InteractionResult.CONSUME;
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        this.checkPoweredState(bqb, fu3, cfj);
    }
    
    private void checkPoweredState(final Level bqb, final BlockPos fu, final BlockState cfj) {
        final boolean boolean5 = !bqb.hasNeighborSignal(fu);
        if (boolean5 != cfj.<Boolean>getValue((Property<Boolean>)HopperBlock.ENABLED)) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)HopperBlock.ENABLED, boolean5), 4);
        }
    }
    
    @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;
        }
        final BlockEntity cdl7 = bqb.getBlockEntity(fu);
        if (cdl7 instanceof HopperBlockEntity) {
            Containers.dropContents(bqb, fu, (Container)cdl7);
            bqb.updateNeighbourForOutputSignal(fu, this);
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(bqb.getBlockEntity(fu));
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)HopperBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)HopperBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)HopperBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(HopperBlock.FACING, HopperBlock.ENABLED);
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        final BlockEntity cdl6 = bqb.getBlockEntity(fu);
        if (cdl6 instanceof HopperBlockEntity) {
            ((HopperBlockEntity)cdl6).entityInside(aom);
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        FACING = BlockStateProperties.FACING_HOPPER;
        ENABLED = BlockStateProperties.ENABLED;
        TOP = Block.box(0.0, 10.0, 0.0, 16.0, 16.0, 16.0);
        FUNNEL = Block.box(4.0, 4.0, 4.0, 12.0, 10.0, 12.0);
        CONVEX_BASE = Shapes.or(HopperBlock.FUNNEL, HopperBlock.TOP);
        BASE = Shapes.join(HopperBlock.CONVEX_BASE, Hopper.INSIDE, BooleanOp.ONLY_FIRST);
        DOWN_SHAPE = Shapes.or(HopperBlock.BASE, Block.box(6.0, 0.0, 6.0, 10.0, 4.0, 10.0));
        EAST_SHAPE = Shapes.or(HopperBlock.BASE, Block.box(12.0, 4.0, 6.0, 16.0, 8.0, 10.0));
        NORTH_SHAPE = Shapes.or(HopperBlock.BASE, Block.box(6.0, 4.0, 0.0, 10.0, 8.0, 4.0));
        SOUTH_SHAPE = Shapes.or(HopperBlock.BASE, Block.box(6.0, 4.0, 12.0, 10.0, 8.0, 16.0));
        WEST_SHAPE = Shapes.or(HopperBlock.BASE, Block.box(0.0, 4.0, 6.0, 4.0, 8.0, 10.0));
        DOWN_INTERACTION_SHAPE = Hopper.INSIDE;
        EAST_INTERACTION_SHAPE = Shapes.or(Hopper.INSIDE, Block.box(12.0, 8.0, 6.0, 16.0, 10.0, 10.0));
        NORTH_INTERACTION_SHAPE = Shapes.or(Hopper.INSIDE, Block.box(6.0, 8.0, 0.0, 10.0, 10.0, 4.0));
        SOUTH_INTERACTION_SHAPE = Shapes.or(Hopper.INSIDE, Block.box(6.0, 8.0, 12.0, 10.0, 10.0, 16.0));
        WEST_INTERACTION_SHAPE = Shapes.or(Hopper.INSIDE, Block.box(0.0, 8.0, 6.0, 4.0, 10.0, 10.0));
    }
}
