Jump to content

Execute Code on Block Update


Onmar
 Share

Recommended Posts

Hello Guys

 

I'm currently developing a mod for forge, and I would like to execute some code whenever a block gets changed in the world (blockBreak, blockPlace, but also TileEntityChange etc.)

 

I have found out that there is a packet sent to the client if there is a blockUpdate.

(net.minecraft.network.packet.Packet53BlockChange.java)

 

Can I use/make a hook that executes my code when the packet is sent, or is there a better way to do this?

Also, will it execute if a TileEntity changes (e.g. the player takes something out of a chest)?

 

It would also be very nice, if this could be done server-side. (My code won't render anything)

Link to comment
Share on other sites

Events for Block breaking / placing have been in planning for several months. I personally doubt it will happen, but never be too negative about it.

 

For Block breaking its relatively easy. Depending on if you want the event to fire always or only when a player breaks the block you need to hook into Chunk.setBlockIDWithMetadata, where it calls Block.breakBlock.

For a player related event use ItemInWorldManager.removeBlock for a place to hook into.

 

And a block placing event is a bit more tough. In ItemStack.tryPlaceItemIntoWorld at the beginning you need to store the given player somewhere (a static field probably). Then let the onItemUse stuff run normally and when you get a call to world.setBlock (you need to hook there, too) assume it's caused by the player you stored earlier. Then after that's done (end of tryPlaceItemIntoWorld) reset the static field to null.

 

Of course you have to be a coremod to do all this.

Link to comment
Share on other sites

Thanks for your reply.

 

Hooking into the breakBlock and setBlock event is a good idea, but I would also like to fire the event if a Tile Entity changes.

I think of it as a "blockChangeEvent" or "ID/Metadata/TileEntity-ChangeEvent".

 

All the data I need is the xyz-coordinate of the block.

Also, I want it to always fire, not only if a player does something.

Link to comment
Share on other sites

Well, for metadata change you'd have to hook into World.setBlockMetadataWithNotify as well.

And for TileEntity changing, you have to decide what you mean by a TileEntity changing. Because there are a lot of different TileEntities, especially when you consider non-vanilla ones. There is no way you can detect if anything in any of those changes.

Link to comment
Share on other sites

There is no way you can detect if anything in any of those changes.

in theorie you could catch some changes by hooking markBlockForUpdate serverside, since modders have to call this to propagate the changes to the clients BUT it could also be called for no reason (and no change)

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

Link to comment
Share on other sites

True words. But it would be unstable because you'd never know.

yeah, but in theory you could catch those, obviously you wouldn't be able to cancel those or get any relevent information out of it because it's different between every TE, but you could catch the "update" part

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

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
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.

 Share



×
×
  • Create New...

Important Information

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