Guest User

Untitled

a guest
May 7th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.73 KB | None | 0 0
  1. package TIRC.common;
  2.  
  3. import cpw.mods.fml.relauncher.Side;
  4. import cpw.mods.fml.relauncher.SideOnly;
  5. import java.util.Random;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockContainer;
  9. import net.minecraft.block.material.Material;
  10. import net.minecraft.client.renderer.texture.IconRegister;
  11. import net.minecraft.entity.EntityLiving;
  12. import net.minecraft.entity.item.EntityItem;
  13. import net.minecraft.entity.player.EntityPlayer;
  14. import net.minecraft.inventory.Container;
  15. import net.minecraft.inventory.IInventory;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.Icon;
  20. import net.minecraft.util.MathHelper;
  21. import net.minecraft.world.World;
  22.  
  23. public class Sourcer extends BlockContainer
  24. {
  25. /**
  26. * Is the random generator used by furnace to drop the inventory contents in random directions.
  27. */
  28. private final Random furnaceRand = new Random();
  29.  
  30. /** True if this is an active furnace, false if idle */
  31. private final boolean isActive;
  32.  
  33. /**
  34. * This flag is used to prevent the furnace inventory to be dropped upon block removal, is used internally when the
  35. * furnace block changes from idle to active and vice-versa.
  36. */
  37. private static boolean keepFurnaceInventory = false;
  38. @SideOnly(Side.CLIENT)
  39. private Icon field_94458_cO;
  40. @SideOnly(Side.CLIENT)
  41. private Icon field_94459_cP;
  42.  
  43. protected Sourcer(int par1, boolean par2)
  44. {
  45. super(par1, Material.rock);
  46. this.isActive = par2;
  47. }
  48.  
  49. /**
  50. * Returns the ID of the items to drop on destruction.
  51. */
  52. public int idDropped(int par1, Random par2Random, int par3)
  53. {
  54. return TheInnerRealmsChronicles.SourcerIdle.blockID;
  55. }
  56.  
  57. /**
  58. * Called whenever the block is added into the world. Args: world, x, y, z
  59. */
  60. public void onBlockAdded(World par1World, int par2, int par3, int par4)
  61. {
  62. super.onBlockAdded(par1World, par2, par3, par4);
  63. this.setDefaultDirection(par1World, par2, par3, par4);
  64. }
  65.  
  66. /**
  67. * set a blocks direction
  68. */
  69. private void setDefaultDirection(World par1World, int par2, int par3, int par4)
  70. {
  71. if (!par1World.isRemote)
  72. {
  73. int l = par1World.getBlockId(par2, par3, par4 - 1);
  74. int i1 = par1World.getBlockId(par2, par3, par4 + 1);
  75. int j1 = par1World.getBlockId(par2 - 1, par3, par4);
  76. int k1 = par1World.getBlockId(par2 + 1, par3, par4);
  77. byte b0 = 3;
  78.  
  79. if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
  80. {
  81. b0 = 3;
  82. }
  83.  
  84. if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
  85. {
  86. b0 = 2;
  87. }
  88.  
  89. if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
  90. {
  91. b0 = 5;
  92. }
  93.  
  94. if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
  95. {
  96. b0 = 4;
  97. }
  98.  
  99. par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
  100. }
  101. }
  102.  
  103. @SideOnly(Side.CLIENT)
  104.  
  105. /**
  106. * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
  107. */
  108. public Icon getBlockTextureFromSideAndMetadata(int par1, int par2)
  109. {
  110. return par1 == 1 ? this.field_94458_cO : (par1 == 0 ? this.field_94458_cO : (par1 != par2 ? this.blockIcon : this.field_94459_cP));
  111. }
  112.  
  113. @SideOnly(Side.CLIENT)
  114.  
  115. /**
  116. * When this method is called, your block should register all the icons it needs with the given IconRegister. This
  117. * is the only chance you get to register icons.
  118. */
  119. public void registerIcons(IconRegister par1IconRegister)
  120. {
  121. this.blockIcon = par1IconRegister.registerIcon("furnace_side");
  122. this.field_94459_cP = par1IconRegister.registerIcon(this.isActive ? "furnace_front_lit" : "furnace_front");
  123. this.field_94458_cO = par1IconRegister.registerIcon("furnace_top");
  124. }
  125.  
  126. /**
  127. * Called upon block activation (right click on the block.)
  128. */
  129. public boolean onBlockActivated(World par1World, int z, int y, int x, EntityPlayer player, int par6, float par7, float par8, float par9)
  130. {
  131. TileEntitySourcer tileentityfurnace = (TileEntitySourcer)par1World.getBlockTileEntity(x, y, z);
  132.  
  133. if (tileentityfurnace == null || player.isSneaking())
  134. {
  135.   return false;
  136. }
  137. player.openGui(TheInnerRealmsChronicles.instance, 1, par1World, x, y, z);
  138.  
  139.   return true;
  140. }
  141.  
  142. /**
  143. * Update which block ID the furnace is using depending on whether or not it is burning
  144. */
  145. public static void updateFurnaceBlockState(boolean par0, World par1World, int par2, int par3, int par4)
  146. {
  147. int l = par1World.getBlockMetadata(par2, par3, par4);
  148. TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
  149. keepFurnaceInventory = true;
  150.  
  151. if (par0)
  152. {
  153. par1World.setBlock(par2, par3, par4, TheInnerRealmsChronicles.SourcerBurning.blockID);
  154. }
  155. else
  156. {
  157. par1World.setBlock(par2, par3, par4, TheInnerRealmsChronicles.SourcerIdle.blockID);
  158. }
  159.  
  160. keepFurnaceInventory = false;
  161. par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
  162.  
  163. if (tileentity != null)
  164. {
  165. tileentity.validate();
  166. par1World.setBlockTileEntity(par2, par3, par4, tileentity);
  167. }
  168. }
  169.  
  170. @SideOnly(Side.CLIENT)
  171.  
  172. /**
  173. * A randomly called display update to be able to add particles or other items for display
  174. */
  175. public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
  176. {
  177. if (this.isActive)
  178. {
  179. int l = par1World.getBlockMetadata(par2, par3, par4);
  180. float f = (float)par2 + 0.5F;
  181. float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
  182. float f2 = (float)par4 + 0.5F;
  183. float f3 = 0.52F;
  184. float f4 = par5Random.nextFloat() * 0.6F - 0.3F;
  185.  
  186. if (l == 4)
  187. {
  188. par1World.spawnParticle("smoke", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  189. par1World.spawnParticle("flame", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  190. }
  191. else if (l == 5)
  192. {
  193. par1World.spawnParticle("smoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  194. par1World.spawnParticle("flame", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
  195. }
  196. else if (l == 2)
  197. {
  198. par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
  199. par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
  200. }
  201. else if (l == 3)
  202. {
  203. par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
  204. par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
  205. }
  206. }
  207. }
  208.  
  209. /**
  210. * Returns a new instance of a block's tile entity class. Called on placing the block.
  211. */
  212. public TileEntity createNewTileEntity(World par1World)
  213. {
  214. return new TileEntitySourcer();
  215. }
  216.  
  217. /**
  218. * Called when the block is placed in the world.
  219. */
  220. public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
  221. {
  222. int l = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
  223.  
  224. if (l == 0)
  225. {
  226. par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
  227. }
  228.  
  229. if (l == 1)
  230. {
  231. par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
  232. }
  233.  
  234. if (l == 2)
  235. {
  236. par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
  237. }
  238.  
  239. if (l == 3)
  240. {
  241. par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
  242. }
  243.  
  244. if (par6ItemStack.hasDisplayName())
  245. {
  246. ((TileEntitySourcer)par1World.getBlockTileEntity(par2, par3, par4)).func_94129_a(par6ItemStack.getDisplayName());
  247. }
  248. }
  249.  
  250. /**
  251. * ejects contained items into the world, and notifies neighbours of an update, as appropriate
  252. */
  253. public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
  254. {
  255. if (!keepFurnaceInventory)
  256. {
  257. TileEntitySourcer tileentityfurnace = (TileEntitySourcer)par1World.getBlockTileEntity(par2, par3, par4);
  258.  
  259. if (tileentityfurnace != null)
  260. {
  261. for (int j1 = 0; j1 < tileentityfurnace.getSizeInventory(); ++j1)
  262. {
  263. ItemStack itemstack = tileentityfurnace.getStackInSlot(j1);
  264.  
  265. if (itemstack != null)
  266. {
  267. float f = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
  268. float f1 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
  269. float f2 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
  270.  
  271. while (itemstack.stackSize > 0)
  272. {
  273. int k1 = this.furnaceRand.nextInt(21) + 10;
  274.  
  275. if (k1 > itemstack.stackSize)
  276. {
  277. k1 = itemstack.stackSize;
  278. }
  279.  
  280. itemstack.stackSize -= k1;
  281. EntityItem entityitem = new EntityItem(par1World, (double)((float)par2 + f), (double)((float)par3 + f1), (double)((float)par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));
  282.  
  283. if (itemstack.hasTagCompound())
  284. {
  285. entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  286. }
  287.  
  288. float f3 = 0.05F;
  289. entityitem.motionX = (double)((float)this.furnaceRand.nextGaussian() * f3);
  290. entityitem.motionY = (double)((float)this.furnaceRand.nextGaussian() * f3 + 0.2F);
  291. entityitem.motionZ = (double)((float)this.furnaceRand.nextGaussian() * f3);
  292. par1World.spawnEntityInWorld(entityitem);
  293. }
  294. }
  295. }
  296.  
  297. par1World.func_96440_m(par2, par3, par4, par5);
  298. }
  299. }
  300.  
  301. super.breakBlock(par1World, par2, par3, par4, par5, par6);
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment