BlockSapling.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package me.km.blocks.leaves;
  2. import java.util.Random;
  3. import me.km.KajetansMod;
  4. import me.km.blocks.IBlockBase;
  5. import net.minecraft.block.BlockBush;
  6. import net.minecraft.block.BlockOldLog;
  7. import net.minecraft.block.BlockPlanks;
  8. import net.minecraft.block.IGrowable;
  9. import net.minecraft.block.SoundType;
  10. import net.minecraft.block.properties.IProperty;
  11. import net.minecraft.block.properties.PropertyInteger;
  12. import net.minecraft.block.state.BlockStateContainer;
  13. import net.minecraft.block.state.IBlockState;
  14. import net.minecraft.creativetab.CreativeTabs;
  15. import net.minecraft.init.Blocks;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.util.math.AxisAlignedBB;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.world.IBlockAccess;
  20. import net.minecraft.world.World;
  21. import net.minecraft.world.gen.feature.WorldGenerator;
  22. public class BlockSapling extends BlockBush implements IBlockBase, IGrowable
  23. {
  24. public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
  25. protected static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1d, 0, 0.1d, 0.9d, 0.8d, 0.9d);
  26. protected String name;
  27. private final IBlockState log;
  28. private final IBlockState leaf;
  29. public BlockSapling(String name, String local, BlockPlanks.EnumType planks, BlockLeaves leaf)
  30. {
  31. this.name = name;
  32. this.setRegistryName(name);
  33. super.setUnlocalizedName(local);
  34. super.setHardness(0.0F);
  35. super.setSoundType(SoundType.PLANT);
  36. super.setDefaultState(this.blockState.getBaseState().withProperty(STAGE, 0));
  37. super.setCreativeTab(CreativeTabs.DECORATIONS);
  38. this.log = Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, planks);
  39. this.leaf = leaf.getDefaultState().withProperty(net.minecraft.block.BlockLeaves.CHECK_DECAY, false);
  40. }
  41. @Override
  42. public void registerItemModel(Item itemBlock)
  43. {
  44. KajetansMod.proxy.registerItemRenderer(itemBlock, 0, name);
  45. }
  46. @Override
  47. public BlockSapling setSoundType(SoundType sound)
  48. {
  49. super.setSoundType(sound);
  50. return this;
  51. }
  52. @Override
  53. public net.minecraftforge.common.EnumPlantType getPlantType(net.minecraft.world.IBlockAccess world, BlockPos pos)
  54. {
  55. return net.minecraftforge.common.EnumPlantType.Plains;
  56. }
  57. @Override
  58. public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
  59. {
  60. return SAPLING_AABB;
  61. }
  62. @Override
  63. public void updateTick(World w, BlockPos pos, IBlockState state, Random rand)
  64. {
  65. if(!w.isRemote)
  66. {
  67. super.updateTick(w, pos, state, rand);
  68. if(w.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(7) == 0)
  69. {
  70. this.grow(w, pos, state, rand);
  71. }
  72. }
  73. }
  74. public void grow(World worldIn, BlockPos pos, IBlockState state, Random rand)
  75. {
  76. if(state.getValue(STAGE) == 0)
  77. {
  78. worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
  79. }
  80. else
  81. {
  82. this.generateTree(worldIn, pos, state, rand);
  83. }
  84. }
  85. public void generateTree(World w, BlockPos pos, IBlockState state, Random rand)
  86. {
  87. WorldGenerator gen = new WorldGenTree(true, log, leaf);
  88. w.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
  89. if(!gen.generate(w, rand, pos))
  90. {
  91. w.setBlockState(pos, state, 4);
  92. }
  93. }
  94. @Override
  95. public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
  96. {
  97. return true;
  98. }
  99. @Override
  100. public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
  101. {
  102. return worldIn.rand.nextFloat() < 0.45d;
  103. }
  104. @Override
  105. public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
  106. {
  107. this.grow(worldIn, pos, state, rand);
  108. }
  109. @Override
  110. public IBlockState getStateFromMeta(int meta)
  111. {
  112. return this.getDefaultState().withProperty(STAGE, meta);
  113. }
  114. @Override
  115. public int getMetaFromState(IBlockState state)
  116. {
  117. return state.getValue(STAGE);
  118. }
  119. @Override
  120. protected BlockStateContainer createBlockState()
  121. {
  122. return new BlockStateContainer(this, new IProperty[] {STAGE});
  123. }
  124. }