package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.LevelReader;
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.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 bjv, final DispenseItemBehavior gl) {
        DispenserBlock.DISPENSER_REGISTRY.put(bjv.asItem(), gl);
    }
    
    protected DispenserBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DispenserBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, false));
    }
    
    @Override
    public int getTickDelay(final LevelReader bjz) {
        return 4;
    }
    
    @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 DispenserBlockEntity) {
            ayj.openMenu((MenuProvider)bwl8);
            if (bwl8 instanceof DropperBlockEntity) {
                ayj.awardStat(Stats.INSPECT_DROPPER);
            }
            else {
                ayj.awardStat(Stats.INSPECT_DISPENSER);
            }
        }
        return InteractionResult.SUCCESS;
    }
    
    protected void dispenseFrom(final Level bjw, final BlockPos fk) {
        final BlockSourceImpl fm4 = new BlockSourceImpl(bjw, fk);
        final DispenserBlockEntity bwu5 = fm4.<DispenserBlockEntity>getEntity();
        final int integer6 = bwu5.getRandomSlot();
        if (integer6 < 0) {
            bjw.levelEvent(1001, fk, 0);
            return;
        }
        final ItemStack ben7 = bwu5.getItem(integer6);
        final DispenseItemBehavior gl8 = this.getDispenseMethod(ben7);
        if (gl8 != DispenseItemBehavior.NOOP) {
            bwu5.setItem(integer6, gl8.dispense(fm4, ben7));
        }
    }
    
    protected DispenseItemBehavior getDispenseMethod(final ItemStack ben) {
        return DispenserBlock.DISPENSER_REGISTRY.get(ben.getItem());
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        final boolean boolean7 = bjw.hasNeighborSignal(fk3) || bjw.hasNeighborSignal(fk3.above());
        final boolean boolean8 = byj.<Boolean>getValue((Property<Boolean>)DispenserBlock.TRIGGERED);
        if (boolean7 && !boolean8) {
            bjw.getBlockTicks().scheduleTick(fk3, this, this.getTickDelay(bjw));
            bjw.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, true), 4);
        }
        else if (!boolean7 && boolean8) {
            bjw.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DispenserBlock.TRIGGERED, false), 4);
        }
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        this.dispenseFrom(xd, fk);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new DispenserBlockEntity();
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)DispenserBlock.FACING, bcq.getNearestLookingDirection().getOpposite());
    }
    
    @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 DispenserBlockEntity) {
                ((DispenserBlockEntity)bwl7).setCustomName(ben.getHoverName());
            }
        }
    }
    
    @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 DispenserBlockEntity) {
            Containers.dropContents(bjw, fk, (Container)bwl7);
            bjw.updateNeighbourForOutputSignal(fk, this);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    public static Position getDispensePosition(final BlockSource fl) {
        final Direction fp2 = fl.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
        final double double3 = fl.x() + 0.7 * fp2.getStepX();
        final double double4 = fl.y() + 0.7 * fp2.getStepY();
        final double double5 = fl.z() + 0.7 * fp2.getStepZ();
        return new PositionImpl(double3, double4, double5);
    }
    
    @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 RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)DispenserBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)DispenserBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<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()));
    }
}
