package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.Util;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.core.PositionImpl;
import net.minecraft.core.Position;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.BlockSource;
import net.minecraft.core.BlockSourceImpl;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.stats.Stats;
import net.minecraft.world.level.block.entity.DropperBlockEntity;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.level.block.entity.DispenserBlockEntity;
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.core.BlockPos;
import net.minecraft.world.level.Level;
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.level.ItemLike;
import net.minecraft.core.dispenser.DispenseItemBehavior;
import net.minecraft.world.item.Item;
import java.util.Map;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public class DispenserBlock extends BaseEntityBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty TRIGGERED;
    private static final Map<Item, DispenseItemBehavior> DISPENSER_REGISTRY;
    
    public static void registerBehavior(final ItemLike bqa, final DispenseItemBehavior gw) {
        DispenserBlock.DISPENSER_REGISTRY.put(bqa.asItem(), gw);
    }
    
    protected DispenserBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DispenserBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, false));
    }
    
    @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 DispenserBlockEntity) {
            bec.openMenu((MenuProvider)cdl8);
            if (cdl8 instanceof DropperBlockEntity) {
                bec.awardStat(Stats.INSPECT_DROPPER);
            }
            else {
                bec.awardStat(Stats.INSPECT_DISPENSER);
            }
        }
        return InteractionResult.CONSUME;
    }
    
    protected void dispenseFrom(final Level bqb, final BlockPos fu) {
        final BlockSourceImpl fw4 = new BlockSourceImpl(bqb, fu);
        final DispenserBlockEntity cdu5 = fw4.<DispenserBlockEntity>getEntity();
        final int integer6 = cdu5.getRandomSlot();
        if (integer6 < 0) {
            bqb.levelEvent(1001, fu, 0);
            return;
        }
        final ItemStack bki7 = cdu5.getItem(integer6);
        final DispenseItemBehavior gw8 = this.getDispenseMethod(bki7);
        if (gw8 != DispenseItemBehavior.NOOP) {
            cdu5.setItem(integer6, gw8.dispense(fw4, bki7));
        }
    }
    
    protected DispenseItemBehavior getDispenseMethod(final ItemStack bki) {
        return DispenserBlock.DISPENSER_REGISTRY.get(bki.getItem());
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        final boolean boolean7 = bqb.hasNeighborSignal(fu3) || bqb.hasNeighborSignal(fu3.above());
        final boolean boolean8 = cfj.<Boolean>getValue((Property<Boolean>)DispenserBlock.TRIGGERED);
        if (boolean7 && !boolean8) {
            bqb.getBlockTicks().scheduleTick(fu3, this, 4);
            bqb.setBlock(fu3, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, true), 4);
        }
        else if (!boolean7 && boolean8) {
            bqb.setBlock(fu3, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, false), 4);
        }
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        this.dispenseFrom(zd, fu);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new DispenserBlockEntity();
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)DispenserBlock.FACING, bin.getNearestLookingDirection().getOpposite());
    }
    
    @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 DispenserBlockEntity) {
                ((DispenserBlockEntity)cdl7).setCustomName(bki.getHoverName());
            }
        }
    }
    
    @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 DispenserBlockEntity) {
            Containers.dropContents(bqb, fu, (Container)cdl7);
            bqb.updateNeighbourForOutputSignal(fu, this);
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    public static Position getDispensePosition(final BlockSource fv) {
        final Direction fz2 = fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
        final double double3 = fv.x() + 0.7 * fz2.getStepX();
        final double double4 = fv.y() + 0.7 * fz2.getStepY();
        final double double5 = fv.z() + 0.7 * fz2.getStepZ();
        return new PositionImpl(double3, double4, double5);
    }
    
    @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 RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)DispenserBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)DispenserBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)DispenserBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DispenserBlock.FACING, DispenserBlock.TRIGGERED);
    }
    
    static {
        FACING = DirectionalBlock.FACING;
        TRIGGERED = BlockStateProperties.TRIGGERED;
        DISPENSER_REGISTRY = Util.<Map<Item, DispenseItemBehavior>>make((Map<Item, DispenseItemBehavior>)new Object2ObjectOpenHashMap(), object2ObjectOpenHashMap -> object2ObjectOpenHashMap.defaultReturnValue(new DefaultDispenseItemBehavior()));
    }
}
