Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BlockCorn extends Block implements IPlantable{
- public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 11);
- public BlockCorn()
- {
- super(Material.plants);
- setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
- float f = 0.375F;
- setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 1.0F, 0.5F + f);
- setTickRandomly(true);
- setHardness(0.3F);
- }
- @Override
- public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
- {
- int j = ((Integer)state.getValue(AGE)).intValue();
- if(j != 4 && j != 7 && j != 9 && j!= 10 && j!= 11){ //states are on the bottom,have blocks above, or corn is done, do not grow them
- if (worldIn.getBlockState(pos.down()).getBlock() == this || this.checkForDrop(worldIn, pos, state))
- {
- if (worldIn.isAirBlock(pos.up()))
- {
- if (worldIn.getLightFromNeighbors(pos.up()) >= 9)
- {
- if (rand.nextInt((int)(25.0F / .5) + 1) == 0)
- {
- //Then Corn can grow
- if(j == 0 || j == 1 || j == 2){
- worldIn.setBlockState(pos, this.getStateFromMeta(j+1));
- }
- else
- if(j == 3){
- worldIn.setBlockState(pos, this.getStateFromMeta(4));
- worldIn.setBlockState(pos.up(), this.getStateFromMeta(5));
- }
- if(j == 5){
- worldIn.setBlockState(pos, this.getStateFromMeta(6));
- }
- if(j == 6){
- worldIn.setBlockState(pos, this.getStateFromMeta(7));
- worldIn.setBlockState(pos.up(), this.getStateFromMeta(8));
- }
- if(j == 9){
- worldIn.setBlockState(pos.up(), this.getStateFromMeta(11));
- worldIn.setBlockState(pos, this.getStateFromMeta(10));
- worldIn.setBlockState(pos.down(), this.getStateFromMeta(9));
- }
- }
- }
- }
- }
- }
- }
- /*
- if (j == 15)
- {
- worldIn.setBlockState(pos.up(), this.getDefaultState());
- worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(0)), 4);
- }
- else
- {
- worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(j + 1)), 4);
- }*/
- @Override
- public void onNeighborBlockChange(World world, BlockPos pos, IBlockState state, Block neighborBlock)
- {
- this.checkForDrop(world, pos, state);
- }
- protected final boolean checkForDrop(World world, BlockPos pos, IBlockState state)
- {
- if (this.canBlockStay(world, pos))
- {
- return true;
- }
- else
- {
- int s = ((Integer)state.getValue(AGE)).intValue();
- if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
- }
- ItemStack out;
- if(world.rand.nextFloat() >= .40){
- out = new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"),2);
- }
- out = new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"));
- super.spawnAsEntity(world,pos,out);
- world.setBlockToAir(pos);
- return false;
- }
- }
- public boolean canBlockStay(World world, BlockPos pos)
- {
- if (world.getBlockState(pos.down()).getBlock() == this)
- {
- return true;
- }
- else{
- return this.canPlaceBlockAt(world, pos);
- }
- }
- @Override
- public boolean canPlaceBlockAt(World world, BlockPos pos)
- {
- Block block = world.getBlockState(pos.down()).getBlock();
- if (block.canSustainPlant(world, pos, EnumFacing.UP, this)){
- return true;
- }
- else if (block != Blocks.farmland )
- {
- return false;
- }
- return false;
- }
- @Override
- public Item getItemDropped(IBlockState state, Random rand, int fortune)
- {
- int s = ((Integer)state.getValue(AGE)).intValue();
- if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
- return GameRegistry.findItem(Corn.MODID, "corncob");
- }
- return GameRegistry.findItem(Corn.MODID, "kernels");
- }
- @Override
- public boolean isOpaqueCube()
- {
- return false;
- }
- @Override
- public boolean isFullCube()
- {
- return false;
- }
- @SideOnly(Side.CLIENT)@Override
- public Item getItem(World worldIn, BlockPos pos)
- {
- int s = ((Integer)worldIn.getBlockState(pos).getValue(AGE)).intValue();
- if(s == 9 || s == 10 || s == 11 ){ //if corn is ripe
- return GameRegistry.findItem(Corn.MODID, "corncob");
- }
- return GameRegistry.findItem(Corn.MODID, "kernels");
- }
- @Override
- public List<ItemStack> getDrops(IBlockAccess world,BlockPos pos, IBlockState state, int fortune){
- List<ItemStack> out = new ArrayList<ItemStack>();
- if(((World)world).rand.nextFloat() >= .50){
- out.add(new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob"),2));
- return out;
- }
- out.add(new ItemStack(GameRegistry.findItem(Corn.MODID, "corncob")));
- return out;
- }
- @SideOnly(Side.CLIENT)@Override
- public int colorMultiplier(IBlockAccess worldIn, BlockPos pos, int renderPass)
- {
- return worldIn.getBiomeGenForCoords(pos).getGrassColorAtPos(pos);
- }
- @Override
- public int getMetaFromState(IBlockState state)
- {
- return ((Integer)state.getValue(AGE)).intValue();
- }
- @Override
- public IBlockState getStateFromMeta(int meta)
- {
- return this.getDefaultState().withProperty(AGE, Integer.valueOf(meta));
- }
- @Override
- protected BlockState createBlockState()
- {
- return new BlockState(this, new IProperty[] {AGE});
- }
- @Override
- public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos) {
- return EnumPlantType.Crop;
- }
- protected Item getSeed()
- {
- return GameRegistry.findItem(Corn.MODID, "kernels");
- }
- @Override
- public IBlockState getPlant(IBlockAccess world, BlockPos pos) {
- // TODO Auto-generated method stub
- IBlockState state = world.getBlockState(pos);
- if (state.getBlock() != this) return getDefaultState();
- return state;
- //return this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment