Jump to content

Recommended Posts

Posted

I am working on a custom crop and instead of just copying and pasting code I am trying to understand what is going on. Some of the code I understand but the code related to the ticking of the block is throwing me could someone explain what is going on? The mechanics, not the Java basics... I got that part down but the obfuscation is making this hard to wrap my head around.

 

Here is the code (BlockCrops.java from mojang)

 

 

protected BlockCrops () {
     float f = 0.5F;
     this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
     ...more code not included
}

 

 

 

public void updateTick(World p_149674_1_, int p_149674_2_, int p_149674_3_, int p_149674_4_, Random p_149674_5_)
    {
        super.updateTick(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_, p_149674_5_);

        if (p_149674_1_.getBlockLightValue(p_149674_2_, p_149674_3_ + 1, p_149674_4_) >= 9)
        {
            int l = p_149674_1_.getBlockMetadata(p_149674_2_, p_149674_3_, p_149674_4_);

            if (l < 7)
            {
                float f = this.func_149864_n(p_149674_1_, p_149674_2_, p_149674_3_, p_149674_4_);

                if (p_149674_5_.nextInt((int)(25.0F / f) + 1) == 0)
                {
                    ++l;
                    p_149674_1_.setBlockMetadataWithNotify(p_149674_2_, p_149674_3_, p_149674_4_, l, 2);
                }
            }
        }
    }

    public void func_149863_m(World p_149863_1_, int p_149863_2_, int p_149863_3_, int p_149863_4_)
    {
        int l = p_149863_1_.getBlockMetadata(p_149863_2_, p_149863_3_, p_149863_4_) + MathHelper.getRandomIntegerInRange(p_149863_1_.rand, 2, 5);

        if (l > 7)
        {
            l = 7;
        }

        p_149863_1_.setBlockMetadataWithNotify(p_149863_2_, p_149863_3_, p_149863_4_, l, 2);
    }

    private float func_149864_n(World p_149864_1_, int p_149864_2_, int p_149864_3_, int p_149864_4_)
    {
        float f = 1.0F;
        Block block = p_149864_1_.getBlock(p_149864_2_, p_149864_3_, p_149864_4_ - 1);
        Block block1 = p_149864_1_.getBlock(p_149864_2_, p_149864_3_, p_149864_4_ + 1);
        Block block2 = p_149864_1_.getBlock(p_149864_2_ - 1, p_149864_3_, p_149864_4_);
        Block block3 = p_149864_1_.getBlock(p_149864_2_ + 1, p_149864_3_, p_149864_4_);
        Block block4 = p_149864_1_.getBlock(p_149864_2_ - 1, p_149864_3_, p_149864_4_ - 1);
        Block block5 = p_149864_1_.getBlock(p_149864_2_ + 1, p_149864_3_, p_149864_4_ - 1);
        Block block6 = p_149864_1_.getBlock(p_149864_2_ + 1, p_149864_3_, p_149864_4_ + 1);
        Block block7 = p_149864_1_.getBlock(p_149864_2_ - 1, p_149864_3_, p_149864_4_ + 1);
        boolean flag = block2 == this || block3 == this;
        boolean flag1 = block == this || block1 == this;
        boolean flag2 = block4 == this || block5 == this || block6 == this || block7 == this;

        for (int l = p_149864_2_ - 1; l <= p_149864_2_ + 1; ++l)
        {
            for (int i1 = p_149864_4_ - 1; i1 <= p_149864_4_ + 1; ++i1)
            {
                float f1 = 0.0F;

                if (p_149864_1_.getBlock(l, p_149864_3_ - 1, i1).canSustainPlant(p_149864_1_, l, p_149864_3_ - 1, i1, ForgeDirection.UP, this))
                {
                    f1 = 1.0F;

                    if (p_149864_1_.getBlock(l, p_149864_3_ - 1, i1).isFertile(p_149864_1_, l, p_149864_3_ - 1, i1))
                    {
                        f1 = 3.0F;
                    }
                }

                if (l != p_149864_2_ || i1 != p_149864_4_)
                {
                    f1 /= 4.0F;
                }

                f += f1;
            }
        }

        if (flag2 || flag && flag1)
        {
            f /= 2.0F;
        }

        return f;
    }

 

 

Basically I get the idea that the block ticks. On each tick it checks if there is enough light to support the plant, but what is the part about the floating point numbers looking like they have something to with the size of bounding box around the plant.

 

Am I on the right path? My guess is not close...

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.” - Linus Torvalds

Posted

The BlockBounds is to set how to get the hitboxes, like wheat its different from regular blocks.

 

The update tick checks (each tick) for valid light - so it can grow, and checks if the crop metadata is 7, if it is (then it will become harvest-able)

Posted

Hi

 

Coding in 1.7.2 is still a pain in the rear.

 

Here is the code from 1.6.4 (with a couple of parameters renamed)

 

    /**
     * Ticks the block if it's been scheduled
     */
    public void updateTick(World par1World, int x, int y, int z, Random par5Random)
    {
        super.updateTick(par1World, x, y, z, par5Random);

        if (par1World.getBlockLightValue(x, y + 1, z) >= 9)
        {
            int l = par1World.getBlockMetadata(x, y, z);

            if (l < 7)
            {
                float f = this.getGrowthRate(par1World, x, y, z);

                if (par5Random.nextInt((int)(25.0F / f) + 1) == 0)
                {
                    ++l;
                    par1World.setBlockMetadataWithNotify(x, y, z, l, 2);
                }
            }
        }
    }

 

The weird stuff in this line

        float f = 0.5F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);

is probably because the original code had something like

        float HALF_CROP_BLOCK_WIDTH = 0.5F;
        this.setBlockBounds(0.5F - HALF_CROP_BLOCK_WIDTH, 0.0F, 0.5F - HALF_CROP_BLOCK_WIDTH, 0.5F + HALF_CROP_BLOCK_WIDTH, 0.25F, 0.5F + HALF_CROP_BLOCK_WIDTH);

 

(You realise that the variable f in the constructor has nothing to do with the f in the updatetick, yeah?)

 

-TGG

 

Posted

Yeah, at first I thought they were related then after looking at it again I realized they are not.

 

Thanks for the explained code. It helps a lot. Hopefully things get better in 1.7 soon. I know they are hard at work.

“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.” - Linus Torvalds

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.