Advertisement
Guest User

WorldGenTutTree

a guest
Mar 25th, 2013
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.22 KB | None | 0 0
  1. package OliveCraft;
  2.  
  3. import java.util.Random;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockSapling;
  7. import net.minecraft.util.Direction;
  8. import net.minecraft.world.World;
  9. import net.minecraft.world.gen.feature.WorldGenerator;
  10. import net.minecraftforge.common.ForgeDirection;
  11.  
  12. public class WorldGenTutTree extends WorldGenerator
  13. {
  14.     /** The minimum height of a generated tree. */
  15.     private final int minTreeHeight;
  16.  
  17.     /** True if this tree should grow Vines. */
  18.     private final boolean vinesGrow;
  19.  
  20.     /** The metadata value of the wood to use in tree generation. */
  21.     private final int metaWood;
  22.  
  23.     /** The metadata value of the leaves to use in tree generation. */
  24.     private final int metaLeaves;
  25.  
  26.     public WorldGenTutTree(boolean par1)
  27.     {
  28.         this(par1, 4, 0, 0, false);
  29.     }
  30.  
  31.     public WorldGenTutTree(boolean par1, int par2, int par3, int par4, boolean par5)
  32.     {
  33.         super(par1);
  34.         this.minTreeHeight = par2;
  35.         this.metaWood = par3;
  36.         this.metaLeaves = par4;
  37.         this.vinesGrow = par5;
  38.     }
  39.  
  40.     public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
  41.     {
  42.         int l = par2Random.nextInt(3) + this.minTreeHeight;
  43.         boolean flag = true;
  44.  
  45.         if (par4 >= 1 && par4 + l + 1 <= 256)
  46.         {
  47.             int i1;
  48.             byte b0;
  49.             int j1;
  50.             int k1;
  51.  
  52.             for (i1 = par4; i1 <= par4 + 1 + l; ++i1)
  53.             {
  54.                 b0 = 1;
  55.  
  56.                 if (i1 == par4)
  57.                 {
  58.                     b0 = 0;
  59.                 }
  60.  
  61.                 if (i1 >= par4 + 1 + l - 2)
  62.                 {
  63.                     b0 = 2;
  64.                 }
  65.  
  66.                 for (int l1 = par3 - b0; l1 <= par3 + b0 && flag; ++l1)
  67.                 {
  68.                     for (j1 = par5 - b0; j1 <= par5 + b0 && flag; ++j1)
  69.                     {
  70.                         if (i1 >= 0 && i1 < 256)
  71.                         {
  72.                             k1 = par1World.getBlockId(l1, i1, j1);
  73.  
  74.                             Block block = Block.blocksList[k1];
  75.  
  76.                             if (k1 != 0 &&
  77.                                !block.isLeaves(par1World, l1, i1, j1) &&
  78.                                 k1 != Block.grass.blockID &&
  79.                                 k1 != Block.dirt.blockID &&
  80.                                !block.isWood(par1World, l1, i1, j1))
  81.                             {
  82.                                 flag = false;
  83.                             }
  84.                         }
  85.                         else
  86.                         {
  87.                             flag = false;
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             if (!flag)
  94.             {
  95.                 return false;
  96.             }
  97.             else
  98.             {
  99.                 i1 = par1World.getBlockId(par3, par4 - 1, par5);
  100.                 Block soil = Block.blocksList[i1];
  101.                 boolean isSoil = (soil != null && soil.canSustainPlant(par1World, par3, par4 - 1, par5, ForgeDirection.UP, (BlockSapling)Block.sapling));
  102.  
  103.                 if (isSoil && par4 < 256 - l - 1)
  104.                 {
  105.                     soil.onPlantGrow(par1World, par3, par4 - 1, par5, par3, par4, par5);
  106.                     b0 = 3;
  107.                     byte b1 = 0;
  108.                     int i2;
  109.                     int j2;
  110.                     int k2;
  111.  
  112.                     for (j1 = par4 - b0 + l; j1 <= par4 + l; ++j1)
  113.                     {
  114.                         k1 = j1 - (par4 + l);
  115.                         i2 = b1 + 1 - k1 / 2;
  116.  
  117.                         for (j2 = par3 - i2; j2 <= par3 + i2; ++j2)
  118.                         {
  119.                             k2 = j2 - par3;
  120.  
  121.                             for (int l2 = par5 - i2; l2 <= par5 + i2; ++l2)
  122.                             {
  123.                                 int i3 = l2 - par5;
  124.  
  125.                                 if (Math.abs(k2) != i2 || Math.abs(i3) != i2 || par2Random.nextInt(2) != 0 && k1 != 0)
  126.                                 {
  127.                                     int j3 = par1World.getBlockId(j2, j1, l2);
  128.                                     Block block = Block.blocksList[j3];
  129.  
  130.                                     if (block == null || block.canBeReplacedByLeaves(par1World, j2, j1, l2))
  131.                                     {
  132.                                         this.setBlockAndMetadata(par1World, j2, j1, l2, mod_OliveCraft.TutLeaf.blockID, this.metaLeaves);
  133.                                     }
  134.                                 }
  135.                             }
  136.                         }
  137.                     }
  138.  
  139.                     for (j1 = 0; j1 < l; ++j1)
  140.                     {
  141.                         k1 = par1World.getBlockId(par3, par4 + j1, par5);
  142.  
  143.                         Block block = Block.blocksList[k1];
  144.  
  145.                         if (k1 == 0 || block == null || block.isLeaves(par1World, par3, par4 + j1, par5))
  146.                         {
  147.                             this.setBlockAndMetadata(par1World, par3, par4 + j1, par5, mod_OliveCraft.TutLog.blockID, this.metaWood);
  148.  
  149.                             if (this.vinesGrow && j1 > 0)
  150.                             {
  151.                                 if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 - 1, par4 + j1, par5))
  152.                                 {
  153.                                     this.setBlockAndMetadata(par1World, par3 - 1, par4 + j1, par5, Block.vine.blockID, 8);
  154.                                 }
  155.  
  156.                                 if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 + 1, par4 + j1, par5))
  157.                                 {
  158.                                     this.setBlockAndMetadata(par1World, par3 + 1, par4 + j1, par5, Block.vine.blockID, 2);
  159.                                 }
  160.  
  161.                                 if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + j1, par5 - 1))
  162.                                 {
  163.                                     this.setBlockAndMetadata(par1World, par3, par4 + j1, par5 - 1, Block.vine.blockID, 1);
  164.                                 }
  165.  
  166.                                 if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + j1, par5 + 1))
  167.                                 {
  168.                                     this.setBlockAndMetadata(par1World, par3, par4 + j1, par5 + 1, Block.vine.blockID, 4);
  169.                                 }
  170.                             }
  171.                         }
  172.                     }
  173.  
  174.                     if (this.vinesGrow)
  175.                     {
  176.                         for (j1 = par4 - 3 + l; j1 <= par4 + l; ++j1)
  177.                         {
  178.                             k1 = j1 - (par4 + l);
  179.                             i2 = 2 - k1 / 2;
  180.  
  181.                             for (j2 = par3 - i2; j2 <= par3 + i2; ++j2)
  182.                             {
  183.                                 for (k2 = par5 - i2; k2 <= par5 + i2; ++k2)
  184.                                 {
  185.                                     Block block = Block.blocksList[par1World.getBlockId(j2, j1, k2)];
  186.                                     if (block != null && block.isLeaves(par1World, j2, j1, k2))
  187.                                     {
  188.                                         if (par2Random.nextInt(4) == 0 && par1World.getBlockId(j2 - 1, j1, k2) == 0)
  189.                                         {
  190.                                             this.growVines(par1World, j2 - 1, j1, k2, 8);
  191.                                         }
  192.  
  193.                                         if (par2Random.nextInt(4) == 0 && par1World.getBlockId(j2 + 1, j1, k2) == 0)
  194.                                         {
  195.                                             this.growVines(par1World, j2 + 1, j1, k2, 2);
  196.                                         }
  197.  
  198.                                         if (par2Random.nextInt(4) == 0 && par1World.getBlockId(j2, j1, k2 - 1) == 0)
  199.                                         {
  200.                                             this.growVines(par1World, j2, j1, k2 - 1, 1);
  201.                                         }
  202.  
  203.                                         if (par2Random.nextInt(4) == 0 && par1World.getBlockId(j2, j1, k2 + 1) == 0)
  204.                                         {
  205.                                             this.growVines(par1World, j2, j1, k2 + 1, 4);
  206.                                         }
  207.                                     }
  208.                                 }
  209.                             }
  210.                         }
  211.  
  212.                         if (par2Random.nextInt(5) == 0 && l > 5)
  213.                         {
  214.                             for (j1 = 0; j1 < 2; ++j1)
  215.                             {
  216.                                 for (k1 = 0; k1 < 4; ++k1)
  217.                                 {
  218.                                     if (par2Random.nextInt(4 - j1) == 0)
  219.                                     {
  220.                                         i2 = par2Random.nextInt(3);
  221.                                         this.setBlockAndMetadata(par1World, par3 + Direction.offsetX[Direction.footInvisibleFaceRemap[k1]], par4 + l - 5 + j1, par5 + Direction.offsetZ[Direction.footInvisibleFaceRemap[k1]], Block.cocoaPlant.blockID, i2 << 2 | k1);
  222.                                     }
  223.                                 }
  224.                             }
  225.                         }
  226.                     }
  227.  
  228.                     return true;
  229.                 }
  230.                 else
  231.                 {
  232.                     return false;
  233.                 }
  234.             }
  235.         }
  236.         else
  237.         {
  238.             return false;
  239.         }
  240.     }
  241.  
  242.     /**
  243.      * Grows vines downward from the given block for a given length. Args: World, x, starty, z, vine-length
  244.      */
  245.     private void growVines(World par1World, int par2, int par3, int par4, int par5)
  246.     {
  247.         this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
  248.         int i1 = 4;
  249.  
  250.         while (true)
  251.         {
  252.             --par3;
  253.  
  254.             if (par1World.getBlockId(par2, par3, par4) != 0 || i1 <= 0)
  255.             {
  256.                 return;
  257.             }
  258.  
  259.             this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
  260.             --i1;
  261.         }
  262.     }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement