Code (cpp):
package pda.common;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class BlockPDACrop extends BlockBush
{
@SideOnly(Side.CLIENT)
private IIcon icones[];
private int nbIcons;
private Block soil;
private float growingSpeed;
private String baseTextureName;
public BlockPDACrop(String name, int n, Block b, float gs)
{
super();
setTickRandomly(true);
float f = 0.5F;
setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
this.setCreativeTab((CreativeTabs) null);
GameRegistry.registerBlock(this,name);
this.setBlockName(name);
baseTextureName="pda:"+name;
nbIcons=n;
icones = new IIcon[nbIcons-1];
soil=b;
growingSpeed=gs;
setStepSound(Block.soundTypeGrass);
}
protected boolean canPlaceBlockOn(Block b)
{
return b == soil;
}
public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
{
super.updateTick(par1World, par2, par3, par4, par5Random);
if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9)
{
int i = par1World.getBlockMetadata(par2, par3, par4);
if (i < 4) {
float f = getGrowthRate(par1World, par2, par3, par4);
if (par5Random.nextInt((int) (growingSpeed / f) + 1) == 0)
{
i++;
par1World.setBlockMetadataWithNotify(par2, par3, par4, i, 2);
}
}
}
}
private float getGrowthRate(World par1World, int par2, int par3, int par4)
{
float f = 1.0F;
Block i = par1World.getBlock(par2, par3, par4 - 1);
Block j = par1World.getBlock(par2, par3, par4 + 1);
Block k = par1World.getBlock(par2 - 1, par3, par4);
Block l = par1World.getBlock(par2 + 1, par3, par4);
Block i1 = par1World.getBlock(par2 - 1, par3, par4 - 1);
Block j1 = par1World.getBlock(par2 + 1, par3, par4 - 1);
Block k1 = par1World.getBlock(par2 + 1, par3, par4 + 1);
Block l1 = par1World.getBlock(par2 - 1, par3, par4 + 1);
boolean flag = k == this || l == this;
boolean flag1 = i == this || j == this;
boolean flag2 = i1 == this || j1 == this || k1 == this
|| l1 == this;
for (int i2 = par2 - 1; i2 <= par2 + 1; i2++) {
for (int j2 = par4 - 1; j2 <= par4 + 1; j2++) {
Block k2 = par1World.getBlock(i2, par3 - 1, j2);
float f1 = 0.0F;
if (k2 == soil) {
f1 = 1.0F;
if (par1World.getBlockMetadata(i2, par3 - 1, j2) > 0) {
f1 = 3F;
}
}
if (i2 != par2 || j2 != par4) {
f1 /= 4F;
}
f += f1;
}
}
if (flag2 || flag && flag1) {
f /= 2.0F;
}
return f;
}
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister par1IconRegister)
{
this.blockIcon = par1IconRegister.registerIcon(baseTextureName+"1");
for(int i=2; i<=nbIcons; i++)
icones[i-2] = par1IconRegister.registerIcon(baseTextureName+String.valueOf(i));
}
public IIcon getIcon(int i, int j)
{
j=MathHelper.clamp_int(j, 0, nbIcons-1);
if (j == 0)
return blockIcon;
return icones[j-1];
}
public int getRenderType()
{
return 1;
}
public Item getItemDropped(int i, Random random, int j)
{
if (i == nbIcons-1)
return baseTextureName.equals("pda:croppiment") ? PDA.piment :
(baseTextureName.equals("pda:cropvigne") ? PDA.raisin :
(baseTextureName.equals("pda:fraisier") ? PDA.fraise :
(baseTextureName.equals("pda:croptomate") ? PDA.tomate : PDA.mais)));
return null;
}
public int quantityDropped(Random random)
{
return (baseTextureName.equals("pda:croppiment") ? 1 : 2) + random.nextInt(3);
}
public boolean onBlockActivated(World par1World, int par2, int par3, int par4,
EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
{
ItemStack itemstack = par5EntityPlayer.inventory.getCurrentItem();
if (!(itemstack == null) && itemstack.getItem() == Items.dye
&& itemstack.getItemDamage() == 15)
{
par1World.setBlock(par2, par3, par4, this, nbIcons-1, 3);
if (!par5EntityPlayer.capabilities.isCreativeMode && --itemstack.stackSize <= 0)
{
par5EntityPlayer.inventory.setInventorySlotContents(par5EntityPlayer.inventory.currentItem, null);
}
if (!par1World.isRemote)
par1World.playAuxSFX(2005, par2, par3, par4, 0);
return true;
}
return false;
}
public ItemStack getPickBlock(MovingObjectPosition target, World world, int x, int y, int z)
{
return new ItemStack(baseTextureName.equals("pda:croppiment") ? PDA.seedpiment :
(baseTextureName.equals("pda:cropvigne") ? PDA.raisin :
(baseTextureName.equals("pda:fraisier") ? PDA.fraise :
(baseTextureName.equals("pda:croptomate") ? PDA.tomate : PDA.mais))));
}
}