Guest User

MangoLeaves.java

a guest
Apr 16th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.49 KB | None | 0 0
  1. package com.chef.mod.blocks;
  2.  
  3. import java.awt.Color;
  4. import java.util.Random;
  5.  
  6. import net.minecraft.block.Block;
  7. import net.minecraft.block.BlockLeaves;
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.client.renderer.texture.IIconRegister;
  10. import net.minecraft.entity.EntityLivingBase;
  11. import net.minecraft.init.Blocks;
  12. import net.minecraft.item.Item;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.util.IIcon;
  15. import net.minecraft.world.ColorizerFoliage;
  16. import net.minecraft.world.IBlockAccess;
  17. import net.minecraft.world.World;
  18. import net.minecraft.world.biome.BiomeGenBase;
  19. import net.minecraft.world.biome.BiomeGenBase.TempCategory;
  20. import net.minecraft.world.biome.BiomeGenDesert;
  21. import net.minecraft.world.biome.BiomeGenJungle;
  22. import net.minecraft.world.biome.BiomeGenSavanna;
  23. import net.minecraft.world.biome.BiomeGenTaiga;
  24. import net.minecraftforge.common.util.ForgeDirection;
  25.  
  26. import com.chef.mod.Debugger;
  27. import com.chef.mod.Reference;
  28. import com.chef.mod.init.MyBlocks;
  29.  
  30. import cpw.mods.fml.relauncher.Side;
  31. import cpw.mods.fml.relauncher.SideOnly;
  32.  
  33. public class MangoLeaves extends BlockLeaves {
  34.  
  35.     int[] fieldA;
  36.     @SideOnly(Side.CLIENT)
  37.     private IIcon[] iconArray;
  38.     private boolean recentlyGrownMango = false;
  39.     private int recentlyGrownMangoTime = 0;
  40.  
  41.     @SideOnly(Side.CLIENT)
  42.     public int getRenderColor(int i)
  43.     {
  44.         return (i & 3) == 1 ? ColorizerFoliage.getFoliageColorPine() : ((i & 3) == 2 ? ColorizerFoliage.getFoliageColorBirch() : super.getRenderColor(i));
  45.     }
  46.    
  47.     @Override
  48.     protected int func_150123_b(int metadata)
  49.     {
  50.         return 100;
  51.     }
  52.    
  53.     @Override
  54.     public int getFireSpreadSpeed(IBlockAccess world, int x, int y, int z, ForgeDirection face)
  55.     {
  56.         return 30;
  57.     }
  58.    
  59.     @Override
  60.     public int getFlammability(IBlockAccess world, int x, int y, int z, ForgeDirection face)
  61.     {
  62.         return 60;
  63.     }
  64.  
  65.     /**
  66.      * Called when fire is updating, checks if a block face can catch fire.
  67.      *
  68.      *
  69.      * @param world The current world
  70.      * @param x The blocks X position
  71.      * @param y The blocks Y position
  72.      * @param z The blocks Z position
  73.      * @param face The face that the fire is coming from
  74.      * @return True if the face can be on fire, false otherwise.
  75.      */
  76.     public boolean isFlammable(IBlockAccess world, int x, int y, int z, ForgeDirection face)
  77.     {
  78.         return true;
  79.     }
  80.  
  81.     /**
  82.      * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
  83.      * when first determining what to render.
  84.      */
  85.     @SideOnly(Side.CLIENT)
  86.     public int colorMultiplier(IBlockAccess world, int x, int y, int z)
  87.     {
  88.        
  89.         Color c = new Color(world.getBiomeGenForCoords(x, z).getBiomeFoliageColor(x, y, z));
  90.  
  91.         return c.brighter().getRGB();
  92.     }
  93.    
  94.     public int getDamageValue(World world, int x, int y, int z) {
  95.         return world.getBlockMetadata(x, y, z);
  96.     }
  97.  
  98.     @SideOnly(Side.CLIENT)
  99.     public IIcon getIcon(int side, int meta) {
  100.  
  101.         this.setGraphicsLevel(Minecraft.getMinecraft().gameSettings.fancyGraphics);
  102.  
  103.         return this.iconArray[this.field_150127_b];
  104.  
  105.     }
  106.  
  107.     @SideOnly(Side.CLIENT)
  108.     public void registerBlockIcons(IIconRegister iconRegister) {
  109.  
  110.         this.iconArray = new IIcon[2];
  111.  
  112.         this.iconArray[0] = iconRegister.registerIcon(Reference.MOD_ID + ":" + this.getUnlocalizedName().substring(5));
  113.         this.iconArray[1] = iconRegister.registerIcon(Reference.MOD_ID + ":" + this.getUnlocalizedName().substring(5) + "_opaque");
  114.     }
  115.  
  116.     public void updateTick(World worldIn, int x, int y, int z, Random rand) {
  117.  
  118.         int metadata = worldIn.getBlockMetadata(x, y, z);
  119.        
  120.         super.updateTick(worldIn, x, y, z, rand);
  121.        
  122.         if (worldIn.getBlockLightValue(x, y, z) >= 5) {
  123.            
  124.             if (metadata == 1) {
  125.  
  126.                 if (this.canGrowMango(worldIn, x, y, z, rand)) {
  127.  
  128.                     if (rand.nextInt(calculateGrowthSpeed(worldIn, x, y, z, rand)) == 0) {
  129.  
  130.                         this.growMango(worldIn, x, y - 1, z, rand);
  131.                         recentlyGrownMangoTime = 200;
  132.  
  133.                     }
  134.  
  135.                 }
  136.  
  137.                 if (recentlyGrownMangoTime > 0) {
  138.  
  139.                     recentlyGrownMango = true;
  140.                     recentlyGrownMangoTime--;
  141.  
  142.                 } else {
  143.  
  144.                     recentlyGrownMango = false;
  145.  
  146.                 }
  147.             }
  148.         }
  149.     }
  150.  
  151.     public int calculateGrowthSpeed(World worldIn, int x, int y, int z, Random rand) {
  152.  
  153.         BiomeGenBase biome = worldIn.getBiomeGenForCoords(x, z);
  154.  
  155.         TempCategory tempCategory = biome.getTempCategory();
  156.  
  157.         // The Growth speed the plant will have
  158.         int growthSpeed = 20;
  159.  
  160.         // The extra growth speed
  161.         int extraGrowthSpeed = 0;
  162.  
  163.         // The extraSpeed you get from the light value
  164.         int extraSpeedLightValue = Math.round(worldIn.getBlockLightValue(x, y, z) / 3);
  165.  
  166.         // The extra Speed you get from the biome temperature and biome
  167.         int biomeExtraSpeed = Math.round(biome.temperature);
  168.  
  169.         if (tempCategory == biome.getTempCategory().COLD) {
  170.  
  171.             biomeExtraSpeed -= 3;
  172.  
  173.         } else if (tempCategory == biome.getTempCategory().MEDIUM) {
  174.  
  175.             biomeExtraSpeed += 5;
  176.  
  177.         } else if (tempCategory == biome.getTempCategory().WARM) {
  178.  
  179.             biomeExtraSpeed -= 2;
  180.  
  181.         }
  182.  
  183.         if (biome instanceof BiomeGenJungle) {
  184.  
  185.             biomeExtraSpeed += 3;
  186.  
  187.         } else if (biome instanceof BiomeGenTaiga) {
  188.  
  189.             biomeExtraSpeed -= 2;
  190.  
  191.         } else if (biome instanceof BiomeGenDesert) {
  192.  
  193.             biomeExtraSpeed -= 2;
  194.  
  195.         } else if (biome instanceof BiomeGenSavanna) {
  196.  
  197.             biomeExtraSpeed -= 1;
  198.  
  199.         }
  200.        
  201.         if (worldIn.isRaining()) {
  202.            
  203.             extraGrowthSpeed += 3;
  204.            
  205.         }
  206.  
  207.         extraGrowthSpeed = extraSpeedLightValue + biomeExtraSpeed;
  208.  
  209.         growthSpeed -= extraGrowthSpeed;
  210.  
  211.         return growthSpeed;
  212.  
  213.     }
  214.  
  215.     public void onBlockPlacedBy(World worldIn, int x, int y, int z, EntityLivingBase livingBase, ItemStack itemstack) {
  216.  
  217.         worldIn.setBlockMetadataWithNotify(x, y, z, 15, 2);
  218.  
  219.     }
  220.  
  221.     private void growMango(World worldIn, int x, int y, int z, Random rand) {
  222.  
  223.         worldIn.setBlock(x, y, z, MyBlocks.mangoes);
  224.  
  225.     }
  226.  
  227.     private boolean canGrowMango(World worldIn, int x, int y, int z, Random rand) {
  228.  
  229.         Block block = worldIn.getBlock(x, y - 1, z);
  230.  
  231.         if (this.isConnectedToTree(worldIn, x, y, z)) {
  232.  
  233.             if (block == Blocks.air) {
  234.  
  235.                 if (worldIn.getBlock(x - 1, y - 1, z - 1) == Blocks.air && worldIn.getBlock(x - 1, y - 1, z) == Blocks.air
  236.                         && worldIn.getBlock(x - 1, y - 1, z + 1) == Blocks.air && worldIn.getBlock(x, y - 1, z - 1) == Blocks.air
  237.                         && worldIn.getBlock(x, y - 1, z) == Blocks.air && worldIn.getBlock(x, y - 1, z + 1) == Blocks.air
  238.                         && worldIn.getBlock(x + 1, y - 1, z - 1) == Blocks.air && worldIn.getBlock(x + 1, y - 1, z) == Blocks.air
  239.                         && worldIn.getBlock(x + 1, y - 1, z + 1) == Blocks.air) {
  240.  
  241.                     if (recentlyGrownMango) {
  242.  
  243.                         return false;
  244.  
  245.                     } else {
  246.  
  247.                         return true;
  248.  
  249.                     }
  250.                 }
  251.             }
  252.         }
  253.  
  254.         return false;
  255.     }
  256.  
  257.     private boolean isConnectedToTree(World worldIn, int x, int y, int z) {
  258.  
  259.         byte b0 = 4;
  260.         int i1 = b0 + 1;
  261.         byte b1 = 32;
  262.         int j1 = b1 * b1;
  263.         int k1 = b1 / 2;
  264.  
  265.         if (this.fieldA == null) {
  266.             this.fieldA = new int[b1 * b1 * b1];
  267.         }
  268.  
  269.         int l1;
  270.  
  271.         if (worldIn.checkChunksExist(x - i1, y - i1, z - i1, x + i1, y + i1, z + i1)) {
  272.             int i2;
  273.             int j2;
  274.  
  275.             for (l1 = -b0; l1 <= b0; ++l1) {
  276.                 for (i2 = -b0; i2 <= b0; ++i2) {
  277.                     for (j2 = -b0; j2 <= b0; ++j2) {
  278.                         Block block = worldIn.getBlock(x + l1, y + i2, z + j2);
  279.  
  280.                         if (!block.canSustainLeaves(worldIn, x + l1, y + i2, z + j2)) {
  281.                             if (block.isLeaves(worldIn, x + l1, y + i2, z + j2)) {
  282.                                 this.fieldA[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -2;
  283.                             } else {
  284.                                 this.fieldA[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -1;
  285.                             }
  286.                         } else {
  287.                             this.fieldA[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = 0;
  288.                         }
  289.                     }
  290.                 }
  291.             }
  292.  
  293.             for (l1 = 1; l1 <= 4; ++l1) {
  294.                 for (i2 = -b0; i2 <= b0; ++i2) {
  295.                     for (j2 = -b0; j2 <= b0; ++j2) {
  296.                         for (int k2 = -b0; k2 <= b0; ++k2) {
  297.                             if (this.fieldA[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1) {
  298.                                 if (this.fieldA[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2) {
  299.                                     this.fieldA[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
  300.                                 }
  301.  
  302.                                 if (this.fieldA[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2) {
  303.                                     this.fieldA[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
  304.                                 }
  305.  
  306.                                 if (this.fieldA[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] == -2) {
  307.                                     this.fieldA[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] = l1;
  308.                                 }
  309.  
  310.                                 if (this.fieldA[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] == -2) {
  311.                                     this.fieldA[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] = l1;
  312.                                 }
  313.  
  314.                                 if (this.fieldA[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] == -2) {
  315.                                     this.fieldA[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] = l1;
  316.                                 }
  317.  
  318.                                 if (this.fieldA[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] == -2) {
  319.                                     this.fieldA[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] = l1;
  320.                                 }
  321.                             }
  322.                         }
  323.                     }
  324.                 }
  325.             }
  326.         }
  327.  
  328.         l1 = this.fieldA[k1 * j1 + k1 * b1 + k1];
  329.  
  330.         if (l1 >= 0) {
  331.             return true;
  332.         } else {
  333.             return false;
  334.         }
  335.     }
  336.  
  337.     @Override
  338.     public String[] func_150125_e() {
  339.  
  340.         return null;
  341.     }
  342.  
  343.     public boolean isOpaqueCube() {
  344.  
  345.         this.setGraphicsLevel(Minecraft.getMinecraft().isFancyGraphicsEnabled());
  346.  
  347.         return !this.field_150121_P;
  348.  
  349.     }
  350.  
  351.     public int damageDropped(int i) {
  352.         return 0;
  353.     }
  354.  
  355.     public Item getItemDropped(int i, Random rand, int fortune) {
  356.         return Item.getItemFromBlock(MyBlocks.sapling);
  357.     }
  358. }
Add Comment
Please, Sign In to add comment