package me.km.blocks.leaves; import java.util.Random; import me.km.KajetansMod; import me.km.blocks.IBlockBase; import net.minecraft.block.BlockBush; import net.minecraft.block.BlockOldLog; import net.minecraft.block.BlockPlanks; import net.minecraft.block.IGrowable; import net.minecraft.block.SoundType; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyInteger; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import net.minecraft.world.gen.feature.WorldGenerator; public class BlockSapling extends BlockBush implements IBlockBase, IGrowable { public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1); protected static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1d, 0, 0.1d, 0.9d, 0.8d, 0.9d); protected String name; private final IBlockState log; private final IBlockState leaf; public BlockSapling(String name, String local, BlockPlanks.EnumType planks, BlockLeaves leaf) { this.name = name; this.setRegistryName(name); super.setUnlocalizedName(local); super.setHardness(0.0F); super.setSoundType(SoundType.PLANT); super.setDefaultState(this.blockState.getBaseState().withProperty(STAGE, 0)); super.setCreativeTab(CreativeTabs.DECORATIONS); this.log = Blocks.LOG.getDefaultState().withProperty(BlockOldLog.VARIANT, planks); this.leaf = leaf.getDefaultState().withProperty(net.minecraft.block.BlockLeaves.CHECK_DECAY, false); } @Override public void registerItemModel(Item itemBlock) { KajetansMod.proxy.registerItemRenderer(itemBlock, 0, name); } @Override public BlockSapling setSoundType(SoundType sound) { super.setSoundType(sound); return this; } @Override public net.minecraftforge.common.EnumPlantType getPlantType(net.minecraft.world.IBlockAccess world, BlockPos pos) { return net.minecraftforge.common.EnumPlantType.Plains; } @Override public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { return SAPLING_AABB; } @Override public void updateTick(World w, BlockPos pos, IBlockState state, Random rand) { if(!w.isRemote) { super.updateTick(w, pos, state, rand); if(w.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(7) == 0) { this.grow(w, pos, state, rand); } } } public void grow(World worldIn, BlockPos pos, IBlockState state, Random rand) { if(state.getValue(STAGE) == 0) { worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4); } else { this.generateTree(worldIn, pos, state, rand); } } public void generateTree(World w, BlockPos pos, IBlockState state, Random rand) { WorldGenerator gen = new WorldGenTree(true, log, leaf); w.setBlockState(pos, Blocks.AIR.getDefaultState(), 4); if(!gen.generate(w, rand, pos)) { w.setBlockState(pos, state, 4); } } @Override public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient) { return true; } @Override public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state) { return worldIn.rand.nextFloat() < 0.45d; } @Override public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state) { this.grow(worldIn, pos, state, rand); } @Override public IBlockState getStateFromMeta(int meta) { return this.getDefaultState().withProperty(STAGE, meta); } @Override public int getMetaFromState(IBlockState state) { return state.getValue(STAGE); } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] {STAGE}); } }