package net.minecraft.block;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
import net.minecraft.dispenser.IBehaviorDispenseItem;
import net.minecraft.dispenser.IBlockSource;
import net.minecraft.dispenser.IPosition;
import net.minecraft.dispenser.IRegistry;
import net.minecraft.dispenser.PositionImpl;
import net.minecraft.dispenser.RegistryDefaulted;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
public class BlockDispenser extends BlockContainer
{
/** Registry for all dispense behaviors. */
public static final IRegistry dispenseBehaviorRegistry = new RegistryDefaulted(new BehaviorDefaultDispenseItem());
private Random random = new Random();
protected BlockDispenser(int par1)
{
super(par1, Material.rock);
this.blockIndexInTexture = 45;
this.setCreativeTab(CreativeTabs.tabRedstone);
}
/**
* How many world ticks before ticking
*/
public int tickRate()
{
return 4;
}
/**
* Returns the ID of the items to drop on destruction.
*/
public int idDropped(int par1, Random par2Random, int par3)
{
return Block.dispenser.blockID;
}
/**
* Called whenever the block is added into the world. Args: world, x, y, z
*/
public void onBlockAdded(World par1World, int par2, int par3, int par4)
{
super.onBlockAdded(par1World, par2, par3, par4);
this.setDispenserDefaultDirection(par1World, par2, par3, par4);
}
/**
* sets Dispenser block direction so that the front faces an non-opaque block; chooses west to be direction if all
* surrounding blocks are opaque.
*/
private void setDispenserDefaultDirection(World par1World, int par2, int par3, int par4)
{
if (!par1World.isRemote)
{
int var5 = par1World.getBlockId(par2, par3, par4 - 1);
int var6 = par1World.getBlockId(par2, par3, par4 + 1);
int var7 = par1World.getBlockId(par2 - 1, par3, par4);
int var8 = par1World.getBlockId(par2 + 1, par3, par4);
byte var9 = 3;
if (Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var6])
{
var9 = 3;
}
if (Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var5])
{
var9 = 2;
}
if (Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var8])
{
var9 = 5;
}
if (Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var7])
{
var9 = 4;
}
par1World.setBlockMetadataWithNotify(par2, par3, par4, var9);
}
}
@SideOnly(Side.CLIENT)
/**
* Retrieves the block texture to use based on the display side. Args: iBlockAccess, x, y, z, side
*/
public int getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
{
if (par5 == 1)
{
return this.blockIndexInTexture + 17;
}
else if (par5 == 0)
{
return this.blockIndexInTexture + 17;
}
else
{
int var6 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
return par5 == var6 ? this.blockIndexInTexture + 1 : this.blockIndexInTexture;
}
}
/**
* Returns the block texture based on the side being looked at. Args: side
*/
public int getBlockTextureFromSide(int par1)
{
return par1 == 1 ? this.blockIndexInTexture + 17 : (par1 == 0 ? this.blockIndexInTexture + 17 : (par1 == 3 ? this.blockIndexInTexture + 1 : this.blockIndexInTexture));
}
/**
* Called upon block activation (right click on the block.)
*/
public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
{
if (par1World.isRemote)
{
return true;
}
else
{
TileEntityDispenser var10 = (TileEntityDispenser)par1World.getBlockTileEntity(par2, par3, par4);
if (var10 != null)
{
par5EntityPlayer.displayGUIDispenser(var10);
}
return true;
}
}
private void dispense(World par1World, int par2, int par3, int par4)
{
BlockSourceImpl var5 = new BlockSourceImpl(par1World, par2, par3, par4);
TileEntityDispenser var6 = (TileEntityDispenser)var5.func_82619_j();
if (var6 != null)
{
int var7 = var6.getRandomStackFromInventory();
if (var7 < 0)
{
par1World.playAuxSFX(1001, par2, par3, par4, 0);
}
else
{
ItemStack var8 = var6.getStackInSlot(var7);
IBehaviorDispenseItem var9 = (IBehaviorDispenseItem)dispenseBehaviorRegistry.func_82594_a(var8.getItem());
if (var9 != IBehaviorDispenseItem.itemDispenseBehaviorProvider)
{
ItemStack var10 = var9.dispense(var5, var8);
var6.setInventorySlotContents(var7, var10.stackSize == 0 ? null : var10);
}
}
}
}
/**
* Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
* their own) Args: x, y, z, neighbor blockID
*/
public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
{
if (par5 > 0 && Block.blocksList[par5].canProvidePower())
{
boolean var6 = par1World.isBlockIndirectlyGettingPowered(par2, par3, par4) || par1World.isBlockIndirectlyGettingPowered(par2, par3 + 1, par4);
if (var6)
{
par1World.scheduleBlockUpdate(par2, par3, par4, this.blockID, this.tickRate());
}
}
}
/**
* Ticks the block if it's been scheduled
*/
public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
{
if (!par1World.isRemote && (par1World.isBlockIndirectlyGettingPowered(par2, par3, par4) || par1World.isBlockIndirectlyGettingPowered(par2, par3 + 1, par4)))
{
this.dispense(par1World, par2, par3, par4);
}
}
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
public TileEntity createNewTileEntity(World par1World)
{
return new TileEntityDispenser();
}
/**
* Called when the block is placed in the world.
*/
public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
{
int var6 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
if (var6 == 0)
{
par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
}
if (var6 == 1)
{
par1World.setBlockMetadataWithNotify(par2, par3, par4, 5);
}
if (var6 == 2)
{
par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
}
if (var6 == 3)
{
par1World.setBlockMetadataWithNotify(par2, par3, par4, 4);
}
}
/**
* ejects contained items into the world, and notifies neighbours of an update, as appropriate
*/
public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
{
TileEntityDispenser var7 = (TileEntityDispenser)par1World.getBlockTileEntity(par2, par3, par4);
if (var7 != null)
{
for (int var8 = 0; var8 < var7.getSizeInventory(); ++var8)
{
ItemStack var9 = var7.getStackInSlot(var8);
if (var9 != null)
{
float var10 = this.random.nextFloat() * 0.8F + 0.1F;
float var11 = this.random.nextFloat() * 0.8F + 0.1F;
float var12 = this.random.nextFloat() * 0.8F + 0.1F;
while (var9.stackSize > 0)
{
int var13 = this.random.nextInt(21) + 10;
if (var13 > var9.stackSize)
{
var13 = var9.stackSize;
}
var9.stackSize -= var13;
EntityItem var14 = new EntityItem(par1World, (double)((float)par2 + var10), (double)((float)par3 + var11), (double)((float)par4 + var12), new ItemStack(var9.itemID, var13, var9.getItemDamage()));
if (var9.hasTagCompound())
{
var14.func_92014_d().setTagCompound((NBTTagCompound)var9.getTagCompound().copy());
}
float var15 = 0.05F;
var14.motionX = (double)((float)this.random.nextGaussian() * var15);
var14.motionY = (double)((float)this.random.nextGaussian() * var15 + 0.2F);
var14.motionZ = (double)((float)this.random.nextGaussian() * var15);
par1World.spawnEntityInWorld(var14);
}
}
}
}
super.breakBlock(par1World, par2, par3, par4, par5, par6);
}
public static IPosition func_82525_a(IBlockSource par0IBlockSource)
{
EnumFacing var1 = EnumFacing.func_82600_a(par0IBlockSource.func_82620_h());
double var2 = par0IBlockSource.getX() + 0.7D * (double)var1.func_82601_c();
double var4 = par0IBlockSource.getY();
double var6 = par0IBlockSource.getZ() + 0.7D * (double)var1.func_82599_e();
return new PositionImpl(var2, var4, var6);
}
}