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 fv, final ItemStack bki) {
        final ItemStack bki2 = this.execute(fv, bki);
        this.playSound(fv);
        this.playAnimation(fv, fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING));
        return bki2;
    }
    
    protected ItemStack execute(final BlockSource fv, final ItemStack bki) {
        final Direction fz4 = fv.getBlockState().<Direction>getValue((Property<Direction>)DispenserBlock.FACING);
        final Position gj5 = DispenserBlock.getDispensePosition(fv);
        final ItemStack bki2 = bki.split(1);
        spawnItem(fv.getLevel(), bki2, 6, fz4, gj5);
        return bki;
    }
    
    public static void spawnItem(final Level bqb, final ItemStack bki, final int integer, final Direction fz, final Position gj) {
        final double double6 = gj.x();
        double double7 = gj.y();
        final double double8 = gj.z();
        if (fz.getAxis() == Direction.Axis.Y) {
            double7 -= 0.125;
        }
        else {
            double7 -= 0.15625;
        }
        final ItemEntity bbg12 = new ItemEntity(bqb, double6, double7, double8, bki);
        final double double9 = bqb.random.nextDouble() * 0.1 + 0.2;
        bbg12.setDeltaMovement(bqb.random.nextGaussian() * 0.007499999832361937 * integer + fz.getStepX() * double9, bqb.random.nextGaussian() * 0.007499999832361937 * integer + 0.20000000298023224, bqb.random.nextGaussian() * 0.007499999832361937 * integer + fz.getStepZ() * double9);
        bqb.addFreshEntity(bbg12);
    }
    
    protected void playSound(final BlockSource fv) {
        fv.getLevel().levelEvent(1000, fv.getPos(), 0);
    }
    
    protected void playAnimation(final BlockSource fv, final Direction fz) {
        fv.getLevel().levelEvent(2000, fv.getPos(), fz.get3DDataValue());
    }
}
