Jump to content

Get block position from block object...?


Keter

Recommended Posts

So, I'm using the Event PlayerEvent.HarvestCheck and I'm trying to make the harvested block explode in the player's face. Don't ask why. However, I've run into a bit of a problem. There doesn't seem to be any way whatsoever to actually get the location of a block (to make an explosion) with nothing but the block object itself. Entities have static variables that are very easy to use to get an explosion, but I want the explosion to actually originate from the block itself.

 

@ForgeSubscribe
public void blockHarvested(PlayerEvent.HarvestCheck event)
{
	event.entityPlayer.worldObj.createExplosion((Entity)null, event.block.posX., event.block.posY, event.block.posZ, 4.0F, true);
}

 

Yep, that's all I have. Obviously it doesnt work - the Block class doesn't have posX, or posY, or posZ. Anyone have any clue how this could be accomplished?

Link to comment
Share on other sites

what if you give the block a tile entity and create the explosion from the tile entity.

That would be wasteful.

 

Try adding the onBlockAdded(World, x, y, z) method. This is called whenever the block is added to the world (placed, oregen, setBlockWithNotify...).

From there, set 3 variables - x, y, and z.

 

Then, in the blockDestroyedByPlayer (or whatever it is called), you can use those variables.

Link to comment
Share on other sites

If you need to use PlayerEvent.HarvestCheck and really want to make the block explode, not the player, its not possible, not even with a TileEntity (to get the TE you'd need the Coordinates as well).

 

I mean that you create the explosion in the tile entity. Not get the coordinates from the tile entity and then create the explosion in the block

Link to comment
Share on other sites

@Reika: That wouldn't work, because there is only one instance of each block. So if you mined a block it would always make the block explode, that was placed last.

If you need to use PlayerEvent.HarvestCheck and really want to make the block explode, not the player, its not possible, not even with a TileEntity (to get the TE you'd need the Coordinates as well).

 

Are you sure? I have had my blocks have variables store internal data (like "int dropmeta") which was unique to each block.

Link to comment
Share on other sites

@

Are you sure? I have had my blocks have variables store internal data (like "int dropmeta") which was unique to each block.

Yes, I am sure. If you used TileEntities thats something else. TileEntities exist multiple times. Only the Block exists once.

 

Then explain how I was able to make the same block have different light values dependent on its metadata (multiple values in the world at once) - using vanilla code - and no Tile Entities...

Link to comment
Share on other sites

couldn't one do the explosion in front of the player, based on his location and the way he is facing?

I wrote possible solution quite time ago, it does exactly what you're saying:

you have a player entity so you could do something like this maybe

player.rayTrace

to get the active block?

mnn.getNativeLang() != English

If I helped you please click on the "thank you" button.

Link to comment
Share on other sites

This option is the easiest: use the player.getLook(1.0F); to obtain the normalized vector the player is heading. Then get his position, add this vector and make an explosion at these coordinates - exactly 1 meter in front of the player.

 

My other idea is to use Minecraft.objectMouseOver object to obtain the coordinates of the block the player is currently looking at. If during the event call objectMouseOver is already updated ( so that there's no block being destroyed there ), just monitor this variable and store the coordinates somewhere.

 

Just my few cents.

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.