package net.minecraft.world.level.block;

import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
import net.minecraft.world.Container;
import net.minecraft.core.BlockSource;
import net.minecraft.world.level.block.entity.HopperBlockEntity;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.DispenserBlockEntity;
import net.minecraft.core.BlockSourceImpl;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.DropperBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.core.dispenser.DispenseItemBehavior;

public class DropperBlock extends DispenserBlock {
    private static final DispenseItemBehavior DISPENSE_BEHAVIOUR;
    
    public DropperBlock(final Properties c) {
        super(c);
    }
    
    @Override
    protected DispenseItemBehavior getDispenseMethod(final ItemStack bki) {
        return DropperBlock.DISPENSE_BEHAVIOUR;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new DropperBlockEntity();
    }
    
    @Override
    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);
        if (bki7.isEmpty()) {
            return;
        }
        final Direction fz8 = bqb.getBlockState(fu).<Direction>getValue((Property<Direction>)DropperBlock.FACING);
        final Container amz9 = HopperBlockEntity.getContainerAt(bqb, fu.relative(fz8));
        ItemStack bki8;
        if (amz9 == null) {
            bki8 = DropperBlock.DISPENSE_BEHAVIOUR.dispense(fw4, bki7);
        }
        else {
            bki8 = HopperBlockEntity.addItem(cdu5, amz9, bki7.copy().split(1), fz8.getOpposite());
            if (bki8.isEmpty()) {
                bki8 = bki7.copy();
                bki8.shrink(1);
            }
            else {
                bki8 = bki7.copy();
            }
        }
        cdu5.setItem(integer6, bki8);
    }
    
    static {
        DISPENSE_BEHAVIOUR = new DefaultDispenseItemBehavior();
    }
}
