Guest User

SauceMaker.java

a guest
Dec 5th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. package com.morefood.mod.machines;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.morefood.mod.Morefood;
  6. import com.morefood.mod.tileentity.TileEntitySauceMaker;
  7.  
  8. import net.minecraft.block.Block;
  9. import net.minecraft.block.BlockContainer;
  10. import net.minecraft.block.material.Material;
  11. import net.minecraft.client.renderer.texture.IIconRegister;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.entity.item.EntityItem;
  14. import net.minecraft.entity.player.EntityPlayer;
  15. import net.minecraft.item.Item;
  16. import net.minecraft.item.ItemStack;
  17. import net.minecraft.nbt.NBTTagCompound;
  18. import net.minecraft.tileentity.TileEntity;
  19. import net.minecraft.util.IIcon;
  20. import net.minecraft.util.MathHelper;
  21. import net.minecraft.world.World;
  22. import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
  23. import cpw.mods.fml.relauncher.Side;
  24. import cpw.mods.fml.relauncher.SideOnly;
  25.  
  26. public class SauceMaker extends BlockContainer {
  27.  
  28. private final boolean isActive;
  29.  
  30. @SideOnly(Side.CLIENT)
  31. private IIcon iconFront;
  32.  
  33. @SideOnly(Side.CLIENT)
  34. private IIcon iconTop;
  35.  
  36. private static boolean keepInventory;
  37. private Random rand = new Random();
  38.  
  39. public SauceMaker(boolean isActive) {
  40. super(Material.iron);
  41.  
  42. this.isActive = isActive;
  43. this.setHarvestLevel("pickaxe", 1);
  44.  
  45. }
  46.  
  47. @SideOnly(Side.CLIENT)
  48. public void registerBlockIcons(IIconRegister iconRegister) {
  49. this.blockIcon = iconRegister.registerIcon(Morefood.modid + ":" + "SauceMakerSide");
  50. this.iconFront = iconRegister.registerIcon(Morefood.modid + ":" + (this.isActive ? "SauceMakerFrontOn" : "SauceMakerFrontOff"));
  51. this.iconTop = iconRegister.registerIcon(Morefood.modid + ":" + "SauceMakerTop");
  52. }
  53.  
  54. @SideOnly(Side.CLIENT)
  55. public IIcon getIcon(int side, int metadata) {
  56. return metadata == 0 && side == 3 ? this.iconFront : side == 1 ? this.iconTop : (side == 0 ? this.iconTop : (side == metadata ? this.iconFront : this.blockIcon));
  57. }
  58.  
  59. public Item getItemDropped(int i, Random random, int j) {
  60. return Item.getItemFromBlock(Morefood.blockSauceMakerIdle);
  61. }
  62.  
  63. public void onBlockAdded(World world, int x, int y, int z) {
  64. super.onBlockAdded(world, x, y, z);
  65. this.setDefaultDirection(world, x, y, z);
  66. }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. private void setDefaultDirection(World world, int x, int y, int z) {
  74. if(!world.isRemote) {
  75. Block b1 = world.getBlock(x, y, z - 1);
  76. Block b2 = world.getBlock(x, y, z + 1);
  77. Block b3 = world.getBlock(x - 1, y, z);
  78. Block b4 = world.getBlock(x + 1, y, z);
  79.  
  80. byte b0 = 3;
  81.  
  82. if(b1.func_149730_j() && !b2.func_149730_j()) {
  83. b0 = 3;
  84. }
  85.  
  86. if(b2.func_149730_j() && !b1.func_149730_j()) {
  87. b0 = 2;
  88. }
  89.  
  90. if(b3.func_149730_j() && !b4.func_149730_j()) {
  91. b0 = 5;
  92. }
  93.  
  94. if(b4.func_149730_j() && !b3.func_149730_j()) {
  95. b0 = 4;
  96. }
  97.  
  98. world.setBlockMetadataWithNotify(x, y, z, b0, 2);
  99. }
  100.  
  101. }
  102.  
  103. public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float hitX, float hitY, float hitZ) {
  104. if (world.isRemote) {
  105. return true;
  106. }else if (!player.isSneaking()) {
  107. TileEntitySauceMaker entity = (TileEntitySauceMaker) world.getTileEntity(x, y, z);
  108. if(entity != null) {
  109. FMLNetworkHandler.openGui(player, Morefood.instance, Morefood.GuiIDSauceMaker, world, x, y, z);
  110. }
  111. return true;
  112. }else{
  113. return false;
  114. }
  115. }
  116.  
  117. @Override
  118. public TileEntity createNewTileEntity(World world, int i) {
  119. return new TileEntitySauceMaker();
  120. }
  121.  
  122. @SideOnly(Side.CLIENT)
  123. public void randomDisplayTick(World world, int x, int y, int z, Random random) {
  124. if(this.isActive) {
  125. int direction = world.getBlockMetadata(x, y, z);
  126.  
  127. float x1 = (float)x + 0.5F;
  128. float y1 = (float)y + random.nextFloat();
  129. float z1 = (float)z + 0.5F;
  130.  
  131. float f = 0.52F;
  132. float f1 = random.nextFloat() * 0.5F - 0.3F;
  133.  
  134. if(direction == 4) {
  135. world.spawnParticle("smoke", (double)(x1 - f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  136. world.spawnParticle("flame", (double)(x1 - f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  137. world.spawnParticle("spellMob", (double)(x1 - f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  138. }
  139.  
  140. if(direction == 5) {
  141. world.spawnParticle("smoke", (double)(x1 + f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  142. world.spawnParticle("flame", (double)(x1 + f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  143. world.spawnParticle("spellMob", (double)(x1 + f), (double)(y1), (double)(z1 + f1), 0D, 0D, 0D);
  144. }
  145.  
  146. if(direction == 2) {
  147. world.spawnParticle("smoke", (double)(x1 + f1), (double)(y1), (double)(z1 - f), 0D, 0D, 0D);
  148. world.spawnParticle("flame", (double)(x1 + f1), (double)(y1), (double)(z1 - f), 0D, 0D, 0D);
  149. world.spawnParticle("spellMob", (double)(x1 + f1), (double)(y1), (double)(z1 - f), 0D, 0D, 0D);
  150. }
  151.  
  152. if(direction == 3) {
  153. world.spawnParticle("smoke", (double)(x1 + f1), (double)(y1), (double)(z1 + f), 0D, 0D, 0D);
  154. world.spawnParticle("flame", (double)(x1 + f1), (double)(y1), (double)(z1 + f), 0D, 0D, 0D);
  155. world.spawnParticle("spellMob", (double)(x1 + f1), (double)(y1), (double)(z1 + f), 0D, 0D, 0D);
  156. }
  157. }
  158. }
  159.  
  160. public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entityplayer, ItemStack itemstack) {
  161. int l = MathHelper.floor_double((double)(entityplayer.rotationYaw * 4.0F / 360.F) + 0.5D) & 3;
  162.  
  163. if(l == 0 ) {
  164. world.setBlockMetadataWithNotify(x, y, z, 2, 2);
  165. }
  166.  
  167. if(l == 1 ) {
  168. world.setBlockMetadataWithNotify(x, y, z, 5, 2);
  169. }
  170.  
  171. if(l == 2 ) {
  172. world.setBlockMetadataWithNotify(x, y, z, 3, 2);
  173. }
  174.  
  175. if(l == 3 ) {
  176. world.setBlockMetadataWithNotify(x, y, z, 4, 2);
  177. }
  178.  
  179. if(itemstack.hasDisplayName()) {
  180. ((TileEntitySauceMaker)world.getTileEntity(x, y, z)).setGuiDisplayName(itemstack.getDisplayName());
  181. }
  182. }
  183.  
  184. public static void updateSauceMakerBlockState(boolean active, World worldObj, int xCoord, int yCoord, int zCoord) {
  185. int i = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
  186.  
  187. TileEntity tileentity = worldObj.getTileEntity(xCoord, yCoord, zCoord);
  188. keepInventory = true;
  189.  
  190. if(active) {
  191. worldObj.setBlock(xCoord, yCoord, zCoord, Morefood.blockSauceMakerActive);
  192. }else{
  193. worldObj.setBlock(xCoord, yCoord, zCoord, Morefood.blockSauceMakerIdle);
  194. }
  195.  
  196. keepInventory = false;
  197.  
  198. worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, i, 2);
  199.  
  200. if(tileentity != null) {
  201. tileentity.validate();
  202. worldObj.setTileEntity(xCoord, yCoord, zCoord, tileentity);
  203. }
  204.  
  205. }
  206.  
  207. public void breakBlock(World world, int x, int y, int z, Block oldblock, int oldMetadata) {
  208. if(!keepInventory) {
  209. TileEntitySauceMaker tileentity = (TileEntitySauceMaker) world.getTileEntity(x, y, z);
  210.  
  211. if(tileentity != null) {
  212. for (int i = 0; i < tileentity.getSizeInventory(); i++) {
  213. ItemStack itemstack = tileentity.getStackInSlot(i);
  214.  
  215. if(itemstack != null) {
  216. float f = this.rand.nextFloat() * 0.8F + 0.1F;
  217. float f1 = this.rand.nextFloat() * 0.8F + 0.1F;
  218. float f2 = this.rand.nextFloat() * 0.8F + 0.1F;
  219.  
  220. while(itemstack.stackSize > 0) {
  221. int j = this.rand.nextInt(21) + 10;
  222.  
  223. if(j > itemstack.stackSize) {
  224. j = itemstack.stackSize;
  225. }
  226.  
  227. itemstack.stackSize -= j;
  228.  
  229. EntityItem item = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j, itemstack.getItemDamage()));
  230.  
  231. if(itemstack.hasTagCompound()) {
  232. item.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
  233. }
  234.  
  235. world.spawnEntityInWorld(item);
  236. }
  237. }
  238. }
  239.  
  240. world.func_147453_f(x, y, z, oldblock);
  241. }
  242. }
  243.  
  244. super.breakBlock(world, x, y, z, oldblock, oldMetadata);
  245. }
  246.  
  247. public Item getItem(World world, int x, int y, int z) {
  248. return Item.getItemFromBlock(Morefood.blockSauceMakerIdle);
  249. }
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment