Advertisement
Guest User

Custom Leaf Block

a guest
May 11th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.44 KB | None | 0 0
  1. package fantasy_biomes.blocks;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockLeavesBase;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.client.renderer.texture.IconRegister;
  11. import net.minecraft.creativetab.CreativeTabs;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.util.Icon;
  16. import net.minecraft.world.ColorizerFoliage;
  17. import net.minecraft.world.IBlockAccess;
  18. import net.minecraft.world.World;
  19. import net.minecraftforge.common.IShearable;
  20. import cpw.mods.fml.relauncher.Side;
  21. import cpw.mods.fml.relauncher.SideOnly;
  22. import fantasy_biomes.Main;
  23.  
  24. public class BlockTulipTreeLeaves extends BlockLeavesBase implements IShearable
  25. {
  26. public static final String[] TulipLEAF_TYPES = new String[] {"Tulip"};
  27. public static final String[][] field_94396_b = new String[][] {{"BlockTulipLeaves"}, {"BlockTulipLeaveOpaque"}};
  28. @SideOnly(Side.CLIENT)
  29.  
  30. /** 1 for fast graphic. 0 for fancy graphics. used in iconArray. */
  31. private int iconType;
  32. private Icon[][] iconArray = new Icon[2][];
  33. int[] adjacentTreeBlocks;
  34.  
  35. public BlockTulipTreeLeaves(int par1, int i)
  36. {
  37. super(par1, Material.leaves, false);
  38. this.setTickRandomly(true);
  39. this.setCreativeTab(CreativeTabs.tabDecorations);
  40. }
  41.  
  42. @SideOnly(Side.CLIENT)
  43.  
  44.  
  45. /**
  46. * Called on server worlds only when the block has been replaced by a different block ID, or the same block with a
  47. * different metadata value, but before the new metadata value is set. Args: World, x, y, z, old block ID, old
  48. * metadata
  49. */
  50. public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
  51. {
  52. byte b0 = 1;
  53. int j1 = b0 + 1;
  54.  
  55. if (par1World.checkChunksExist(par2 - j1, par3 - j1, par4 - j1, par2 + j1, par3 + j1, par4 + j1))
  56. {
  57. for (int k1 = -b0; k1 <= b0; ++k1)
  58. {
  59. for (int l1 = -b0; l1 <= b0; ++l1)
  60. {
  61. for (int i2 = -b0; i2 <= b0; ++i2)
  62. {
  63. int j2 = par1World.getBlockId(par2 + k1, par3 + l1, par4 + i2);
  64.  
  65. if (Block.blocksList[j2] != null)
  66. {
  67. Block.blocksList[j2].beginLeavesDecay(par1World, par2 + k1, par3 + l1, par4 + i2);
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. /**
  76. * Ticks the block if it's been scheduled
  77. */
  78. public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
  79. {
  80. if (!par1World.isRemote)
  81. {
  82. int l = par1World.getBlockMetadata(par2, par3, par4);
  83.  
  84. if ((l & 8) != 0 && (l & 4) == 0)
  85. {
  86. byte b0 = 4;
  87. int i1 = b0 + 1;
  88. byte b1 = 32;
  89. int j1 = b1 * b1;
  90. int k1 = b1 / 2;
  91.  
  92. if (this.adjacentTreeBlocks == null)
  93. {
  94. this.adjacentTreeBlocks = new int[b1 * b1 * b1];
  95. }
  96.  
  97. int l1;
  98.  
  99. if (par1World.checkChunksExist(par2 - i1, par3 - i1, par4 - i1, par2 + i1, par3 + i1, par4 + i1))
  100. {
  101. int i2;
  102. int j2;
  103. int k2;
  104.  
  105. for (l1 = -b0; l1 <= b0; ++l1)
  106. {
  107. for (i2 = -b0; i2 <= b0; ++i2)
  108. {
  109. for (j2 = -b0; j2 <= b0; ++j2)
  110. {
  111. k2 = par1World.getBlockId(par2 + l1, par3 + i2, par4 + j2);
  112.  
  113. Block block = Block.blocksList[k2];
  114.  
  115. if (block != null && block.canSustainLeaves(par1World, par2 + l1, par3 + i2, par4 + j2))
  116. {
  117. this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = 0;
  118. }
  119. else if (block != null && block.isLeaves(par1World, par2 + l1, par3 + i2, par4 + j2))
  120. {
  121. this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -2;
  122. }
  123. else
  124. {
  125. this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -1;
  126. }
  127. }
  128. }
  129. }
  130.  
  131. for (l1 = 1; l1 <= 4; ++l1)
  132. {
  133. for (i2 = -b0; i2 <= b0; ++i2)
  134. {
  135. for (j2 = -b0; j2 <= b0; ++j2)
  136. {
  137. for (k2 = -b0; k2 <= b0; ++k2)
  138. {
  139. if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1)
  140. {
  141. if (this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
  142. {
  143. this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
  144. }
  145.  
  146. if (this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
  147. {
  148. this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
  149. }
  150.  
  151. if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] == -2)
  152. {
  153. this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] = l1;
  154. }
  155.  
  156. if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] == -2)
  157. {
  158. this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] = l1;
  159. }
  160.  
  161. if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] == -2)
  162. {
  163. this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] = l1;
  164. }
  165.  
  166. if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] == -2)
  167. {
  168. this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] = l1;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176.  
  177. l1 = this.adjacentTreeBlocks[k1 * j1 + k1 * b1 + k1];
  178.  
  179. if (l1 >= 0)
  180. {
  181. par1World.setBlockMetadataWithNotify(par2, par3, par4, l & -9, 4);
  182. }
  183. else
  184. {
  185. this.removeLeaves(par1World, par2, par3, par4);
  186. }
  187. }
  188. }
  189. }
  190.  
  191. @SideOnly(Side.CLIENT)
  192.  
  193. /**
  194. * A randomly called display update to be able to add particles or other items for display
  195. */
  196. public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
  197. {
  198. if (par1World.canLightningStrikeAt(par2, par3 + 1, par4) && !par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4) && par5Random.nextInt(15) == 1)
  199. {
  200. double d0 = (double)((float)par2 + par5Random.nextFloat());
  201. double d1 = (double)par3 - 0.05D;
  202. double d2 = (double)((float)par4 + par5Random.nextFloat());
  203. par1World.spawnParticle("dripWater", d0, d1, d2, 0.0D, 0.0D, 0.0D);
  204. }
  205. }
  206.  
  207. private void removeLeaves(World par1World, int par2, int par3, int par4)
  208. {
  209. this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
  210. par1World.setBlockToAir(par2, par3, par4);
  211. }
  212.  
  213. /**
  214. * Returns the quantity of items to drop on block destruction.
  215. */
  216.  
  217. /**public int quantityDropped(Random par1Random)
  218. {
  219. return par1Random.nextInt(20) == 0 ? 1 : 0;
  220. }
  221. **/
  222.  
  223. /**
  224. * Returns the ID of the items to drop on destruction.
  225. */
  226. public int idDropped(int par1, Random par2Random, int par3)
  227. {
  228. return Main.blockTulipTreeSapling.blockID;
  229. }
  230.  
  231. /**
  232. * Drops the block items with a specified chance of dropping the specified items
  233. */
  234. public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
  235. {
  236. if (!par1World.isRemote)
  237. {
  238. int j1 = 20;
  239.  
  240. if ((par5 & 3) == 3)
  241. {
  242. j1 = 40;
  243. }
  244.  
  245. if (par7 > 0)
  246. {
  247. j1 -= 2 << par7;
  248.  
  249. if (j1 < 10)
  250. {
  251. j1 = 10;
  252. }
  253. }
  254.  
  255. if (par1World.rand.nextInt(j1) == 0)
  256. {
  257. int k1 = this.idDropped(par5, par1World.rand, par7);
  258. this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(k1, 1, this.damageDropped(par5)));
  259. }
  260.  
  261. j1 = 200;
  262.  
  263. if (par7 > 0)
  264. {
  265. j1 -= 10 << par7;
  266.  
  267. if (j1 < 40)
  268. {
  269. j1 = 40;
  270. }
  271. }
  272.  
  273. if ((par5 & 3) == 0 && par1World.rand.nextInt(j1) == 0)
  274. {
  275. this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(Item.appleRed, 1, 0));
  276. }
  277. }
  278. }
  279.  
  280. /**
  281. * Called when the player destroys a block with an item that can harvest it. (i, j, k) are the coordinates of the
  282. * block and l is the block's subtype/damage.
  283. */
  284. public void harvestBlock(World par1World, EntityPlayer par2EntityPlayer, int par3, int par4, int par5, int par6)
  285. {
  286. super.harvestBlock(par1World, par2EntityPlayer, par3, par4, par5, par6);
  287. }
  288.  
  289. /**
  290. * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two
  291. * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
  292. */
  293. public boolean isOpaqueCube()
  294. {
  295. return !this.graphicsLevel;
  296. }
  297.  
  298. @SideOnly(Side.CLIENT)
  299.  
  300. /**
  301. * Pass true to draw this block using fancy graphics, or false for fast graphics.
  302. */
  303. public void setGraphicsLevel(boolean par1)
  304. {
  305. this.graphicsLevel = par1;
  306. this.iconType = par1 ? 0 : 1;
  307. }
  308.  
  309. @SideOnly(Side.CLIENT)
  310.  
  311. /**
  312. * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
  313. */
  314. public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
  315. {
  316. par3List.add(new ItemStack(par1, 1, 0));
  317. }
  318.  
  319. /**
  320. * Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage
  321. * and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.
  322. */
  323. protected ItemStack createStackedBlock(int par1)
  324. {
  325. return new ItemStack(this.blockID, 1, par1 & 3);
  326. }
  327.  
  328. @SideOnly(Side.CLIENT)
  329.  
  330. /**
  331. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  332. * is the only chance you get to register icons.
  333. */
  334. public void registerIcons(IconRegister par1IconRegister)
  335. {
  336. for (int i = 0; i < field_94396_b.length; ++i)
  337. {
  338. this.iconArray[i] = new Icon[field_94396_b[i].length];
  339.  
  340. for (int j = 0; j < field_94396_b[i].length; ++j)
  341. {
  342. this.iconArray[i][j] = par1IconRegister.registerIcon(field_94396_b[i][j]);
  343. }
  344. }
  345. }
  346.  
  347. @Override
  348. public boolean isShearable(ItemStack item, World world, int x, int y, int z)
  349. {
  350. return true;
  351. }
  352.  
  353. @Override
  354. public ArrayList<ItemStack> onSheared(ItemStack item, World world, int x, int y, int z, int fortune)
  355. {
  356. ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
  357. ret.add(new ItemStack(this, 1, world.getBlockMetadata(x, y, z) & 3));
  358. return ret;
  359. }
  360.  
  361. @Override
  362. public void beginLeavesDecay(World world, int x, int y, int z)
  363. {
  364. world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) | 8, 4);
  365. }
  366.  
  367. @Override
  368. public boolean isLeaves(World world, int x, int y, int z)
  369. {
  370. return true;
  371. }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement