package net.minecraft.core.dispenser;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.Level;
import net.minecraft.core.Position;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.BlockSource;

public class DefaultDispenseItemBehavior implements DispenseItemBehavior {
    @Override
    public final ItemStack dispense(final BlockSource fl, final ItemStack ben) {
        final ItemStack ben2 = this.execute(fl, ben);
        this.playSound(fl);
        this.playAnimation(fl, fl.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING));
        return ben2;
    }
    
    protected ItemStack execute(final BlockSource fl, final ItemStack ben) {
        final Direction fp4 = fl.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
        final Position fz5 = DispenserBlock.getDispensePosition(fl);
        final ItemStack ben2 = ben.split(1);
        spawnItem(fl.getLevel(), ben2, 6, fp4, fz5);
        return ben;
    }
    
    public static void spawnItem(final Level bjw, final ItemStack ben, final int integer, final Direction fp, final Position fz) {
        final double double6 = fz.x();
        double double7 = fz.y();
        final double double8 = fz.z();
        if (fp.getAxis() == Direction.Axis.Y) {
            double7 -= 0.125;
        }
        else {
            double7 -= 0.15625;
        }
        final ItemEntity awb12 = new ItemEntity(bjw, double6, double7, double8, ben);
        final double double9 = bjw.random.nextDouble() * 0.1 + 0.2;
        awb12.setDeltaMovement(bjw.random.nextGaussian() * 0.007499999832361937 * integer + fp.getStepX() * double9, bjw.random.nextGaussian() * 0.007499999832361937 * integer + 0.20000000298023224, bjw.random.nextGaussian() * 0.007499999832361937 * integer + fp.getStepZ() * double9);
        bjw.addFreshEntity(awb12);
    }
    
    protected void playSound(final BlockSource fl) {
        fl.getLevel().levelEvent(1000, fl.getPos(), 0);
    }
    
    protected void playAnimation(final BlockSource fl, final Direction fp) {
        fl.getLevel().levelEvent(2000, fl.getPos(), fp.get3DDataValue());
    }
}
