Jump to content

How to break a Block with Items being dropped?


Bedrock_Miner

Recommended Posts

Search the Block class for methods containing the word "drop". Choose one to override. If unsure, look at a vanilla example such as diamond ore, which drops diamonds rather than itself. See how it does it and then imitate.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

this is taken from one of my mods

 

					Block bit = par1World.getBlock(x+xo, y+yo, z+zo);
					int met = par1World.getBlockMetadata(x+xo, y+yo, z+zo);
					if ((bit instanceof BlockLog)||(bit instanceof BlockLeavesBase)){
						bit.harvestBlock(par1World, plr, x+xo, y+yo, z+zo,met);
						par1World.setBlockToAir(x+xo, y+yo, z+zo);
					}

Link to comment
Share on other sites

You shouldn't need to dive into the mechanics of breaking the block; the vanilla behavior should get that far as is. As for hitting until it breaks, that just drops the block by default, not some item (e.g. diamond) found within an ore (e.g. diamond ore).

 

Your something-Block subclass needs to override a method pertaining to what it drops when harvested, but I can't recall the exact method name. Search for "drop" and imitate the BlockDiamond class.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

The way I've done it is the same as the way BuildCraft has done it.

You first get drops by calling block.getDrops(), then you get drop chance by calling ForgeEventFactory.fireBlockHarvesting() which is the same fired by forge when a block breaks and is being harvested. Then at last you make another empty list and iterate over the drops list and if a random generated number (every time) is less than the chance you add the drop to your other list.

After you get the drops, iterate over them and spawn each by using world.spawnEntityInWorld().

Link to comment
Share on other sites

It's not that hard to look into the ore block file...

 

@Override
public Item getItemDropped(int par1, Random par2, int par3) {
	return Items.diamond;
}

 

Some blocks have different multiple drops, and some have different dropped metadata, so block.getDrops is better.

Link to comment
Share on other sites

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.