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.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 ben) {
        return DropperBlock.DISPENSE_BEHAVIOUR;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new DropperBlockEntity();
    }
    
    @Override
    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);
        if (ben7.isEmpty()) {
            return;
        }
        final Direction fp8 = bjw.getBlockState(fk).<Direction>getValue((Property<Direction>)DropperBlock.FACING);
        final Container aje9 = HopperBlockEntity.getContainerAt(bjw, fk.relative(fp8));
        ItemStack ben8;
        if (aje9 == null) {
            ben8 = DropperBlock.DISPENSE_BEHAVIOUR.dispense(fm4, ben7);
        }
        else {
            ben8 = HopperBlockEntity.addItem(bwu5, aje9, ben7.copy().split(1), fp8.getOpposite());
            if (ben8.isEmpty()) {
                ben8 = ben7.copy();
                ben8.shrink(1);
            }
            else {
                ben8 = ben7.copy();
            }
        }
        bwu5.setItem(integer6, ben8);
    }
    
    static {
        DISPENSE_BEHAVIOUR = new DefaultDispenseItemBehavior();
    }
}
