package net.minecraft.src;
import java.util.Random;
public class BlockChestSmelter extends BlockContainer {
private BaseMod base;
private Random random;
protected BlockChestSmelter (BaseMod _base, int i) {
super (i, Material.wood);
base = _base;
random = new Random();
blockIndexInTexture = 26;
setTickRandomly(true);
setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}
public boolean renderAsNormalBlock()
{
return false;
}
public boolean isOpaqueCube()
{
return false;
}
public int getRenderType()
{
return ((mod_utilityChest)base).chestSmelterRenderID;
}
public int getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l)
{
if(l == 1)
{
return blockIndexInTexture - 1;
}
if(l == 0)
{
return blockIndexInTexture - 1;
}
int i1 = iblockaccess.getBlockId(i, j, k - 1);
int j1 = iblockaccess.getBlockId(i, j, k + 1);
int k1 = iblockaccess.getBlockId(i - 1, j, k);
int l1 = iblockaccess.getBlockId(i + 1, j, k);
byte byte0 = 3;
if(Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[j1])
{
byte0 = 3;
}
if(Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[i1])
{
byte0 = 2;
}
if(Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[l1])
{
byte0 = 5;
}
if(Block.opaqueCubeLookup[l1] && !Block.opaqueCubeLookup[k1])
{
byte0 = 4;
}
return l != byte0 ? blockIndexInTexture : blockIndexInTexture + 1;
}
public int getBlockTextureFromSide(int i)
{
if(i == 1)
{
return blockIndexInTexture - 1;
}
if(i == 0)
{
return blockIndexInTexture - 1;
}
if(i == 3)
{
return blockIndexInTexture + 1;
} else
{
return blockIndexInTexture;
}
}
public void updateTick(World world, int i, int j, int k, Random random) {
System.out.println("TICK");
if (world.isRemote) {
return;
}
TileEntityUtilityChest uchest = (TileEntityUtilityChest)world.getBlockTileEntity(i, j, k);
for (int i1 = 0; i1 < uchest.getSizeInventory(); i1 ++) {
ItemStack is = uchest.getStackInSlot(i1);
if (is == null) {
continue;
}
if (canSmelt(world, is, uchest)) {
uchest.decrStackSize(i1, 1);
}
}
uchest.onInventoryChanged();
}
public boolean canSmelt(World world, ItemStack is, TileEntityUtilityChest uchest) {
ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(is);
if (itemstack == null) {
return false;
}
return getNextFreeSlot(world, itemstack, uchest);
}
public boolean getNextFreeSlot(World world, ItemStack is, TileEntityUtilityChest uchest) {
if (is == null) {
return false;
}
int i = 0;
ItemStack is2;
if (is.isStackable()) {
System.out.println(is.stackSize);
while (is.stackSize > 0 && i < uchest.getSizeInventory()) {
is2 = uchest.getStackInSlot(i);
if (is2 == null) {
i++;
continue;
}
System.out.println("IS2!null: " + (is2 != null) + " IDs " + (is2.itemID == is.itemID) + " Damage " + (!is.getHasSubtypes() || is.getItemDamage() == is2.getItemDamage()) + " Enchant " + (ItemStack.func_46154_a(is, is2)));
if (is2 != null && is2.itemID == is.itemID && (!is.getHasSubtypes() || is.getItemDamage() == is2.getItemDamage()) && ItemStack.func_46154_a(is, is2)) {
int j = is.stackSize + is2.stackSize;
System.out.println("New SS " + j);
if (j <= is.getMaxStackSize()) {
is.stackSize = 0;
is2.stackSize = j;
} else if (is.stackSize < is.getMaxStackSize()){
is.stackSize -= is.getMaxStackSize() - is2.stackSize;
is2.stackSize = is2.getMaxStackSize();
}
}
i++;
}
}
if (is.stackSize > 0) {
i = 0;
while (i < uchest.getSizeInventory()) {
is2 = uchest.getStackInSlot(i);
if (is2 == null) {
uchest.setInventorySlotContents(i, is.copy());
is.stackSize = 0;
break;
}
i++;
}
if (is.stackSize > 0) {
dropItem(world, is.copy(), uchest);
is.stackSize = 0;
}
}
return true;
}
public void dropItem(World world, ItemStack is, TileEntityUtilityChest uchest) {
EntityItem item = new EntityItem(world, (float)uchest.xCoord, (float)uchest.yCoord, (float)uchest.zCoord, is);
item.motionX = 0.0D;
item.motionY = 0.0D;
item.motionZ = 0.0D;
if (is.hasTagCompound())
{
item.item.setTagCompound((NBTTagCompound)is.getTagCompound().copy());
}
world.spawnEntityInWorld(item);
}
@Override
public TileEntity getBlockEntity() {
return new TileEntitySmelterChest();
}
public boolean blockActivated(World world, int i, int j, int k, EntityPlayer entityplayer)
{
TileEntitySmelterChest tesc = (TileEntitySmelterChest)world.getBlockTileEntity(i, j, k);
entityplayer.displayGUIChest(((IInventory) (tesc)));
return true;
}
public void onBlockRemoval(World par1World, int par2, int par3, int par4)
{
TileEntitySmelterChest tileentitychest = (TileEntitySmelterChest)par1World.getBlockTileEntity(par2, par3, par4);
if (tileentitychest != null)
{
for (int i = 0; i < tileentitychest.getSizeInventory(); i++)
{
ItemStack itemstack = tileentitychest.getStackInSlot(i);
if (itemstack == null)
{
continue;
}
float f = random.nextFloat() * 0.8F + 0.1F;
float f1 = random.nextFloat() * 0.8F + 0.1F;
float f2 = random.nextFloat() * 0.8F + 0.1F;
while (itemstack.stackSize > 0)
{
int j = random.nextInt(21) + 10;
if (j > itemstack.stackSize)
{
j = itemstack.stackSize;
}
itemstack.stackSize -= j;
EntityItem entityitem = new EntityItem(par1World, (float)par2 + f, (float)par3 + f1, (float)par4 + f2, new ItemStack(itemstack.itemID, j, itemstack.getItemDamage()));
float f3 = 0.05F;
entityitem.motionX = (float)random.nextGaussian() * f3;
entityitem.motionY = (float)random.nextGaussian() * f3 + 0.2F;
entityitem.motionZ = (float)random.nextGaussian() * f3;
if (itemstack.hasTagCompound())
{
entityitem.item.setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
}
par1World.spawnEntityInWorld(entityitem);
}
}
}
super.onBlockRemoval(par1World, par2, par3, par4);
}
public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving)
{
byte byte0 = 0;
int i1 = MathHelper.floor_double((double)((entityliving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
if (i1 == 0)
{
byte0 = 2;
}
if (i1 == 1)
{
byte0 = 5;
}
if (i1 == 2)
{
byte0 = 3;
}
if (i1 == 3)
{
byte0 = 4;
}
world.setBlockMetadataWithNotify(i, j, k, byte0);
}
}