| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Random;
|
| 5 | import net.minecraft.block.properties.IProperty;
|
| 6 | import net.minecraft.block.properties.PropertyEnum;
|
| 7 | import net.minecraft.block.properties.PropertyInteger;
|
| 8 | import net.minecraft.block.state.BlockState;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.creativetab.CreativeTabs;
|
| 11 | import net.minecraft.init.Blocks;
|
| 12 | import net.minecraft.item.Item;
|
| 13 | import net.minecraft.item.ItemStack;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.StatCollector;
|
| 16 | import net.minecraft.world.World;
|
| 17 | import net.minecraft.world.gen.feature.WorldGenBigTree;
|
| 18 | import net.minecraft.world.gen.feature.WorldGenCanopyTree;
|
| 19 | import net.minecraft.world.gen.feature.WorldGenForest;
|
| 20 | import net.minecraft.world.gen.feature.WorldGenMegaJungle;
|
| 21 | import net.minecraft.world.gen.feature.WorldGenMegaPineTree;
|
| 22 | import net.minecraft.world.gen.feature.WorldGenSavannaTree;
|
| 23 | import net.minecraft.world.gen.feature.WorldGenTaiga2;
|
| 24 | import net.minecraft.world.gen.feature.WorldGenTrees;
|
| 25 | import net.minecraft.world.gen.feature.WorldGenerator;
|
| 26 |
|
| 27 | public class BlockSapling extends BlockBush implements IGrowable
|
| 28 | {
|
| 29 | public static final PropertyEnum<BlockPlanks.EnumType> TYPE = PropertyEnum.<BlockPlanks.EnumType>create("type", BlockPlanks.EnumType.class);
|
| 30 | public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
|
| 31 |
|
| 32 | protected BlockSapling()
|
| 33 | {
|
| 34 | this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, BlockPlanks.EnumType.OAK).withProperty(STAGE, Integer.valueOf(0)));
|
| 35 | float f = 0.4F;
|
| 36 | this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
|
| 37 | this.setCreativeTab(CreativeTabs.tabDecorations);
|
| 38 | }
|
| 39 |
|
| 40 | /**
|
| 41 | * Gets the localized name of this block. Used for the statistics page.
|
| 42 | */
|
| 43 | public String getLocalizedName()
|
| 44 | {
|
| 45 | return StatCollector.translateToLocal(this.getUnlocalizedName() + "." + BlockPlanks.EnumType.OAK.getUnlocalizedName() + ".name");
|
| 46 | }
|
| 47 |
|
| 48 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 49 | {
|
| 50 | if (!worldIn.isRemote)
|
| 51 | {
|
| 52 | super.updateTick(worldIn, pos, state, rand);
|
| 53 |
|
| 54 | if (worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(7) == 0)
|
| 55 | {
|
| 56 | this.grow(worldIn, pos, state, rand);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public void grow(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 62 | {
|
| 63 | if (((Integer)state.getValue(STAGE)).intValue() == 0)
|
| 64 | {
|
| 65 | worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
|
| 66 | }
|
| 67 | else
|
| 68 | {
|
| 69 | this.generateTree(worldIn, pos, state, rand);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public void generateTree(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 74 | {
|
| 75 | WorldGenerator worldgenerator = (WorldGenerator)(rand.nextInt(10) == 0 ? new WorldGenBigTree(true) : new WorldGenTrees(true));
|
| 76 | int i = 0;
|
| 77 | int j = 0;
|
| 78 | boolean flag = false;
|
| 79 |
|
| 80 | switch ((BlockPlanks.EnumType)state.getValue(TYPE))
|
| 81 | {
|
| 82 | case SPRUCE:
|
| 83 | label114:
|
| 84 | for (i = 0; i >= -1; --i)
|
| 85 | {
|
| 86 | for (j = 0; j >= -1; --j)
|
| 87 | {
|
| 88 | if (this.func_181624_a(worldIn, pos, i, j, BlockPlanks.EnumType.SPRUCE))
|
| 89 | {
|
| 90 | worldgenerator = new WorldGenMegaPineTree(false, rand.nextBoolean());
|
| 91 | flag = true;
|
| 92 | break label114;
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | if (!flag)
|
| 98 | {
|
| 99 | j = 0;
|
| 100 | i = 0;
|
| 101 | worldgenerator = new WorldGenTaiga2(true);
|
| 102 | }
|
| 103 |
|
| 104 | break;
|
| 105 |
|
| 106 | case BIRCH:
|
| 107 | worldgenerator = new WorldGenForest(true, false);
|
| 108 | break;
|
| 109 |
|
| 110 | case JUNGLE:
|
| 111 | IBlockState iblockstate = Blocks.log.getDefaultState().withProperty(BlockOldLog.VARIANT, BlockPlanks.EnumType.JUNGLE);
|
| 112 | IBlockState iblockstate1 = Blocks.leaves.getDefaultState().withProperty(BlockOldLeaf.VARIANT, BlockPlanks.EnumType.JUNGLE).withProperty(BlockLeaves.CHECK_DECAY, Boolean.valueOf(false));
|
| 113 | label269:
|
| 114 |
|
| 115 | for (i = 0; i >= -1; --i)
|
| 116 | {
|
| 117 | for (j = 0; j >= -1; --j)
|
| 118 | {
|
| 119 | if (this.func_181624_a(worldIn, pos, i, j, BlockPlanks.EnumType.JUNGLE))
|
| 120 | {
|
| 121 | worldgenerator = new WorldGenMegaJungle(true, 10, 20, iblockstate, iblockstate1);
|
| 122 | flag = true;
|
| 123 | break label269;
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | if (!flag)
|
| 129 | {
|
| 130 | j = 0;
|
| 131 | i = 0;
|
| 132 | worldgenerator = new WorldGenTrees(true, 4 + rand.nextInt(7), iblockstate, iblockstate1, false);
|
| 133 | }
|
| 134 |
|
| 135 | break;
|
| 136 |
|
| 137 | case ACACIA:
|
| 138 | worldgenerator = new WorldGenSavannaTree(true);
|
| 139 | break;
|
| 140 |
|
| 141 | case DARK_OAK:
|
| 142 | label390:
|
| 143 | for (i = 0; i >= -1; --i)
|
| 144 | {
|
| 145 | for (j = 0; j >= -1; --j)
|
| 146 | {
|
| 147 | if (this.func_181624_a(worldIn, pos, i, j, BlockPlanks.EnumType.DARK_OAK))
|
| 148 | {
|
| 149 | worldgenerator = new WorldGenCanopyTree(true);
|
| 150 | flag = true;
|
| 151 | break label390;
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | if (!flag)
|
| 157 | {
|
| 158 | return;
|
| 159 | }
|
| 160 |
|
| 161 | case OAK:
|
| 162 | }
|
| 163 |
|
| 164 | IBlockState iblockstate2 = Blocks.air.getDefaultState();
|
| 165 |
|
| 166 | if (flag)
|
| 167 | {
|
| 168 | worldIn.setBlockState(pos.add(i, 0, j), iblockstate2, 4);
|
| 169 | worldIn.setBlockState(pos.add(i + 1, 0, j), iblockstate2, 4);
|
| 170 | worldIn.setBlockState(pos.add(i, 0, j + 1), iblockstate2, 4);
|
| 171 | worldIn.setBlockState(pos.add(i + 1, 0, j + 1), iblockstate2, 4);
|
| 172 | }
|
| 173 | else
|
| 174 | {
|
| 175 | worldIn.setBlockState(pos, iblockstate2, 4);
|
| 176 | }
|
| 177 |
|
| 178 | if (!worldgenerator.generate(worldIn, rand, pos.add(i, 0, j)))
|
| 179 | {
|
| 180 | if (flag)
|
| 181 | {
|
| 182 | worldIn.setBlockState(pos.add(i, 0, j), state, 4);
|
| 183 | worldIn.setBlockState(pos.add(i + 1, 0, j), state, 4);
|
| 184 | worldIn.setBlockState(pos.add(i, 0, j + 1), state, 4);
|
| 185 | worldIn.setBlockState(pos.add(i + 1, 0, j + 1), state, 4);
|
| 186 | }
|
| 187 | else
|
| 188 | {
|
| 189 | worldIn.setBlockState(pos, state, 4);
|
| 190 | }
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | private boolean func_181624_a(World p_181624_1_, BlockPos p_181624_2_, int p_181624_3_, int p_181624_4_, BlockPlanks.EnumType p_181624_5_)
|
| 195 | {
|
| 196 | return this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_, 0, p_181624_4_), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_ + 1, 0, p_181624_4_), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_, 0, p_181624_4_ + 1), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_ + 1, 0, p_181624_4_ + 1), p_181624_5_);
|
| 197 | }
|
| 198 |
|
| 199 | /**
|
| 200 | * Check whether the given BlockPos has a Sapling of the given type
|
| 201 | */
|
| 202 | public boolean isTypeAt(World worldIn, BlockPos pos, BlockPlanks.EnumType type)
|
| 203 | {
|
| 204 | IBlockState iblockstate = worldIn.getBlockState(pos);
|
| 205 | return iblockstate.getBlock() == this && iblockstate.getValue(TYPE) == type;
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
|
| 210 | * returns the metadata of the dropped item based on the old metadata of the block.
|
| 211 | */
|
| 212 | public int damageDropped(IBlockState state)
|
| 213 | {
|
| 214 | return ((BlockPlanks.EnumType)state.getValue(TYPE)).getMetadata();
|
| 215 | }
|
| 216 |
|
| 217 | /**
|
| 218 | * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
|
| 219 | */
|
| 220 | public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list)
|
| 221 | {
|
| 222 | for (BlockPlanks.EnumType blockplanks$enumtype : BlockPlanks.EnumType.values())
|
| 223 | {
|
| 224 | list.add(new ItemStack(itemIn, 1, blockplanks$enumtype.getMetadata()));
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | /**
|
| 229 | * Whether this IGrowable can grow
|
| 230 | */
|
| 231 | public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
|
| 232 | {
|
| 233 | return true;
|
| 234 | }
|
| 235 |
|
| 236 | public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 237 | {
|
| 238 | return (double)worldIn.rand.nextFloat() < 0.45D;
|
| 239 | }
|
| 240 |
|
| 241 | public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 242 | {
|
| 243 | this.grow(worldIn, pos, state, rand);
|
| 244 | }
|
| 245 |
|
| 246 | /**
|
| 247 | * Convert the given metadata into a BlockState for this Block
|
| 248 | */
|
| 249 | public IBlockState getStateFromMeta(int meta)
|
| 250 | {
|
| 251 | return this.getDefaultState().withProperty(TYPE, BlockPlanks.EnumType.byMetadata(meta & 7)).withProperty(STAGE, Integer.valueOf((meta & 8) >> 3));
|
| 252 | }
|
| 253 |
|
| 254 | /**
|
| 255 | * Convert the BlockState into the correct metadata value
|
| 256 | */
|
| 257 | public int getMetaFromState(IBlockState state)
|
| 258 | {
|
| 259 | int i = 0;
|
| 260 | i = i | ((BlockPlanks.EnumType)state.getValue(TYPE)).getMetadata();
|
| 261 | i = i | ((Integer)state.getValue(STAGE)).intValue() << 3;
|
| 262 | return i;
|
| 263 | }
|
| 264 |
|
| 265 | protected BlockState createBlockState()
|
| 266 | {
|
| 267 | return new BlockState(this, new IProperty[] {TYPE, STAGE});
|
| 268 | }
|
| 269 | }
|
| 270 |
|