Jump to content

Recommended Posts

Posted

Hi hello,  I would like to do a question.

 

I did a custom axe, but I would like to know if there is a way to tell to my

axe that when the player break the specific block with this tool destroy

all the connected blocks with this id. I'm trying to destroy the trees just

breaking only one block of the tree.

 

I'm looking for a block break event or I don't know if there is a better way

to do.

 

Thanks for the help.

Posted

@Override
public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int x, int y, int z, EntityLivingBase par7EntityLivingBase)
    {
	super.onBlockDestroyed(par1ItemStack, par2World, par3, x, y, z, par7EntityLivingBase);
	if(!par7EntityLivingBase.isSneaking()) //Checks if the entity is not sneaking.  You don't need if you don't want.
	{
		if(world.getBlockId(x, y, z) == Block.wood.blockID)
		{
			this.repeatChop(par2World, x, y, z);
		}
	}
        return true;
    }

public void repeatChop(World world, int x, int y, int z)
{
	for(int i = -1; i <= 1; i++)
	{
		for(int j = -1; j <= 1; j++)
		{
			for(int k = -1; k <= 1; k++)
			{
				if(world.getBlockId(x + i, y + j, z + k) == Block.wood.blockID)
				{
					world.destroyBlock(x + i, y + j, z + k, true);
					this.repeatChop(world, x + i, y + j, z + k);
				}
			}
		}
	}
}

 

This is some personal code that I've worked on for an item in my mod that does the same thing.  You can use it if you wish.  I've removed bits that aren't needed for you.

EDIT: Fixed some code I messed up.  All is good.

width=320 height=64http://www.slothygaming.com/img/ota.png[/img]

If your grammar is shit and you blatantly don't know what you're doing, I will not help you.

Posted

@Override
public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int x, int y, int z, EntityLivingBase par7EntityLivingBase)
    {
	super.onBlockDestroyed(par1ItemStack, par2World, par3, x, y, z, par7EntityLivingBase);
	if(!par7EntityLivingBase.isSneaking()) //Checks if the entity is not sneaking.  You don't need if you don't want.
	{
		if(world.getBlockId(x, y, z) == Block.wood.blockID)
		{
			this.repeatChop(par2World, x, y, z);
		}
	}
        return true;
    }

public void repeatChop(World world, int x, int y, int z)
{
	for(int i = -1; i <= 1; i++)
	{
		for(int j = -1; j <= 1; j++)
		{
			for(int k = -1; k <= 1; k++)
			{
				if(world.getBlockId(x + i, y + j, z + k) == Block.wood.blockID)
				{
					world.destroyBlock(x + i, y + j, z + k, true);
					this.repeatChop(world, x + i, y + j, z + k);
				}
			}
		}
	}
}

 

This is some personal code that I've worked on for an item in my mod that does the same thing.  You can use it if you wish.  I've removed bits that aren't needed for you.

EDIT: Fixed some code I messed up.  All is good.

 

That repeatChop method is highly recursive and is prone to repeatedly check the same nine blocks up to 3 times each. Doing 18 times the amount of work it needs to.

Posted

Well yes.  You can change it if you wish but it is needed if you want it to successfully chop down the entire tree.  If I just broke the loop, then it would only chop one log every time.  That would then cause you to barely cut down jungle trees.  The code lets you start your chop from the middle of a tree if needed and then removes everything below and above it that share the blockID of wood.

EDIT: Somewhat misunderstood what you said.  Well, no code is perfect.  If you find a way to optimize it tell me.

width=320 height=64http://www.slothygaming.com/img/ota.png[/img]

If your grammar is shit and you blatantly don't know what you're doing, I will not help you.

Posted

Omg.Thanks, the code is working, that what I was looking for. Now watching your code I can

understand the process but is something that is I'm doing for myself I can't do because my

creativity is zero. Thanks.

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.