    001package net.minecraft.dispenser;
    002
    003import net.minecraft.block.BlockDispenser;
    004import net.minecraft.entity.item.EntityItem;
    005import net.minecraft.item.ItemStack;
    006import net.minecraft.util.EnumFacing;
    007import net.minecraft.world.World;
    008
    009public class BehaviorDefaultDispenseItem implements IBehaviorDispenseItem
    010{
    011    /**
    012     * Dispenses the specified ItemStack from a dispenser.
    013     */
    014    public final ItemStack dispense(IBlockSource par1IBlockSource, ItemStack par2ItemStack)
    015    {
    016        ItemStack itemstack1 = this.dispenseStack(par1IBlockSource, par2ItemStack);
    017        this.playDispenseSound(par1IBlockSource);
    018        this.spawnDispenseParticles(par1IBlockSource, BlockDispenser.getFacing(par1IBlockSource.getBlockMetadata()));
    019        return itemstack1;
    020    }
    021
    022    /**
    023     * Dispense the specified stack, play the dispense sound and spawn particles.
    024     */
    025    protected ItemStack dispenseStack(IBlockSource par1IBlockSource, ItemStack par2ItemStack)
    026    {
    027        EnumFacing enumfacing = BlockDispenser.getFacing(par1IBlockSource.getBlockMetadata());
    028        IPosition iposition = BlockDispenser.getIPositionFromBlockSource(par1IBlockSource);
    029        ItemStack itemstack1 = par2ItemStack.splitStack(1);
    030        doDispense(par1IBlockSource.getWorld(), itemstack1, 6, enumfacing, iposition);
    031        return par2ItemStack;
    032    }
    033
    034    public static void doDispense(World par0World, ItemStack par1ItemStack, int par2, EnumFacing par3EnumFacing, IPosition par4IPosition)
    035    {
    036        double d0 = par4IPosition.getX();
    037        double d1 = par4IPosition.getY();
    038        double d2 = par4IPosition.getZ();
    039        EntityItem entityitem = new EntityItem(par0World, d0, d1 - 0.3D, d2, par1ItemStack);
    040        double d3 = par0World.rand.nextDouble() * 0.1D + 0.2D;
    041        entityitem.motionX = (double)par3EnumFacing.getFrontOffsetX() * d3;
    042        entityitem.motionY = 0.20000000298023224D;
    043        entityitem.motionZ = (double)par3EnumFacing.getFrontOffsetZ() * d3;
    044        entityitem.motionX += par0World.rand.nextGaussian() * 0.007499999832361937D * (double)par2;
    045        entityitem.motionY += par0World.rand.nextGaussian() * 0.007499999832361937D * (double)par2;
    046        entityitem.motionZ += par0World.rand.nextGaussian() * 0.007499999832361937D * (double)par2;
    047        par0World.spawnEntityInWorld(entityitem);
    048    }
    049
    050    /**
    051     * Play the dispense sound from the specified block.
    052     */
    053    protected void playDispenseSound(IBlockSource par1IBlockSource)
    054    {
    055        par1IBlockSource.getWorld().playAuxSFX(1000, par1IBlockSource.getXInt(), par1IBlockSource.getYInt(), par1IBlockSource.getZInt(), 0);
    056    }
    057
    058    /**
    059     * Order clients to display dispense particles from the specified block and facing.
    060     */
    061    protected void spawnDispenseParticles(IBlockSource par1IBlockSource, EnumFacing par2EnumFacing)
    062    {
    063        par1IBlockSource.getWorld().playAuxSFX(2000, par1IBlockSource.getXInt(), par1IBlockSource.getYInt(), par1IBlockSource.getZInt(), this.func_82488_a(par2EnumFacing));
    064    }
    065
    066    private int func_82488_a(EnumFacing par1EnumFacing)
    067    {
    068        return par1EnumFacing.getFrontOffsetX() + 1 + (par1EnumFacing.getFrontOffsetZ() + 1) * 3;
    069    }
    070}