palmerjj01

BlockYoshiOven

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