| 1 | package net.minecraft.block;
|
| 2 |
|
| 3 | import java.util.Random;
|
| 4 | import net.minecraft.block.properties.IProperty;
|
| 5 | import net.minecraft.block.properties.PropertyInteger;
|
| 6 | import net.minecraft.block.state.BlockState;
|
| 7 | import net.minecraft.block.state.IBlockState;
|
| 8 | import net.minecraft.creativetab.CreativeTabs;
|
| 9 | import net.minecraft.init.Blocks;
|
| 10 | import net.minecraft.init.Items;
|
| 11 | import net.minecraft.item.Item;
|
| 12 | import net.minecraft.item.ItemStack;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.util.MathHelper;
|
| 15 | import net.minecraft.world.World;
|
| 16 |
|
| 17 | public class BlockCrops extends BlockBush implements IGrowable
|
| 18 | {
|
| 19 | public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 7);
|
| 20 |
|
| 21 | protected BlockCrops()
|
| 22 | {
|
| 23 | this.setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
|
| 24 | this.setTickRandomly(true);
|
| 25 | float f = 0.5F;
|
| 26 | this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
|
| 27 | this.setCreativeTab((CreativeTabs)null);
|
| 28 | this.setHardness(0.0F);
|
| 29 | this.setStepSound(soundTypeGrass);
|
| 30 | this.disableStats();
|
| 31 | }
|
| 32 |
|
| 33 | /**
|
| 34 | * is the block grass, dirt or farmland
|
| 35 | */
|
| 36 | protected boolean canPlaceBlockOn(Block ground)
|
| 37 | {
|
| 38 | return ground == Blocks.farmland;
|
| 39 | }
|
| 40 |
|
| 41 | public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
|
| 42 | {
|
| 43 | super.updateTick(worldIn, pos, state, rand);
|
| 44 |
|
| 45 | if (worldIn.getLightFromNeighbors(pos.up()) >= 9)
|
| 46 | {
|
| 47 | int i = ((Integer)state.getValue(AGE)).intValue();
|
| 48 |
|
| 49 | if (i < 7)
|
| 50 | {
|
| 51 | float f = getGrowthChance(this, worldIn, pos);
|
| 52 |
|
| 53 | if (rand.nextInt((int)(25.0F / f) + 1) == 0)
|
| 54 | {
|
| 55 | worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public void grow(World worldIn, BlockPos pos, IBlockState state)
|
| 62 | {
|
| 63 | int i = ((Integer)state.getValue(AGE)).intValue() + MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);
|
| 64 |
|
| 65 | if (i > 7)
|
| 66 | {
|
| 67 | i = 7;
|
| 68 | }
|
| 69 |
|
| 70 | worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i)), 2);
|
| 71 | }
|
| 72 |
|
| 73 | protected static float getGrowthChance(Block blockIn, World worldIn, BlockPos pos)
|
| 74 | {
|
| 75 | float f = 1.0F;
|
| 76 | BlockPos blockpos = pos.down();
|
| 77 |
|
| 78 | for (int i = -1; i <= 1; ++i)
|
| 79 | {
|
| 80 | for (int j = -1; j <= 1; ++j)
|
| 81 | {
|
| 82 | float f1 = 0.0F;
|
| 83 | IBlockState iblockstate = worldIn.getBlockState(blockpos.add(i, 0, j));
|
| 84 |
|
| 85 | if (iblockstate.getBlock() == Blocks.farmland)
|
| 86 | {
|
| 87 | f1 = 1.0F;
|
| 88 |
|
| 89 | if (((Integer)iblockstate.getValue(BlockFarmland.MOISTURE)).intValue() > 0)
|
| 90 | {
|
| 91 | f1 = 3.0F;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | if (i != 0 || j != 0)
|
| 96 | {
|
| 97 | f1 /= 4.0F;
|
| 98 | }
|
| 99 |
|
| 100 | f += f1;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | BlockPos blockpos1 = pos.north();
|
| 105 | BlockPos blockpos2 = pos.south();
|
| 106 | BlockPos blockpos3 = pos.west();
|
| 107 | BlockPos blockpos4 = pos.east();
|
| 108 | boolean flag = blockIn == worldIn.getBlockState(blockpos3).getBlock() || blockIn == worldIn.getBlockState(blockpos4).getBlock();
|
| 109 | boolean flag1 = blockIn == worldIn.getBlockState(blockpos1).getBlock() || blockIn == worldIn.getBlockState(blockpos2).getBlock();
|
| 110 |
|
| 111 | if (flag && flag1)
|
| 112 | {
|
| 113 | f /= 2.0F;
|
| 114 | }
|
| 115 | else
|
| 116 | {
|
| 117 | boolean flag2 = blockIn == worldIn.getBlockState(blockpos3.north()).getBlock() || blockIn == worldIn.getBlockState(blockpos4.north()).getBlock() || blockIn == worldIn.getBlockState(blockpos4.south()).getBlock() || blockIn == worldIn.getBlockState(blockpos3.south()).getBlock();
|
| 118 |
|
| 119 | if (flag2)
|
| 120 | {
|
| 121 | f /= 2.0F;
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | return f;
|
| 126 | }
|
| 127 |
|
| 128 | public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
|
| 129 | {
|
| 130 | return (worldIn.getLight(pos) >= 8 || worldIn.canSeeSky(pos)) && this.canPlaceBlockOn(worldIn.getBlockState(pos.down()).getBlock());
|
| 131 | }
|
| 132 |
|
| 133 | protected Item getSeed()
|
| 134 | {
|
| 135 | return Items.wheat_seeds;
|
| 136 | }
|
| 137 |
|
| 138 | protected Item getCrop()
|
| 139 | {
|
| 140 | return Items.wheat;
|
| 141 | }
|
| 142 |
|
| 143 | /**
|
| 144 | * Spawns this Block's drops into the World as EntityItems.
|
| 145 | */
|
| 146 | public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, IBlockState state, float chance, int fortune)
|
| 147 | {
|
| 148 | super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);
|
| 149 |
|
| 150 | if (!worldIn.isRemote)
|
| 151 | {
|
| 152 | int i = ((Integer)state.getValue(AGE)).intValue();
|
| 153 |
|
| 154 | if (i >= 7)
|
| 155 | {
|
| 156 | int j = 3 + fortune;
|
| 157 |
|
| 158 | for (int k = 0; k < j; ++k)
|
| 159 | {
|
| 160 | if (worldIn.rand.nextInt(15) <= i)
|
| 161 | {
|
| 162 | spawnAsEntity(worldIn, pos, new ItemStack(this.getSeed(), 1, 0));
|
| 163 | }
|
| 164 | }
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | /**
|
| 170 | * Get the Item that this Block should drop when harvested.
|
| 171 | */
|
| 172 | public Item getItemDropped(IBlockState state, Random rand, int fortune)
|
| 173 | {
|
| 174 | return ((Integer)state.getValue(AGE)).intValue() == 7 ? this.getCrop() : this.getSeed();
|
| 175 | }
|
| 176 |
|
| 177 | public Item getItem(World worldIn, BlockPos pos)
|
| 178 | {
|
| 179 | return this.getSeed();
|
| 180 | }
|
| 181 |
|
| 182 | /**
|
| 183 | * Whether this IGrowable can grow
|
| 184 | */
|
| 185 | public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
|
| 186 | {
|
| 187 | return ((Integer)state.getValue(AGE)).intValue() < 7;
|
| 188 | }
|
| 189 |
|
| 190 | public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 191 | {
|
| 192 | return true;
|
| 193 | }
|
| 194 |
|
| 195 | public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
|
| 196 | {
|
| 197 | this.grow(worldIn, pos, state);
|
| 198 | }
|
| 199 |
|
| 200 | /**
|
| 201 | * Convert the given metadata into a BlockState for this Block
|
| 202 | */
|
| 203 | public IBlockState getStateFromMeta(int meta)
|
| 204 | {
|
| 205 | return this.getDefaultState().withProperty(AGE, Integer.valueOf(meta));
|
| 206 | }
|
| 207 |
|
| 208 | /**
|
| 209 | * Convert the BlockState into the correct metadata value
|
| 210 | */
|
| 211 | public int getMetaFromState(IBlockState state)
|
| 212 | {
|
| 213 | return ((Integer)state.getValue(AGE)).intValue();
|
| 214 | }
|
| 215 |
|
| 216 | protected BlockState createBlockState()
|
| 217 | {
|
| 218 | return new BlockState(this, new IProperty[] {AGE});
|
| 219 | }
|
| 220 | }
|
| 221 |
|