package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<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 byj, final BlockGetter bjg, final BlockPos fk) {
        switch (byj.<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 bcq) {
        final Direction fp3 = bcq.getClickedFace().getOpposite();
        return (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)HopperBlock.FACING, (fp3.getAxis() == Direction.Axis.Y) ? Direction.DOWN : fp3)).<Comparable, Boolean>setValue((Property<Comparable>)HopperBlock.ENABLED, true);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new HopperBlockEntity();
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        if (ben.hasCustomHoverName()) {
            final BlockEntity bwl7 = bjw.getBlockEntity(fk);
            if (bwl7 instanceof HopperBlockEntity) {
                ((HopperBlockEntity)bwl7).setCustomName(ben.getHoverName());
            }
        }
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj4.getBlock() == byj1.getBlock()) {
            return;
        }
        this.checkPoweredState(bjw, fk, byj1);
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockEntity bwl8 = bjw.getBlockEntity(fk);
        if (bwl8 instanceof HopperBlockEntity) {
            ayj.openMenu((MenuProvider)bwl8);
            ayj.awardStat(Stats.INSPECT_HOPPER);
        }
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        this.checkPoweredState(bjw, fk3, byj);
    }
    
    private void checkPoweredState(final Level bjw, final BlockPos fk, final BlockState byj) {
        final boolean boolean5 = !bjw.hasNeighborSignal(fk);
        if (boolean5 != byj.<Boolean>getValue((Property<Boolean>)HopperBlock.ENABLED)) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)HopperBlock.ENABLED, boolean5), 4);
        }
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        final BlockEntity bwl7 = bjw.getBlockEntity(fk);
        if (bwl7 instanceof HopperBlockEntity) {
            Containers.dropContents(bjw, fk, (Container)bwl7);
            bjw.updateNeighbourForOutputSignal(fk, this);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(bjw.getBlockEntity(fk));
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)HopperBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)HopperBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<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 byj, final Level bjw, final BlockPos fk, final Entity akq) {
        final BlockEntity bwl6 = bjw.getBlockEntity(fk);
        if (bwl6 instanceof HopperBlockEntity) {
            ((HopperBlockEntity)bwl6).entityInside(akq);
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        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));
    }
}
