Advertisement
XeNe

CombinerMachine

Jan 1st, 2015
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. package firstetestmod;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockContainer;
  7. import net.minecraft.block.material.Material;
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.client.renderer.texture.IIconRegister;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.entity.item.EntityItem;
  12. import net.minecraft.entity.player.EntityPlayer;
  13. import net.minecraft.inventory.Container;
  14. import net.minecraft.inventory.IInventory;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.tileentity.TileEntity;
  18. import net.minecraft.util.IIcon;
  19. import net.minecraft.util.MathHelper;
  20. import net.minecraft.world.World;
  21. import cpw.mods.fml.relauncher.Side;
  22. import cpw.mods.fml.relauncher.SideOnly;
  23.  
  24. public class CombinerMachine extends BlockContainer{
  25.  
  26. private final Random maceratorRand = new Random();
  27.  
  28.  
  29. private static boolean hasPower;
  30. private final boolean hasPower2;
  31.  
  32. private static boolean keepMaceratorInventory;
  33. @SideOnly(Side.CLIENT)
  34. private IIcon CombinerMachineIconTop;
  35. private IIcon CombinerMachineIconFront;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. public CombinerMachine(boolean isActive) {
  42. super(Material.rock);
  43.  
  44.  
  45. this.hasPower2 = isActive;
  46. }
  47.  
  48. @SideOnly(Side.CLIENT)
  49. public void registerBlockIcons(IIconRegister iconRegister){
  50. this.blockIcon = iconRegister.registerIcon("test:CombinerMachine");
  51. this.CombinerMachineIconTop = iconRegister.registerIcon(this.hasPower2 ? "test:CombinerMachinetopActive" : "test:CombinerMachinetop");
  52. this.CombinerMachineIconFront = iconRegister.registerIcon(this.hasPower2 ? "test:CombinerMachinefrontActive" : "test:CombinerMachinefrontInactive");
  53.  
  54. }
  55.  
  56. @SideOnly(Side.CLIENT)
  57.  
  58. /**
  59. * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
  60. */
  61. public IIcon getIcon(int side, int meta) {
  62. if (side == 1 ) {
  63. return this.CombinerMachineIconTop;
  64. } else if ((side == 5 && meta == 3) || (side == 3 && meta == 0) || (side == 2 && meta == 2) || (side == 4 && meta == 1)) {
  65. return this.CombinerMachineIconFront;
  66. }// else {
  67. // return this.blockIcon;
  68. // }
  69. return this.blockIcon;
  70. }
  71.  
  72. /**
  73. * Returns the ID of the items to drop on destruction.
  74. * @return
  75. */
  76. public Block getBlockDropped(int par1, Random par2Random, int par3)
  77. {
  78. return firsttestmodmain.CombinerMachine;
  79. }
  80.  
  81.  
  82. public TileEntity createNewTileEntity(World world, int p_149915_2_) {
  83. return null;
  84. }
  85.  
  86.  
  87. /**
  88. * Called whenever the block is added into the world. Args: world, x, y, z
  89. */
  90. public void onBlockAdded(World par1World, int par2, int par3, int par4)
  91. {
  92. super.onBlockAdded(par1World, par2, par3, par4);
  93. this.setDefaultDirection(par1World, par2, par3, par4);
  94. }
  95.  
  96. /**
  97. * set a blocks direction
  98. */
  99. private void setDefaultDirection(World world, int x, int y, int z)
  100. {
  101. if (!world.isRemote)
  102. {
  103. Block direction = world.getBlock(x, y, z - 1);
  104. Block direction1 = world.getBlock(x, y, z + 1);
  105. Block direction2 = world.getBlock(x - 1, y, z);
  106. Block direction3 = world.getBlock(x + 1, y, z);
  107. byte b0 = 3;
  108.  
  109. if (direction.func_149730_j() && !direction.func_149730_j())
  110. {
  111. b0 = 3;
  112. }
  113.  
  114. if (direction1.func_149730_j() && !direction1.func_149730_j())
  115. {
  116. b0 = 2;
  117. }
  118.  
  119. if (direction2.func_149730_j() && !direction2.func_149730_j())
  120. {
  121. b0 = 5;
  122. }
  123.  
  124. if (direction3.func_149730_j() && !direction3.func_149730_j())
  125. {
  126. b0 = 4;
  127. }
  128.  
  129. world.setBlockMetadataWithNotify(x, y, z, b0, 2);
  130. }
  131. }
  132.  
  133. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
  134. {
  135. player.openGui(firsttestmodmain.instance, firsttestmodmain.guiIdCombinerMachine, world, x, y, z);
  136.  
  137. return true;
  138. }
  139.  
  140.  
  141. /**
  142. * Update which block ID the furnace is using depending on whether or not it is burning
  143. */
  144. public static void updateFurnaceBlockState(boolean par0, World par1World, int par2, int par3, int par4)
  145. {
  146. int l = par1World.getBlockMetadata(par2, par3, par4);
  147. TileEntity tileentity = par1World.getTileEntity(par2, par3, par4);
  148. keepMaceratorInventory = true;
  149.  
  150. if (par0)
  151. {
  152. par1World.setBlock(par2, par3, par4, firsttestmodmain.CombinerMachineActive);
  153. }
  154. else
  155. {
  156. par1World.setBlock(par2, par3, par4, firsttestmodmain.CombinerMachine);
  157. }
  158.  
  159. keepMaceratorInventory = false;
  160. par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
  161.  
  162. if (tileentity != null)
  163. {
  164. tileentity.validate();
  165. par1World.setTileEntity(par2, par3, par4, tileentity);
  166. }
  167. }
  168.  
  169. /**
  170. * Returns a new instance of a block's tile entity class. Called on placing the block.
  171. */
  172. public TileEntity createNewTileEntity(World par1World)
  173. {
  174. return new TileEntityCombinerMachine();
  175. }
  176.  
  177. /**
  178. * Called when the block is placed in the world.
  179. */
  180. public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack itemstack)
  181. {
  182. int l = MathHelper.floor_double((double)(living.rotationYaw * 4.0F / 360.0F) + 2.5D) & 3;
  183. world.setBlockMetadataWithNotify(x, y, z, l, 2);
  184.  
  185. world.setBlockMetadataWithNotify(x, y, z, l, 2);
  186.  
  187. world.setBlockMetadataWithNotify(x, y, z, l, 2);
  188.  
  189. world.setBlockMetadataWithNotify(x, y, z, l, 2);
  190.  
  191. if (itemstack.hasDisplayName())
  192. {
  193. ((TileEntityCombinerMachine) world.getTileEntity(x, y, z)).CombinerMachineName(itemstack.getDisplayName());
  194. }
  195. }
  196.  
  197. /**
  198. * ejects contained items into the world, and notifies neighbours of an update, as appropriate
  199. */
  200. public void breakBlock(World world, int x, int y, int z, Block block, int meta)
  201. {
  202. if (!keepMaceratorInventory)
  203. {
  204. TileEntityCombinerMachine TileEntityCombinerMachine = (TileEntityCombinerMachine)world.getTileEntity( x, y, z);
  205.  
  206. if (TileEntityCombinerMachine != null)
  207. {
  208. for (int j1 = 0; j1 < TileEntityCombinerMachine.getSizeInventory(); ++j1)
  209. {
  210. ItemStack itemstack = TileEntityCombinerMachine.getStackInSlot(j1);
  211.  
  212. if (itemstack != null)
  213. {
  214. float f = this.maceratorRand.nextFloat() * 0.8F + 0.1F;
  215. float f1 = this.maceratorRand.nextFloat() * 0.8F + 0.1F;
  216. float f2 = this.maceratorRand.nextFloat() * 0.8F + 0.1F;
  217.  
  218. while (itemstack.stackSize > 0)
  219. {
  220. int k1 = this.maceratorRand.nextInt(21) + 10;
  221.  
  222. if (k1 > itemstack.stackSize)
  223. {
  224. k1 = itemstack.stackSize;
  225. }
  226.  
  227. itemstack.stackSize -= k1;
  228. EntityItem entityitem = new EntityItem(world, (double) ((float) x + f), (double) ((float) y + f1), (double) ((float) z + f2), new ItemStack(itemstack.getItem(), k1, itemstack.getItemDamage()));
  229.  
  230. if (itemstack.hasTagCompound())
  231. {
  232. entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  233. }
  234.  
  235. float f3 = 0.05F;
  236. entityitem.motionX = (double)((float)this.maceratorRand.nextGaussian() * f3);
  237. entityitem.motionY = (double)((float)this.maceratorRand.nextGaussian() * f3 + 0.2F);
  238. entityitem.motionZ = (double)((float)this.maceratorRand.nextGaussian() * f3);
  239. world.spawnEntityInWorld(entityitem);
  240. }
  241. }
  242. }
  243.  
  244. world.func_147453_f(x, y, z, block);
  245. }
  246. }
  247.  
  248. super.breakBlock(world, x, y, z, block, meta);
  249. }
  250.  
  251. /**
  252. * If this returns true, then comparators facing away from this block will use the value from
  253. * getComparatorInputOverride instead of the actual redstone signal strength.
  254. */
  255. public boolean hasComparatorInputOverride()
  256. {
  257. return true;
  258. }
  259.  
  260. /**
  261. * If hasComparatorInputOverride returns true, the return value from this is used instead of the redstone signal
  262. * strength when this block inputs to a comparator.
  263. */
  264. public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5)
  265. {
  266. return Container.calcRedstoneFromInventory((IInventory)par1World.getTileEntity(par2, par3, par4));
  267.  
  268. }
  269. @SideOnly(Side.CLIENT)
  270. public void randomDisplayTick(World world, int x, int y, int z, Random random) {
  271. if (this.hasPower2) {
  272. int direction = world.getBlockMetadata(x, y, z);
  273. float xx = (float) x + 0.5F, yy = (float) y + random.nextFloat() * 6.0F / 16.0F, zz = (float) z + 0.5F, xx2 = random.nextFloat() * 0.3F - 0.2F, zz2 = 0.5F;
  274. if (direction == 4) {
  275. world.spawnParticle("smoke", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  276. world.spawnParticle("flame", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  277. } else if (direction == 5) {
  278. world.spawnParticle("smoke", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  279. world.spawnParticle("flame", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  280. } else if (direction == 3) {
  281. world.spawnParticle("smoke", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  282. world.spawnParticle("flame", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  283. } else if (direction == 2) {
  284. world.spawnParticle("smoke", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  285. world.spawnParticle("flame", (double) (xx - zz2), (double) yy, (double) (zz + xx2), 0.0F, 0.0F, 0.0F);
  286. }
  287. }
  288. }
  289.  
  290.  
  291.  
  292.  
  293.  
  294. @SideOnly(Side.CLIENT)
  295.  
  296. /**
  297. * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
  298. */
  299. public Block getBlockPicked(World par1World, int par2, int par3, int par4)
  300. {
  301. return firsttestmodmain.CombinerMachine;
  302. }
  303.  
  304.  
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement