Jump to content

Passing Block metadata to TileEntity


jaminv

Recommended Posts

I have a machine where I'd like things like energy capacity to be based on the meta data of the block. The meta data represents different stages of the machine. I'm having trouble delivering that data to the tile entity in a consistent way. Obviously, I shouldn't pass it to the tile entity in a constructor. Doing so doesn't work anyways because only the default constructor is called with the TileEntity is loaded from NBT. And the World member variable isn't available in the constructor when loading from NBT, so I can't poll that to get the metadata for the block.  I can store the meta upon creation and save/load it from NBT, but there's no guarantee that the "meta" NBT will be read before the "energy" NBT. If the energy is loaded first, it would be limited to the lowest capacity and energy may be lost. I could initialize it to the highest meta, but it all starts feeling pretty "hack"y at this point...

 

Looking it over, it appears the Thermal Expansion solves this issue by storing the machine type in the meta, and the machine stage in item NBT. I'm not entirely sure how this gets passed to the TileEntity, but I'm sure I can figure it out if I went down this route. But it's a pretty big code restructure. Is this indeed the best way to handle this? Or is there something I'm missing? It seems like it should be simple to get the block metadata in the TileEntity, but it's proving quite difficult. I've used it in some other places by just getting the block state, but that was always with fully initialized blocks and TEs. The difficulty I'm having is because I want to initialize TE data (like energy and fluid capacities) based on the metadata.

Link to comment
Share on other sites

Looking into Thermal Expansion further, it looks like what they do is pretty close to what I was saying. They pass the NBT data in the "onBlockPlacedBy" method of the Block (rather than passing it to the constructor). The TileEntity constructor sets the default energy capacity to an arbitrarily large value, which is then scaled down once onBlockPlacedBy is called or after the NBT is loaded.  So I guess for my current specific requirements, there's no real reason to restructure my block metadata. I guess TE does it this way to reduce block ids and/or prevent the need for a bunch of different block classes?

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.