/Client/src/net/minecraft/src/BlockChest.java

http://phantom-labs-mc.googlecode.com/ · Java · 633 lines · 478 code · 110 blank · 45 comment · 217 complexity · 1f776d5b926b5a5fddb94a404640c525 MD5 · raw file

  1. package net.minecraft.src;
  2. import java.util.*;
  3. public class BlockChest extends BlockContainer
  4. {
  5. private Random random;
  6. protected BlockChest(int par1)
  7. {
  8. super(par1, Material.wood);
  9. random = new Random();
  10. blockIndexInTexture = 26;
  11. }
  12. /**
  13. * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
  14. * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
  15. */
  16. public boolean isOpaqueCube()
  17. {
  18. return false;
  19. }
  20. /**
  21. * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
  22. */
  23. public boolean renderAsNormalBlock()
  24. {
  25. return false;
  26. }
  27. /**
  28. * The type of render function that is called for this block
  29. */
  30. public int getRenderType()
  31. {
  32. return 22;
  33. }
  34. /**
  35. * Called whenever the block is added into the world. Args: world, x, y, z
  36. */
  37. public void onBlockAdded(World par1World, int par2, int par3, int par4)
  38. {
  39. super.onBlockAdded(par1World, par2, par3, par4);
  40. unifyAdjacentChests(par1World, par2, par3, par4);
  41. int i = par1World.getBlockId(par2, par3, par4 - 1);
  42. int j = par1World.getBlockId(par2, par3, par4 + 1);
  43. int k = par1World.getBlockId(par2 - 1, par3, par4);
  44. int l = par1World.getBlockId(par2 + 1, par3, par4);
  45. if (i == blockID)
  46. {
  47. unifyAdjacentChests(par1World, par2, par3, par4 - 1);
  48. }
  49. if (j == blockID)
  50. {
  51. unifyAdjacentChests(par1World, par2, par3, par4 + 1);
  52. }
  53. if (k == blockID)
  54. {
  55. unifyAdjacentChests(par1World, par2 - 1, par3, par4);
  56. }
  57. if (l == blockID)
  58. {
  59. unifyAdjacentChests(par1World, par2 + 1, par3, par4);
  60. }
  61. }
  62. /**
  63. * Called when the block is placed in the world.
  64. */
  65. public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
  66. {
  67. int i = par1World.getBlockId(par2, par3, par4 - 1);
  68. int j = par1World.getBlockId(par2, par3, par4 + 1);
  69. int k = par1World.getBlockId(par2 - 1, par3, par4);
  70. int l = par1World.getBlockId(par2 + 1, par3, par4);
  71. byte byte0 = 0;
  72. int i1 = MathHelper.floor_double((double)((par5EntityLiving.rotationYaw * 4F) / 360F) + 0.5D) & 3;
  73. if (i1 == 0)
  74. {
  75. byte0 = 2;
  76. }
  77. if (i1 == 1)
  78. {
  79. byte0 = 5;
  80. }
  81. if (i1 == 2)
  82. {
  83. byte0 = 3;
  84. }
  85. if (i1 == 3)
  86. {
  87. byte0 = 4;
  88. }
  89. if (i != blockID && j != blockID && k != blockID && l != blockID)
  90. {
  91. par1World.setBlockMetadataWithNotify(par2, par3, par4, byte0);
  92. }
  93. else
  94. {
  95. if ((i == blockID || j == blockID) && (byte0 == 4 || byte0 == 5))
  96. {
  97. if (i == blockID)
  98. {
  99. par1World.setBlockMetadataWithNotify(par2, par3, par4 - 1, byte0);
  100. }
  101. else
  102. {
  103. par1World.setBlockMetadataWithNotify(par2, par3, par4 + 1, byte0);
  104. }
  105. par1World.setBlockMetadataWithNotify(par2, par3, par4, byte0);
  106. }
  107. if ((k == blockID || l == blockID) && (byte0 == 2 || byte0 == 3))
  108. {
  109. if (k == blockID)
  110. {
  111. par1World.setBlockMetadataWithNotify(par2 - 1, par3, par4, byte0);
  112. }
  113. else
  114. {
  115. par1World.setBlockMetadataWithNotify(par2 + 1, par3, par4, byte0);
  116. }
  117. par1World.setBlockMetadataWithNotify(par2, par3, par4, byte0);
  118. }
  119. }
  120. }
  121. /**
  122. * Turns the adjacent chests to a double chest.
  123. */
  124. public void unifyAdjacentChests(World par1World, int par2, int par3, int par4)
  125. {
  126. if (par1World.isRemote)
  127. {
  128. return;
  129. }
  130. int i = par1World.getBlockId(par2, par3, par4 - 1);
  131. int j = par1World.getBlockId(par2, par3, par4 + 1);
  132. int k = par1World.getBlockId(par2 - 1, par3, par4);
  133. int l = par1World.getBlockId(par2 + 1, par3, par4);
  134. byte byte0 = 4;
  135. if (i == blockID || j == blockID)
  136. {
  137. int i1 = par1World.getBlockId(par2 - 1, par3, i != blockID ? par4 + 1 : par4 - 1);
  138. int k1 = par1World.getBlockId(par2 + 1, par3, i != blockID ? par4 + 1 : par4 - 1);
  139. byte0 = 5;
  140. int i2 = -1;
  141. if (i == blockID)
  142. {
  143. i2 = par1World.getBlockMetadata(par2, par3, par4 - 1);
  144. }
  145. else
  146. {
  147. i2 = par1World.getBlockMetadata(par2, par3, par4 + 1);
  148. }
  149. if (i2 == 4)
  150. {
  151. byte0 = 4;
  152. }
  153. if ((Block.opaqueCubeLookup[k] || Block.opaqueCubeLookup[i1]) && !Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[k1])
  154. {
  155. byte0 = 5;
  156. }
  157. if ((Block.opaqueCubeLookup[l] || Block.opaqueCubeLookup[k1]) && !Block.opaqueCubeLookup[k] && !Block.opaqueCubeLookup[i1])
  158. {
  159. byte0 = 4;
  160. }
  161. }
  162. else if (k == blockID || l == blockID)
  163. {
  164. int j1 = par1World.getBlockId(k != blockID ? par2 + 1 : par2 - 1, par3, par4 - 1);
  165. int l1 = par1World.getBlockId(k != blockID ? par2 + 1 : par2 - 1, par3, par4 + 1);
  166. byte0 = 3;
  167. int j2 = -1;
  168. if (k == blockID)
  169. {
  170. j2 = par1World.getBlockMetadata(par2 - 1, par3, par4);
  171. }
  172. else
  173. {
  174. j2 = par1World.getBlockMetadata(par2 + 1, par3, par4);
  175. }
  176. if (j2 == 2)
  177. {
  178. byte0 = 2;
  179. }
  180. if ((Block.opaqueCubeLookup[i] || Block.opaqueCubeLookup[j1]) && !Block.opaqueCubeLookup[j] && !Block.opaqueCubeLookup[l1])
  181. {
  182. byte0 = 3;
  183. }
  184. if ((Block.opaqueCubeLookup[j] || Block.opaqueCubeLookup[l1]) && !Block.opaqueCubeLookup[i] && !Block.opaqueCubeLookup[j1])
  185. {
  186. byte0 = 2;
  187. }
  188. }
  189. else
  190. {
  191. byte0 = 3;
  192. if (Block.opaqueCubeLookup[i] && !Block.opaqueCubeLookup[j])
  193. {
  194. byte0 = 3;
  195. }
  196. if (Block.opaqueCubeLookup[j] && !Block.opaqueCubeLookup[i])
  197. {
  198. byte0 = 2;
  199. }
  200. if (Block.opaqueCubeLookup[k] && !Block.opaqueCubeLookup[l])
  201. {
  202. byte0 = 5;
  203. }
  204. if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[k])
  205. {
  206. byte0 = 4;
  207. }
  208. }
  209. par1World.setBlockMetadataWithNotify(par2, par3, par4, byte0);
  210. }
  211. /**
  212. * Retrieves the block texture to use based on the display side. Args: iBlockAccess, x, y, z, side
  213. */
  214. public int getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
  215. {
  216. if (par5 == 1)
  217. {
  218. return blockIndexInTexture - 1;
  219. }
  220. if (par5 == 0)
  221. {
  222. return blockIndexInTexture - 1;
  223. }
  224. int i = par1IBlockAccess.getBlockId(par2, par3, par4 - 1);
  225. int j = par1IBlockAccess.getBlockId(par2, par3, par4 + 1);
  226. int k = par1IBlockAccess.getBlockId(par2 - 1, par3, par4);
  227. int l = par1IBlockAccess.getBlockId(par2 + 1, par3, par4);
  228. if (i == blockID || j == blockID)
  229. {
  230. if (par5 == 2 || par5 == 3)
  231. {
  232. return blockIndexInTexture;
  233. }
  234. int i1 = 0;
  235. if (i == blockID)
  236. {
  237. i1 = -1;
  238. }
  239. int k1 = par1IBlockAccess.getBlockId(par2 - 1, par3, i != blockID ? par4 + 1 : par4 - 1);
  240. int i2 = par1IBlockAccess.getBlockId(par2 + 1, par3, i != blockID ? par4 + 1 : par4 - 1);
  241. if (par5 == 4)
  242. {
  243. i1 = -1 - i1;
  244. }
  245. byte byte1 = 5;
  246. if ((Block.opaqueCubeLookup[k] || Block.opaqueCubeLookup[k1]) && !Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i2])
  247. {
  248. byte1 = 5;
  249. }
  250. if ((Block.opaqueCubeLookup[l] || Block.opaqueCubeLookup[i2]) && !Block.opaqueCubeLookup[k] && !Block.opaqueCubeLookup[k1])
  251. {
  252. byte1 = 4;
  253. }
  254. return (par5 != byte1 ? blockIndexInTexture + 32 : blockIndexInTexture + 16) + i1;
  255. }
  256. if (k == blockID || l == blockID)
  257. {
  258. if (par5 == 4 || par5 == 5)
  259. {
  260. return blockIndexInTexture;
  261. }
  262. int j1 = 0;
  263. if (k == blockID)
  264. {
  265. j1 = -1;
  266. }
  267. int l1 = par1IBlockAccess.getBlockId(k != blockID ? par2 + 1 : par2 - 1, par3, par4 - 1);
  268. int j2 = par1IBlockAccess.getBlockId(k != blockID ? par2 + 1 : par2 - 1, par3, par4 + 1);
  269. if (par5 == 3)
  270. {
  271. j1 = -1 - j1;
  272. }
  273. byte byte2 = 3;
  274. if ((Block.opaqueCubeLookup[i] || Block.opaqueCubeLookup[l1]) && !Block.opaqueCubeLookup[j] && !Block.opaqueCubeLookup[j2])
  275. {
  276. byte2 = 3;
  277. }
  278. if ((Block.opaqueCubeLookup[j] || Block.opaqueCubeLookup[j2]) && !Block.opaqueCubeLookup[i] && !Block.opaqueCubeLookup[l1])
  279. {
  280. byte2 = 2;
  281. }
  282. return (par5 != byte2 ? blockIndexInTexture + 32 : blockIndexInTexture + 16) + j1;
  283. }
  284. byte byte0 = 3;
  285. if (Block.opaqueCubeLookup[i] && !Block.opaqueCubeLookup[j])
  286. {
  287. byte0 = 3;
  288. }
  289. if (Block.opaqueCubeLookup[j] && !Block.opaqueCubeLookup[i])
  290. {
  291. byte0 = 2;
  292. }
  293. if (Block.opaqueCubeLookup[k] && !Block.opaqueCubeLookup[l])
  294. {
  295. byte0 = 5;
  296. }
  297. if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[k])
  298. {
  299. byte0 = 4;
  300. }
  301. return par5 != byte0 ? blockIndexInTexture : blockIndexInTexture + 1;
  302. }
  303. /**
  304. * Returns the block texture based on the side being looked at. Args: side
  305. */
  306. public int getBlockTextureFromSide(int par1)
  307. {
  308. if (par1 == 1)
  309. {
  310. return blockIndexInTexture - 1;
  311. }
  312. if (par1 == 0)
  313. {
  314. return blockIndexInTexture - 1;
  315. }
  316. if (par1 == 3)
  317. {
  318. return blockIndexInTexture + 1;
  319. }
  320. else
  321. {
  322. return blockIndexInTexture;
  323. }
  324. }
  325. /**
  326. * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
  327. */
  328. public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
  329. {
  330. int i = 0;
  331. if (par1World.getBlockId(par2 - 1, par3, par4) == blockID)
  332. {
  333. i++;
  334. }
  335. if (par1World.getBlockId(par2 + 1, par3, par4) == blockID)
  336. {
  337. i++;
  338. }
  339. if (par1World.getBlockId(par2, par3, par4 - 1) == blockID)
  340. {
  341. i++;
  342. }
  343. if (par1World.getBlockId(par2, par3, par4 + 1) == blockID)
  344. {
  345. i++;
  346. }
  347. if (i > 1)
  348. {
  349. return false;
  350. }
  351. if (isThereANeighborChest(par1World, par2 - 1, par3, par4))
  352. {
  353. return false;
  354. }
  355. if (isThereANeighborChest(par1World, par2 + 1, par3, par4))
  356. {
  357. return false;
  358. }
  359. if (isThereANeighborChest(par1World, par2, par3, par4 - 1))
  360. {
  361. return false;
  362. }
  363. return !isThereANeighborChest(par1World, par2, par3, par4 + 1);
  364. }
  365. /**
  366. * Checks the neighbor blocks to see if there is a chest there. Args: world, x, y, z
  367. */
  368. private boolean isThereANeighborChest(World par1World, int par2, int par3, int par4)
  369. {
  370. if (par1World.getBlockId(par2, par3, par4) != blockID)
  371. {
  372. return false;
  373. }
  374. if (par1World.getBlockId(par2 - 1, par3, par4) == blockID)
  375. {
  376. return true;
  377. }
  378. if (par1World.getBlockId(par2 + 1, par3, par4) == blockID)
  379. {
  380. return true;
  381. }
  382. if (par1World.getBlockId(par2, par3, par4 - 1) == blockID)
  383. {
  384. return true;
  385. }
  386. return par1World.getBlockId(par2, par3, par4 + 1) == blockID;
  387. }
  388. /**
  389. * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
  390. * their own) Args: x, y, z, neighbor blockID
  391. */
  392. public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
  393. {
  394. super.onNeighborBlockChange(par1World, par2, par3, par4, par5);
  395. TileEntityChest tileentitychest = (TileEntityChest)par1World.getBlockTileEntity(par2, par3, par4);
  396. if (tileentitychest != null)
  397. {
  398. tileentitychest.updateContainingBlockInfo();
  399. }
  400. }
  401. /**
  402. * Called whenever the block is removed.
  403. */
  404. public void onBlockRemoval(World par1World, int par2, int par3, int par4)
  405. {
  406. TileEntityChest tileentitychest = (TileEntityChest)par1World.getBlockTileEntity(par2, par3, par4);
  407. if (tileentitychest != null)
  408. {
  409. for (int i = 0; i < tileentitychest.getSizeInventory(); i++)
  410. {
  411. ItemStack itemstack = tileentitychest.getStackInSlot(i);
  412. if (itemstack == null)
  413. {
  414. continue;
  415. }
  416. float f = random.nextFloat() * 0.8F + 0.1F;
  417. float f1 = random.nextFloat() * 0.8F + 0.1F;
  418. float f2 = random.nextFloat() * 0.8F + 0.1F;
  419. while (itemstack.stackSize > 0)
  420. {
  421. int j = random.nextInt(21) + 10;
  422. if (j > itemstack.stackSize)
  423. {
  424. j = itemstack.stackSize;
  425. }
  426. itemstack.stackSize -= j;
  427. EntityItem entityitem = new EntityItem(par1World, (float)par2 + f, (float)par3 + f1, (float)par4 + f2, new ItemStack(itemstack.itemID, j, itemstack.getItemDamage()));
  428. float f3 = 0.05F;
  429. entityitem.motionX = (float)random.nextGaussian() * f3;
  430. entityitem.motionY = (float)random.nextGaussian() * f3 + 0.2F;
  431. entityitem.motionZ = (float)random.nextGaussian() * f3;
  432. if (itemstack.hasTagCompound())
  433. {
  434. entityitem.item.setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  435. }
  436. par1World.spawnEntityInWorld(entityitem);
  437. }
  438. }
  439. }
  440. super.onBlockRemoval(par1World, par2, par3, par4);
  441. }
  442. /**
  443. * Called upon block activation (left or right click on the block.). The three integers represent x,y,z of the
  444. * block.
  445. */
  446. public boolean blockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer)
  447. {
  448. Object obj = (TileEntityChest)par1World.getBlockTileEntity(par2, par3, par4);
  449. if (obj == null)
  450. {
  451. return true;
  452. }
  453. if (par1World.isBlockNormalCube(par2, par3 + 1, par4))
  454. {
  455. return true;
  456. }
  457. if (func_50075_j(par1World, par2, par3, par4))
  458. {
  459. return true;
  460. }
  461. if (par1World.getBlockId(par2 - 1, par3, par4) == blockID && (par1World.isBlockNormalCube(par2 - 1, par3 + 1, par4) || func_50075_j(par1World, par2 - 1, par3, par4)))
  462. {
  463. return true;
  464. }
  465. if (par1World.getBlockId(par2 + 1, par3, par4) == blockID && (par1World.isBlockNormalCube(par2 + 1, par3 + 1, par4) || func_50075_j(par1World, par2 + 1, par3, par4)))
  466. {
  467. return true;
  468. }
  469. if (par1World.getBlockId(par2, par3, par4 - 1) == blockID && (par1World.isBlockNormalCube(par2, par3 + 1, par4 - 1) || func_50075_j(par1World, par2, par3, par4 - 1)))
  470. {
  471. return true;
  472. }
  473. if (par1World.getBlockId(par2, par3, par4 + 1) == blockID && (par1World.isBlockNormalCube(par2, par3 + 1, par4 + 1) || func_50075_j(par1World, par2, par3, par4 + 1)))
  474. {
  475. return true;
  476. }
  477. if (par1World.getBlockId(par2 - 1, par3, par4) == blockID)
  478. {
  479. obj = new InventoryLargeChest("Large chest", (TileEntityChest)par1World.getBlockTileEntity(par2 - 1, par3, par4), ((IInventory)(obj)));
  480. }
  481. if (par1World.getBlockId(par2 + 1, par3, par4) == blockID)
  482. {
  483. obj = new InventoryLargeChest("Large chest", ((IInventory)(obj)), (TileEntityChest)par1World.getBlockTileEntity(par2 + 1, par3, par4));
  484. }
  485. if (par1World.getBlockId(par2, par3, par4 - 1) == blockID)
  486. {
  487. obj = new InventoryLargeChest("Large chest", (TileEntityChest)par1World.getBlockTileEntity(par2, par3, par4 - 1), ((IInventory)(obj)));
  488. }
  489. if (par1World.getBlockId(par2, par3, par4 + 1) == blockID)
  490. {
  491. obj = new InventoryLargeChest("Large chest", ((IInventory)(obj)), (TileEntityChest)par1World.getBlockTileEntity(par2, par3, par4 + 1));
  492. }
  493. if (par1World.isRemote)
  494. {
  495. return true;
  496. }
  497. else
  498. {
  499. par5EntityPlayer.displayGUIChest(((IInventory)(obj)));
  500. return true;
  501. }
  502. }
  503. /**
  504. * Returns the TileEntity used by this block.
  505. */
  506. public TileEntity getBlockEntity()
  507. {
  508. return new TileEntityChest();
  509. }
  510. private static boolean func_50075_j(World par0World, int par1, int par2, int par3)
  511. {
  512. for (Iterator iterator = par0World.getEntitiesWithinAABB(net.minecraft.src.EntityOcelot.class, AxisAlignedBB.getBoundingBoxFromPool(par1, par2 + 1, par3, par1 + 1, par2 + 2, par3 + 1)).iterator(); iterator.hasNext();)
  513. {
  514. Entity entity = (Entity)iterator.next();
  515. EntityOcelot entityocelot = (EntityOcelot)entity;
  516. if (entityocelot.isSitting())
  517. {
  518. return true;
  519. }
  520. }
  521. return false;
  522. }
  523. }