Advertisement
Guest User

WorldGenMangoTree

a guest
Jun 19th, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package com.chef.mod.generate.features;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.chef.mod.init.MyBlocks;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.state.IBlockState;
  9. import net.minecraft.init.Blocks;
  10. import net.minecraft.util.BlockPos;
  11. import net.minecraft.world.World;
  12. import net.minecraft.world.gen.feature.WorldGenAbstractTree;
  13.  
  14. public class WorldGenMangoTree extends WorldGenAbstractTree {
  15.  
  16.     public WorldGenMangoTree() {
  17.  
  18.         super(false);
  19.     }
  20.  
  21.  
  22.     @Override
  23.     public boolean generate(World worldIn, Random rand, BlockPos pos) {
  24.  
  25.         int x = pos.getX();
  26.         int y = pos.getY();
  27.         int z = pos.getZ();
  28.         Block block = worldIn.getBlockState(pos).getBlock();
  29.  
  30.         while (worldIn.isAirBlock(pos) && y > 0) {
  31.  
  32.             y--;
  33.  
  34.         }
  35.  
  36.         if (block != Blocks.grass && block != Blocks.dirt) {
  37.  
  38.             return false;
  39.  
  40.         } else {
  41.  
  42.             for (int i = -2; i <= 2; i++) {
  43.  
  44.                 for (int j = -2; j <= 2; j++) {
  45.  
  46.                     if (worldIn.isAirBlock(new BlockPos(x + i, y - 1, z + j)) && worldIn.isAirBlock(new BlockPos(x + i, y - 2, z + j)) && !worldIn.isAirBlock(new BlockPos(x + i, y, z + j))) {
  47.  
  48.                         return false;
  49.  
  50.                     }
  51.  
  52.                 }
  53.  
  54.             }
  55.  
  56.             int baseLength = 4 + rand.nextInt(6);
  57.             int branches = 2 + rand.nextInt(4);
  58.  
  59.             int h = 0;
  60.  
  61.             block.onPlantGrow(worldIn, new BlockPos(x, y - 1, z), pos);
  62.  
  63.             for (int i = 0; i < baseLength; i++) {
  64.  
  65.                 buildBlock(worldIn, new BlockPos(x, y + h, z), Blocks.log.getDefaultState(), 1);
  66.                 h++;
  67.  
  68.             }
  69.  
  70.             generateTop(worldIn, new BlockPos(x, y + h, z));
  71.             return true;
  72.         }
  73.  
  74.     }
  75.  
  76.     public void generateTop(World world, BlockPos pos) {
  77.  
  78.         int x = pos.getX();
  79.         int y = pos.getY();
  80.         int z = pos.getZ();
  81.  
  82.        
  83.         //Top of the tree
  84.         for (int i = -1; i < 2; i++)
  85.         {
  86.            
  87.             for (int j = -1; j < 2; j++)
  88.             {
  89.                
  90.                 buildBlock(world, new BlockPos(x + i, y + 2, z + j), MyBlocks.mango_leaves.getDefaultState(), 1);
  91.                
  92.             }
  93.            
  94.         }
  95.        
  96.         //One beneath the top
  97.         for (int i = -2; i < 3; i++)
  98.         {
  99.            
  100.             for (int j = -1; j < 2; j++)
  101.             {
  102.                
  103.                 buildBlock(world, new BlockPos(x + i, y + 1, z + j), MyBlocks.mango_leaves.getDefaultState(), 1);
  104.                 buildBlock(world, new BlockPos(x + j, y + 1, z + i), MyBlocks.mango_leaves.getDefaultState(), 1);
  105.             }
  106.            
  107.         }
  108.        
  109.         //Two beneath the top
  110.         for (int i = -2; i < 3; i++) {
  111.  
  112.             for (int j = -2; j < 3; j++) {
  113.  
  114.                 buildBlock(world, new BlockPos(x + i, y, z + j), MyBlocks.mango_leaves.getDefaultState(), 1);
  115.  
  116.             }
  117.  
  118.         }
  119.         buildBlock(world, pos, Blocks.log.getDefaultState(), 1);
  120.         buildBlock(world, new BlockPos(x, y + 1, z), Blocks.log.getDefaultState(), 1);
  121.  
  122.     }
  123.  
  124.     public void buildBlock(World world, BlockPos pos, IBlockState state, int meta) {
  125.  
  126.         if (world.isAirBlock(pos) || world.getBlockState(pos).getBlock().isLeaves(world, pos)) {
  127.  
  128.             world.setBlockState(pos, state.getBlock().getDefaultState(), meta);
  129.  
  130.         }
  131.  
  132.     }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement