Jump to content

Recommended Posts

Posted

I'm currently trying to make my custom sapling grow into my custom tree. I copied over the files from the vanilla sapling class, but I'm not sure how to get around the biome specific tree growth. When I use bone meal on my sapling, it changes into a regular oak tree. Any help would be appreciated!

 

 

Custom Sapling

 

public class SilverSapling extends BlockFlower

{

public static final String[] WOOD_TYPES = new String[] {"Silver"};

    private static final String[] field_94370_b = new String[] {"SilverSapling"};

   

    protected SilverSapling(int par1, int par2)

    {

        super(par1);

        float f = 0.4F;

        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);

        this.setCreativeTab(CreativeTabs.tabDecorations);

    }

 

    /**

    * Determines the damage on the item the block drops. Used in cloth and wood.

    */

    public int damageDropped(int par1){

    return par1 & 3;

    }

   

    public String getTextureFile(){

    return "/blocks/SilverSapling.png";

    }

   

    /**

    * Ticks the block if it's been scheduled

    */

    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!par1World.isRemote)

        {

            super.updateTick(par1World, par2, par3, par4, par5Random);

 

            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5Random.nextInt(7) == 0)

            {

                this.markOrGrowMarked(par1World, par2, par3, par4, par5Random);

            }

        }

    }

 

 

    public void markOrGrowMarked(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        int l = par1World.getBlockMetadata(par2, par3, par4);

 

        if ((l & 8) == 0)

        {

            par1World.setBlockMetadataWithNotify(par2, par3, par4, l | 8, 4);

        }

        else

        {

            this.growTree(par1World, par2, par3, par4, par5Random);

        }

    }

 

    /**

    * Attempts to grow a sapling into a tree

    */

    public void growTree(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!TerrainGen.saplingGrowTree(par1World, par5Random, par2, par3, par4)) return;

 

        int l = par1World.getBlockMetadata(par2, par3, par4) & 3;

        Object object = null;

        int i1 = 0;

        int j1 = 0;

      boolean flag = false;

 

        if (l == 1)

        {

            object = new WorldGenTaiga2(true);

        }

        else if (l == 2)

        {

            object = new WorldGenForest(true);

        }

        else if (l == 3)

        {

            for (i1 = 0; i1 >= -1; --i1)

            {

                for (j1 = 0; j1 >= -1; --j1)

                {

                    if (this.isSameSapling(par1World, par2 + i1, par3, par4 + j1, 3) && this.isSameSapling(par1World, par2 + i1 + 1, par3, par4 + j1, 3) && this.isSameSapling(par1World, par2 + i1, par3, par4 + j1 + 1, 3) && this.isSameSapling(par1World, par2 + i1 + 1, par3, par4 + j1 + 1, 3))

                    {

                        object = new WorldGenHugeTrees(true, 10 + par5Random.nextInt(20), 3, 3);

                        flag = true;

                        break;

                    }

                }

 

                if (object != null)

                {

                    break;

                }

            }

 

            if (object == null)

            {

                j1 = 0;

                i1 = 0;

                object = new WorldGenTrees(true, 4 + par5Random.nextInt(7), 3, 3, false);

            }

        }

        else

       

        {

            object = new WorldGenCustomTree(true);

 

            if (par5Random.nextInt(10) == 0)

            {

                object = new WorldGenCustomTree(true);

            }

        }

 

        if (flag)

        {

            par1World.setBlock(par2 + i1, par3, par4 + j1, 0, 0, 4);

            par1World.setBlock(par2 + i1 + 1, par3, par4 + j1, 0, 0, 4);

            par1World.setBlock(par2 + i1, par3, par4 + j1 + 1, 0, 0, 4);

            par1World.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, 0, 0, 4);

        }

        else

        {

            par1World.setBlock(par2, par3, par4, 0, 0, 4);

        }

 

        if (!((WorldGenerator)object).generate(par1World, par5Random, par2 + i1, par3, par4 + j1))

        {

            /*if (flag)

            {

                par1World.setBlock(par2 + i1, par3, par4 + j1, this.blockID, l, 4);

                par1World.setBlock(par2 + i1 + 1, par3, par4 + j1, this.blockID, l, 4);

                par1World.setBlock(par2 + i1, par3, par4 + j1 + 1, this.blockID, l, 4);

                par1World.setBlock(par2 + i1 + 1, par3, par4 + j1 + 1, this.blockID, l, 4);

           

            else*/

            {

                par1World.setBlock(par2, par3, par4, this.blockID, l, 4);

            }

        }

    }

 

    /**

    * Determines if the same sapling is present at the given location.

    */

    public boolean isSameSapling(World par1World, int par2, int par3, int par4, int par5)

    {

        return par1World.getBlockId(par2, par3, par4) == this.blockID && (par1World.getBlockMetadata(par2, par3, par4) & 3) == par5;

    }

}

 

 

 

Here is the new code that actually works:

 

public class SilverSapling extends BlockFlower

{

public static final String[] WOOD_TYPES = new String[] {"Silver"};

    private static final String[] Sapling = new String[] {"SilverSapling"};

    private Icon[] SilverSaplingIcon;

   

    protected SilverSapling(int par1, int par2)

    {

        super(par1);

        float f = 0.4F;

        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);

        this.setCreativeTab(CreativeTabs.tabDecorations);

    }

 

    /**

    * Determines the damage on the item the block drops. Used in cloth and wood.

    */

    public int damageDropped(int par1){

    return par1 & 3;

    }

   

   

    public String getTextureFile(){

    return "/blocks/SilverSapling.png";

    }

   

    /**

    * Ticks the block if it's been scheduled

    */

    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!par1World.isRemote)

        {

            super.updateTick(par1World, par2, par3, par4, par5Random);

 

            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5Random.nextInt(7) == 0)

            {

                this.markOrGrowMarked(par1World, par2, par3, par4, par5Random);

            }

        }

    }

 

 

    public void markOrGrowMarked(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        int l = par1World.getBlockMetadata(par2, par3, par4);

 

        if ((l & 8) == 0)

        {

            par1World.setBlockMetadataWithNotify(par2, par3, par4, l | 8, 4);

        }

        else

        {

            this.growTree(par1World, par2, par3, par4, par5Random);

        }

    }

 

    /**

    * Attempts to grow a sapling into a tree

    */

    public void growTree(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!TerrainGen.saplingGrowTree(par1World, par5Random, par2, par3, par4)) return;

 

        int l = par1World.getBlockMetadata(par2, par3, par4) & 3;

        Object object = null;

        int i1 = 0;

        int j1 = 0;

      boolean flag = false;

      object = new WorldGenCustomTree(true);

   

            par1World.setBlock(par2, par3, par4, 0, 0, 4);

       

 

        if (!((WorldGenerator)object).generate(par1World, par5Random, par2 + i1, par3, par4 + j1))

        {

         

                par1World.setBlock(par2, par3, par4, this.blockID, l, 4);

            }

        }

    //}

 

    /**

    * Determines if the same sapling is present at the given location.

    */

    public boolean isSameSapling(World par1World, int par2, int par3, int par4, int par5)

    {

        return par1World.getBlockId(par2, par3, par4) == this.blockID && (par1World.getBlockMetadata(par2, par3, par4) & 3) == par5;

    }

}

 

Posted

 

Don't call super.updateTick().

You don't want the superclass to be growing things on top of you.

 

getBlockMetaData() probably does not behave as you would expect.

If you are expecting a certain value, it will probably not be there.

(it has never worked for me)

 

 

Odds are good I've been programming since before you were born. Yeah. I'm OLD school.

Posted

Alright, I extended the BlockSapling class, but now my saplings appear as regular oak saplings that grow into my custom tree. So I need to make the class choose my custom texture. Also, if anyone has any input on how I can clean up this code to get rid of the parts I don't need I would appreciate it.

Posted

Yay! I got it to work! Thanks GoToLink for the help! The tip on extending BlockSapling helped tremendously! I was able to figure it out after that by commenting out all the different parts until I better understood what each part did. I will post the code now for anyone who is curious about making their own.

 

 

public class SilverSapling extends BlockFlower

{

public static final String[] WOOD_TYPES = new String[] {"Silver"};

    private static final String[] Sapling = new String[] {"SilverSapling"};

    private Icon[] SilverSaplingIcon;

   

    protected SilverSapling(int par1, int par2)

    {

        super(par1);

        float f = 0.4F;

        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);

        this.setCreativeTab(CreativeTabs.tabDecorations);

    }

 

    /**

    * Determines the damage on the item the block drops. Used in cloth and wood.

    */

    public int damageDropped(int par1){

    return par1 & 3;

    }

   

   

    public String getTextureFile(){

    return "/blocks/SilverSapling.png";

    }

   

    /**

    * Ticks the block if it's been scheduled

    */

    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!par1World.isRemote)

        {

            super.updateTick(par1World, par2, par3, par4, par5Random);

 

            if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9 && par5Random.nextInt(7) == 0)

            {

                this.markOrGrowMarked(par1World, par2, par3, par4, par5Random);

            }

        }

    }

 

 

    public void markOrGrowMarked(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        int l = par1World.getBlockMetadata(par2, par3, par4);

 

        if ((l & 8) == 0)

        {

            par1World.setBlockMetadataWithNotify(par2, par3, par4, l | 8, 4);

        }

        else

        {

            this.growTree(par1World, par2, par3, par4, par5Random);

        }

    }

 

    /**

    * Attempts to grow a sapling into a tree

    */

    public void growTree(World par1World, int par2, int par3, int par4, Random par5Random)

    {

        if (!TerrainGen.saplingGrowTree(par1World, par5Random, par2, par3, par4)) return;

 

        int l = par1World.getBlockMetadata(par2, par3, par4) & 3;

        Object object = null;

        int i1 = 0;

        int j1 = 0;

      boolean flag = false;

      object = new WorldGenCustomTree(true);

   

            par1World.setBlock(par2, par3, par4, 0, 0, 4);

       

 

        if (!((WorldGenerator)object).generate(par1World, par5Random, par2 + i1, par3, par4 + j1))

        {

         

                par1World.setBlock(par2, par3, par4, this.blockID, l, 4);

            }

        }

    //}

 

    /**

    * Determines if the same sapling is present at the given location.

    */

    public boolean isSameSapling(World par1World, int par2, int par3, int par4, int par5)

    {

        return par1World.getBlockId(par2, par3, par4) == this.blockID && (par1World.getBlockMetadata(par2, par3, par4) & 3) == par5;

    }

}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.