WorldGenTree.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package me.km.blocks.leaves;
  2. import java.util.Random;
  3. import net.minecraft.block.Block;
  4. import net.minecraft.block.material.Material;
  5. import net.minecraft.block.state.IBlockState;
  6. import net.minecraft.init.Blocks;
  7. import net.minecraft.util.EnumFacing;
  8. import net.minecraft.util.math.BlockPos;
  9. import net.minecraft.world.World;
  10. import net.minecraft.world.gen.feature.WorldGenAbstractTree;
  11. public class WorldGenTree extends WorldGenAbstractTree
  12. {
  13. private final IBlockState log;
  14. private final IBlockState leaf;
  15. public WorldGenTree(boolean notify, IBlockState log, IBlockState leaf)
  16. {
  17. super(notify);
  18. this.log = log;
  19. this.leaf = leaf;
  20. }
  21. @Override
  22. protected boolean canGrowInto(Block blockType)
  23. {
  24. Material material = blockType.getDefaultState().getMaterial();
  25. return material == Material.AIR || material == Material.LEAVES ||
  26. blockType == Blocks.GRASS || blockType == Blocks.DIRT ||
  27. blockType == Blocks.LOG || blockType == Blocks.LOG2 ||
  28. blockType == Blocks.SAPLING || blockType == Blocks.VINE ||
  29. blockType instanceof BlockLeaves;
  30. }
  31. @Override
  32. public boolean generate(World worldIn, Random rand, BlockPos pos)
  33. {
  34. int i = rand.nextInt(3) + 5;
  35. boolean flag = true;
  36. if(pos.getY() >= 1 && pos.getY() + i + 1 <= 256)
  37. {
  38. for(int j = pos.getY(); j <= pos.getY() + 1 + i; j++)
  39. {
  40. int k = 1;
  41. if (j == pos.getY())
  42. {
  43. k = 0;
  44. }
  45. if (j >= pos.getY() + 1 + i - 2)
  46. {
  47. k = 2;
  48. }
  49. BlockPos.MutableBlockPos mPos = new BlockPos.MutableBlockPos();
  50. for(int x = pos.getX() - k; x <= pos.getX() + k && flag; x++)
  51. {
  52. for(int z = pos.getZ() - k; z <= pos.getZ() + k && flag; z++)
  53. {
  54. if(j >= 0 && j < worldIn.getHeight())
  55. {
  56. if(!this.isReplaceable(worldIn, mPos.setPos(x, j, z)))
  57. {
  58. flag = false;
  59. }
  60. }
  61. else
  62. {
  63. flag = false;
  64. }
  65. }
  66. }
  67. }
  68. if(!flag)
  69. {
  70. return false;
  71. }
  72. else
  73. {
  74. BlockPos down = pos.down();
  75. IBlockState state = worldIn.getBlockState(down);
  76. boolean isSoil = state.getBlock().canSustainPlant(state, worldIn, down, EnumFacing.UP, (net.minecraft.block.BlockSapling) Blocks.SAPLING);
  77. if(isSoil && pos.getY() < worldIn.getHeight() - i - 1)
  78. {
  79. state.getBlock().onPlantGrow(state, worldIn, down, pos);
  80. for(int y = pos.getY() - 3 + i; y <= pos.getY() + i; y++)
  81. {
  82. int k = y - (pos.getY() + i);
  83. int l = 1 - k / 2;
  84. for(int x = pos.getX() - l; x <= pos.getX() + l; x++)
  85. {
  86. int j = x - pos.getX();
  87. for(int z = pos.getZ() - l; z <= pos.getZ() + l; z++)
  88. {
  89. int m = z - pos.getZ();
  90. if(Math.abs(j) != l || Math.abs(m) != l || rand.nextInt(2) != 0 && k != 0)
  91. {
  92. BlockPos blockpos = new BlockPos(x, y, z);
  93. IBlockState state2 = worldIn.getBlockState(blockpos);
  94. if(state2.getBlock().isAir(state2, worldIn, blockpos) || state2.getBlock().isAir(state2, worldIn, blockpos))
  95. {
  96. this.setBlockAndNotifyAdequately(worldIn, blockpos, leaf);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. for(int j = 0; j < i; j++)
  103. {
  104. BlockPos upN = pos.up(j);
  105. IBlockState state2 = worldIn.getBlockState(upN);
  106. if (state2.getBlock().isAir(state2, worldIn, upN) || state2.getBlock().isLeaves(state2, worldIn, upN))
  107. {
  108. this.setBlockAndNotifyAdequately(worldIn, pos.up(j), log);
  109. }
  110. }
  111. return true;
  112. }
  113. else
  114. {
  115. return false;
  116. }
  117. }
  118. }
  119. else
  120. {
  121. return false;
  122. }
  123. }
  124. }