Jump to content

[SOLVED] Is it possible to save a block tile entity inventory when the block is broken?


Recommended Posts

Posted (edited)

I followed this tutorial: https://wiki.mcjty.eu/modding/index.php/GUI-1.12

 

and have been reading about Name Binary Tags to understand them. I checked the ShulkerBox class and its respective TileEntity, but it is very different from what is used in the tutorial. I have been wondering if it is possible for a simple TileEntity to save the inventory when the block is mined by the player such that when the player places it again in the world, it has the inventory. As of now, the inventory that was stored disappears if the block is broken. I cannot identify which part of the ShulkerBox makes the inventory persistent.

 

What steps does one need to follow to make the inventory persist when a block gets broken?

Edited by ctbe
Mark as solved
Posted

Override Block#getDrops to do the following:

  • Write the TileEntity to NBT.
  • Create the ItemStack that will be dropped.
  • Set the "BlockEntityTag" key of the ItemStack's compound tag to the compound tag containing the TileEntity data.
  • Add the ItemStack to the drops list.

The TileEntity is normally removed from the world before Block#getDrops is called, so you need to delay its removal by overriding BlockFlowerPot#removedByPlayer and BlockFlowerPot#harvestBlock. See Forge's patch to BlockFlowerPot for an example of this.

 

When an ItemBlock (or ItemBlockSpecial) is placed by a player and the ItemStack has a compound tag at the "BlockEntityTag" key, it will automatically call TileEntity#readFromNBT with the compound tag (unless TileEntity#onlyOpsCanSetNbt returns true and the player isn't an op).

  • Like 1

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.

Posted

I must be missing something. I mine my block, harvest the drop I get from the overriden getDrops method, but when I place it back in the world, the items are not there.

 

  override def getDrops(drops: NonNullList[ItemStack], world: IBlockAccess, pos: BlockPos, state: IBlockState, fortune: Int): Unit = {
    var tileEntity : TileEntity = world.getTileEntity(pos)
    if (tileEntity.isInstanceOf[TestContainerTileEntity]) {
      // Get the tile entity
      var testContainerTileEntity : TestContainerTileEntity = tileEntity.asInstanceOf[TestContainerTileEntity]
      // Make an nbt tag compund for the tile entity
      var tileEntityNbtTagCompound = new NBTTagCompound()
      // Step 1: Write tile entity to NBT
      testContainerTileEntity.saveToNbt(tileEntityNbtTagCompound)
      // Step 2: Create the item stack that will be dropped
      var itemstack = new ItemStack(Item.getItemFromBlock(this))
      // Make an NBTTagCompound for the item stack
      var itemStackNbtTagCompound = new NBTTagCompound()
      itemstack.writeToNBT(itemStackNbtTagCompound)
      // Step 3: Set the key "BlockEntityTag" of the item stack compound to the compound tag that contains the tile entity data
      itemStackNbtTagCompound.setTag("BlockEntityTag", tileEntityNbtTagCompound)
      // Step 4: Add the ItemStack to the drops lists
      drops.add(itemstack)
    }
  }

 

I debugged and all the NBTTagCompounds have the data on them before the method ends.

 

Do I have to modify the loadFromNBT() or readFromNBT() methods to be different that what they were in the tutorial so that it populates the items as they were before the block got broken? Or is the problem here in my code?

Posted
8 minutes ago, ctbe said:

var itemStackNbtTagCompound = new NBTTagCompound()

itemstack.writeToNBT(itemStackNbtTagCompound)

 

// Step 3: Set the key "BlockEntityTag" of the item stack compound to the compound tag that contains the tile entity data itemStackNbtTagCompound.setTag("BlockEntityTag", tileEntityNbtTagCompound)

 

// Step 4: Add the ItemStack to the drops lists

drops.add(itemstack)

You do not need to write the itemstack into NBT. You need to write TE's data into NBT and set that NBT to itemstack's tag with a BlockEntityTag key. Currently you are creating a new NBT, writing the ItemStack in there, writing your TE data in there and discarding the NBT. The ItemStack has no NBT associated with it as a result. Use ItemStack::setTagInfo to write an NBT into your ItemStack's compound.

  • Like 1
Posted
1 hour ago, V0idWa1k3r said:

You do not need to write the itemstack into NBT. You need to write TE's data into NBT and set that NBT to itemstack's tag with a BlockEntityTag key. Currently you are creating a new NBT, writing the ItemStack in there, writing your TE data in there and discarding the NBT. The ItemStack has no NBT associated with it as a result. Use ItemStack::setTagInfo to write an NBT into your ItemStack's compound.

 

O.o How did I ended up messing it like that. Thanks, it's fixed and it works now. The final code looks like so

 

  override def getDrops(drops: NonNullList[ItemStack], world: IBlockAccess, pos: BlockPos, state: IBlockState, fortune: Int): Unit = {
    var tileEntity : TileEntity = world.getTileEntity(pos)
    if (tileEntity != null && tileEntity.isInstanceOf[TestContainerTileEntity]) {
      // Get the tile entity
      var testContainerTileEntity : TestContainerTileEntity = tileEntity.asInstanceOf[TestContainerTileEntity]
      // Make an nbt tag compund for the tile entity
      var tileEntityNbtTagCompound = new NBTTagCompound()
      // Step 1: Write tile entity to NBT
      testContainerTileEntity.writeToNbt(tileEntityNbtTagCompound)
      // Step 2: Create the item stack that will be dropped
      var itemstack = new ItemStack(Item.getItemFromBlock(this))
      // Step 3: Set the key "BlockEntityTag" of the item stack compound to the compound tag that contains the tile entity data
      itemstack.setTagInfo("BlockEntityTag", tileEntityNbtTagCompound)
      // Step 4: Add the ItemStack to the drops list
      drops.add(itemstack)
    }
  }

 

Now it keeps the inventory when I break it and put it again in the world.

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.