package net.minecraft.block;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.Iterator;
import java.util.Random;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryLargeChest;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import static net.minecraftforge.common.util.ForgeDirection.*;
public class BlockChest extends BlockContainer
{
private final Random rand = new Random();
/** 0 : Normal chest, 1 : Trapped chest */
public final int chestType;
private static final String __OBFID = "CL_00000214";
protected BlockChest(int type)
{
super(Material.wood);
this.chestType = type;
this.setCreativeTab(CreativeTabs.tabDecorations);
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}
public boolean isOpaqueCube()
{
return false;
}
public boolean renderAsNormalBlock()
{
return false;
}
/**
* The type of render function that is called for this block
*/
public int getRenderType()
{
return 22;
}
public void setBlockBoundsBasedOnState(IBlockAccess worldIn, int x, int y, int z)
{
if (worldIn.getBlock(x, y, z - 1) == this)
{
this.setBlockBounds(0.0625F, 0.0F, 0.0F, 0.9375F, 0.875F, 0.9375F);
}
else if (worldIn.getBlock(x, y, z + 1) == this)
{
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 1.0F);
}
else if (worldIn.getBlock(x - 1, y, z) == this)
{
this.setBlockBounds(0.0F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}
else if (worldIn.getBlock(x + 1, y, z) == this)
{
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 1.0F, 0.875F, 0.9375F);
}
else
{
this.setBlockBounds(0.0625F, 0.0F, 0.0625F, 0.9375F, 0.875F, 0.9375F);
}
}
public void onBlockAdded(World worldIn, int x, int y, int z)
{
super.onBlockAdded(worldIn, x, y, z);
this.initMetadata(worldIn, x, y, z);
Block block = worldIn.getBlock(x, y, z - 1);
Block block1 = worldIn.getBlock(x, y, z + 1);
Block block2 = worldIn.getBlock(x - 1, y, z);
Block block3 = worldIn.getBlock(x + 1, y, z);
if (block == this)
{
this.initMetadata(worldIn, x, y, z - 1);
}
if (block1 == this)
{
this.initMetadata(worldIn, x, y, z + 1);
}
if (block2 == this)
{
this.initMetadata(worldIn, x - 1, y, z);
}
if (block3 == this)
{
this.initMetadata(worldIn, x + 1, y, z);
}
}
/**
* Called when the block is placed in the world.
*/
public void onBlockPlacedBy(World worldIn, int x, int y, int z, EntityLivingBase placer, ItemStack itemIn)
{
Block block = worldIn.getBlock(x, y, z - 1);
Block block1 = worldIn.getBlock(x, y, z + 1);
Block block2 = worldIn.getBlock(x - 1, y, z);
Block block3 = worldIn.getBlock(x + 1, y, z);
byte b0 = 0;
int l = MathHelper.floor_double((double)(placer.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
if (l == 0)
{
b0 = 2;
}
if (l == 1)
{
b0 = 5;
}
if (l == 2)
{
b0 = 3;
}
if (l == 3)
{
b0 = 4;
}
if (block != this && block1 != this && block2 != this && block3 != this)
{
worldIn.setBlockMetadataWithNotify(x, y, z, b0, 3);
}
else
{
if ((block == this || block1 == this) && (b0 == 4 || b0 == 5))
{
if (block == this)
{
worldIn.setBlockMetadataWithNotify(x, y, z - 1, b0, 3);
}
else
{
worldIn.setBlockMetadataWithNotify(x, y, z + 1, b0, 3);
}
worldIn.setBlockMetadataWithNotify(x, y, z, b0, 3);
}
if ((block2 == this || block3 == this) && (b0 == 2 || b0 == 3))
{
if (block2 == this)
{
worldIn.setBlockMetadataWithNotify(x - 1, y, z, b0, 3);
}
else
{
worldIn.setBlockMetadataWithNotify(x + 1, y, z, b0, 3);
}
worldIn.setBlockMetadataWithNotify(x, y, z, b0, 3);
}
}
if (itemIn.hasDisplayName())
{
((TileEntityChest)worldIn.getTileEntity(x, y, z)).setCustomName(itemIn.getDisplayName());
}
}
/**
* Actualise metadata (orientation) value from neighbors blocks (another chest ? Opaque block ?)
*/
public void initMetadata(World p_149954_1_, int p_149954_2_, int p_149954_3_, int p_149954_4_)
{
if (!p_149954_1_.isRemote)
{
Block block = p_149954_1_.getBlock(p_149954_2_, p_149954_3_, p_149954_4_ - 1);
Block block1 = p_149954_1_.getBlock(p_149954_2_, p_149954_3_, p_149954_4_ + 1);
Block block2 = p_149954_1_.getBlock(p_149954_2_ - 1, p_149954_3_, p_149954_4_);
Block block3 = p_149954_1_.getBlock(p_149954_2_ + 1, p_149954_3_, p_149954_4_);
boolean flag = true;
int l;
Block block4;
int i1;
Block block5;
boolean flag1;
byte b0;
int j1;
if (block != this && block1 != this)
{
if (block2 != this && block3 != this)
{
b0 = 3;
if (block.isFullBlock() && !block1.isFullBlock())
{
b0 = 3;
}
if (block1.isFullBlock() && !block.isFullBlock())
{
b0 = 2;
}
if (block2.isFullBlock() && !block3.isFullBlock())
{
b0 = 5;
}
if (block3.isFullBlock() && !block2.isFullBlock())
{
b0 = 4;
}
}
else
{
l = block2 == this ? p_149954_2_ - 1 : p_149954_2_ + 1;
block4 = p_149954_1_.getBlock(l, p_149954_3_, p_149954_4_ - 1);
i1 = block2 == this ? p_149954_2_ - 1 : p_149954_2_ + 1;
block5 = p_149954_1_.getBlock(i1, p_149954_3_, p_149954_4_ + 1);
b0 = 3;
flag1 = true;
if (block2 == this)
{
j1 = p_149954_1_.getBlockMetadata(p_149954_2_ - 1, p_149954_3_, p_149954_4_);
}
else
{
j1 = p_149954_1_.getBlockMetadata(p_149954_2_ + 1, p_149954_3_, p_149954_4_);
}
if (j1 == 2)
{
b0 = 2;
}
if ((block.isFullBlock() || block4.isFullBlock()) && !block1.isFullBlock() && !block5.isFullBlock())
{
b0 = 3;
}
if ((block1.isFullBlock() || block5.isFullBlock()) && !block.isFullBlock() && !block4.isFullBlock())
{
b0 = 2;
}
}
}
else
{
l = block == this ? p_149954_4_ - 1 : p_149954_4_ + 1;
block4 = p_149954_1_.getBlock(p_149954_2_ - 1, p_149954_3_, l);
i1 = block == this ? p_149954_4_ - 1 : p_149954_4_ + 1;
block5 = p_149954_1_.getBlock(p_149954_2_ + 1, p_149954_3_, i1);
b0 = 5;
flag1 = true;
if (block == this)
{
j1 = p_149954_1_.getBlockMetadata(p_149954_2_, p_149954_3_, p_149954_4_ - 1);
}
else
{
j1 = p_149954_1_.getBlockMetadata(p_149954_2_, p_149954_3_, p_149954_4_ + 1);
}
if (j1 == 4)
{
b0 = 4;
}
if ((block2.isFullBlock() || block4.isFullBlock()) && !block3.isFullBlock() && !block5.isFullBlock())
{
b0 = 5;
}
if ((block3.isFullBlock() || block5.isFullBlock()) && !block2.isFullBlock() && !block4.isFullBlock())
{
b0 = 4;
}
}
p_149954_1_.setBlockMetadataWithNotify(p_149954_2_, p_149954_3_, p_149954_4_, b0, 3);
}
}
public boolean canPlaceBlockAt(World worldIn, int x, int y, int z)
{
int l = 0;
if (worldIn.getBlock(x - 1, y, z) == this)
{
++l;
}
if (worldIn.getBlock(x + 1, y, z) == this)
{
++l;
}
if (worldIn.getBlock(x, y, z - 1) == this)
{
++l;
}
if (worldIn.getBlock(x, y, z + 1) == this)
{
++l;
}
return l > 1 ? false : (this.isDoubleChest(worldIn, x - 1, y, z) ? false : (this.isDoubleChest(worldIn, x + 1, y, z) ? false : (this.isDoubleChest(worldIn, x, y, z - 1) ? false : !this.isDoubleChest(worldIn, x, y, z + 1))));
}
private boolean isDoubleChest(World worldIn, int x, int y, int z)
{
return worldIn.getBlock(x, y, z) != this ? false : (worldIn.getBlock(x - 1, y, z) == this ? true : (worldIn.getBlock(x + 1, y, z) == this ? true : (worldIn.getBlock(x, y, z - 1) == this ? true : worldIn.getBlock(x, y, z + 1) == this)));
}
public void onNeighborBlockChange(World worldIn, int x, int y, int z, Block neighbor)
{
super.onNeighborBlockChange(worldIn, x, y, z, neighbor);
TileEntityChest tileentitychest = (TileEntityChest)worldIn.getTileEntity(x, y, z);
if (tileentitychest != null)
{
tileentitychest.updateContainingBlockInfo();
}
}
public void breakBlock(World worldIn, int x, int y, int z, Block blockBroken, int meta)
{
TileEntityChest tileentitychest = (TileEntityChest)worldIn.getTileEntity(x, y, z);
if (tileentitychest != null)
{
for (int i1 = 0; i1 < tileentitychest.getSizeInventory(); ++i1)
{
ItemStack itemstack = tileentitychest.getStackInSlot(i1);
if (itemstack != null)
{
float f = this.rand.nextFloat() * 0.8F + 0.1F;
float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
EntityItem entityitem;
for (float f2 = this.rand.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; worldIn.spawnEntityInWorld(entityitem))
{
int j1 = this.rand.nextInt(21) + 10;
if (j1 > itemstack.stackSize)
{
j1 = itemstack.stackSize;
}
itemstack.stackSize -= j1;
entityitem = new EntityItem(worldIn, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getMetadata()));
float f3 = 0.05F;
entityitem.motionX = (double)((float)this.rand.nextGaussian() * f3);
entityitem.motionY = (double)((float)this.rand.nextGaussian() * f3 + 0.2F);
entityitem.motionZ = (double)((float)this.rand.nextGaussian() * f3);
if (itemstack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
}
}
}
}
worldIn.updateNeighborsAboutBlockChange(x, y, z, blockBroken);
}
super.breakBlock(worldIn, x, y, z, blockBroken, meta);
}
/**
* Called upon block activation (right click on the block). Args : world, x, y, z, player, side, hitX, hitY, hitZ.
* Return : Swing hand (client), abort the block placement (server)
*/
public boolean onBlockActivated(World worldIn, int x, int y, int z, EntityPlayer player, int side, float subX, float subY, float subZ)
{
if (worldIn.isRemote)
{
return true;
}
else
{
IInventory iinventory = this.getInventory(worldIn, x, y, z);
if (iinventory != null)
{
player.displayGUIChest(iinventory);
}
return true;
}
}
public IInventory getInventory(World p_149951_1_, int p_149951_2_, int p_149951_3_, int p_149951_4_)
{
Object object = (TileEntityChest)p_149951_1_.getTileEntity(p_149951_2_, p_149951_3_, p_149951_4_);
if (object == null)
{
return null;
}
else if (p_149951_1_.isSideSolid(p_149951_2_, p_149951_3_ + 1, p_149951_4_, DOWN))
{
return null;
}
else if (isOcelotSittingOnTop(p_149951_1_, p_149951_2_, p_149951_3_, p_149951_4_))
{
return null;
}
else if (p_149951_1_.getBlock(p_149951_2_ - 1, p_149951_3_, p_149951_4_) == this && (p_149951_1_.isSideSolid(p_149951_2_ - 1, p_149951_3_ + 1, p_149951_4_, DOWN) || isOcelotSittingOnTop(p_149951_1_, p_149951_2_ - 1, p_149951_3_, p_149951_4_)))
{
return null;
}
else if (p_149951_1_.getBlock(p_149951_2_ + 1, p_149951_3_, p_149951_4_) == this && (p_149951_1_.isSideSolid(p_149951_2_ + 1, p_149951_3_ + 1, p_149951_4_, DOWN) || isOcelotSittingOnTop(p_149951_1_, p_149951_2_ + 1, p_149951_3_, p_149951_4_)))
{
return null;
}
else if (p_149951_1_.getBlock(p_149951_2_, p_149951_3_, p_149951_4_ - 1) == this && (p_149951_1_.isSideSolid(p_149951_2_, p_149951_3_ + 1, p_149951_4_ - 1, DOWN) || isOcelotSittingOnTop(p_149951_1_, p_149951_2_, p_149951_3_, p_149951_4_ - 1)))
{
return null;
}
else if (p_149951_1_.getBlock(p_149951_2_, p_149951_3_, p_149951_4_ + 1) == this && (p_149951_1_.isSideSolid(p_149951_2_, p_149951_3_ + 1, p_149951_4_ + 1, DOWN) || isOcelotSittingOnTop(p_149951_1_, p_149951_2_, p_149951_3_, p_149951_4_ + 1)))
{
return null;
}
else
{
if (p_149951_1_.getBlock(p_149951_2_ - 1, p_149951_3_, p_149951_4_) == this)
{
object = new InventoryLargeChest("container.chestDouble", (TileEntityChest)p_149951_1_.getTileEntity(p_149951_2_ - 1, p_149951_3_, p_149951_4_), (IInventory)object);
}
if (p_149951_1_.getBlock(p_149951_2_ + 1, p_149951_3_, p_149951_4_) == this)
{
object = new InventoryLargeChest("container.chestDouble", (IInventory)object, (TileEntityChest)p_149951_1_.getTileEntity(p_149951_2_ + 1, p_149951_3_, p_149951_4_));
}
if (p_149951_1_.getBlock(p_149951_2_, p_149951_3_, p_149951_4_ - 1) == this)
{
object = new InventoryLargeChest("container.chestDouble", (TileEntityChest)p_149951_1_.getTileEntity(p_149951_2_, p_149951_3_, p_149951_4_ - 1), (IInventory)object);
}
if (p_149951_1_.getBlock(p_149951_2_, p_149951_3_, p_149951_4_ + 1) == this)
{
object = new InventoryLargeChest("container.chestDouble", (IInventory)object, (TileEntityChest)p_149951_1_.getTileEntity(p_149951_2_, p_149951_3_, p_149951_4_ + 1));
}
return (IInventory)object;
}
}
/**
* Returns a new instance of a block's tile entity class. Called on placing the block.
*/
public TileEntity createNewTileEntity(World worldIn, int meta)
{
TileEntityChest tileentitychest = new TileEntityChest();
return tileentitychest;
}
/**
* Can this block provide power. Only wire currently seems to have this change based on its state.
*/
public boolean canProvidePower()
{
return this.chestType == 1;
}
public int isProvidingWeakPower(IBlockAccess worldIn, int x, int y, int z, int side)
{
if (!this.canProvidePower())
{
return 0;
}
else
{
int i1 = ((TileEntityChest)worldIn.getTileEntity(x, y, z)).numPlayersUsing;
return MathHelper.clamp_int(i1, 0, 15);
}
}
public int isProvidingStrongPower(IBlockAccess worldIn, int x, int y, int z, int side)
{
return side == 1 ? this.isProvidingWeakPower(worldIn, x, y, z, side) : 0;
}
private static boolean isOcelotSittingOnTop(World p_149953_0_, int p_149953_1_, int p_149953_2_, int p_149953_3_)
{
Iterator iterator = p_149953_0_.getEntitiesWithinAABB(EntityOcelot.class, AxisAlignedBB.getBoundingBox((double)p_149953_1_, (double)(p_149953_2_ + 1), (double)p_149953_3_, (double)(p_149953_1_ + 1), (double)(p_149953_2_ + 2), (double)(p_149953_3_ + 1))).iterator();
EntityOcelot entityocelot;
do
{
if (!iterator.hasNext())
{
return false;
}
Entity entity = (Entity)iterator.next();
entityocelot = (EntityOcelot)entity;
}
while (!entityocelot.isSitting());
return true;
}
public boolean hasComparatorInputOverride()
{
return true;
}
public int getComparatorInputOverride(World worldIn, int x, int y, int z, int side)
{
return Container.calcRedstoneFromInventory(this.getInventory(worldIn, x, y, z));
}
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister reg)
{
this.blockIcon = reg.registerIcon("planks_oak");
}
}