Jump to content

[1.11.2/1.12] Save TileEntity data when the block breaks


Franckyi

Recommended Posts

Hi !
I'm currently saving data to my Block using a TileEntity and the ITileEntityProvider interface.
But when a player breaks the block, the TileEntity is removed and the data is lost.
I'd like to keep the data in the ItemStack dropped by the block, so when the player replaces it, it'll reload the data previously contained by the TileEntity.

1- Can I do it without using capabilities ?
2- If yes, how/when can I do the conversion from TE data to IS data ?
3- If not, how can I do it using capabilities ?

Link to comment
Share on other sites

12 minutes ago, Franckyi said:

ITileEntityProvider interface

Is outdated, Use Block::hasTileEntity and Block::createTileEntity.

 

1. Yes.

2. Vanilla has a great example of a tile entity that retains it's data when broken. See BlockShulkerBox.

3. If you so chose to you can store all the data of the tile in your item's capability. Capabilities are just means of storing/accessing data easily. Chosing to use them or not in this case is up to you.

 

In general you would want to create a custom ItemStack as a drop that contains your tile's data in it's NBT/Capability. Upon the placement of that item you would either set the data in a newly created tile on your own using a custom ItemBlock or let vanilla handle it by saving your tile data into a NBTTagCompound and storing that tag in your ItemStack that gets dropped with a BlockEntityTag as a key(yet again, see BlockShulkerBox for more clarification)

Link to comment
Share on other sites

A better example of saving data in the ItemStack when the block is broken is Forge's patches to BlockFlowerPot. This delays the removal of the TileEntity until after Block#getDrops is called, allowing you to access it there and store the data.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

20 minutes ago, Choonster said:

A better example of saving data in the ItemStack when the block is broken is Forge's patches to BlockFlowerPot. This delays the removal of the TileEntity until after Block#getDrops is called, allowing you to access it there and store the data.

Oh right I see! Thanks for the information :)

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.