Jump to content

Proper way to read/write multi-part entity to/from NBT? [SOLVED]


Recommended Posts

Posted

I'm trying to work through a chicken-and-egg problem, and I'm not sure what the "proper" method is supposed to be.

 

I have a parent entity that instantiates multiple child entities.  Parent has an array for keeping track of children (accessed via getParts) and each child has a pointer to the Parent. Everything works fine when a player builds the item (spawned on server, writeSpawnData sends all the necessary data over, and everything is instantiated and reconnected on the client).

 

The problem occurs when the parent+children is written to NBT and then read back out.  All the data is there but, because the children already exist and sometimes get spawned on the client before the parent, reconnection is all out of whack -- MC is trying to update and render the various bits before all the linkage is complete, and it keeps hitting null objects.

 

I've added copious null checks (in the MC code as well) and rigged my children to do lazy linkage back to the parent, but I'm stuck on the parent's child array.  It's an MC-defined interface (an array of known size), and it has to exist before the children start showing up, but in between the parent instantiation (and allocation of the array) and the registration of all the children, MC comes along and tries to start loading skins, hits a null, and throws.

 

I tried killing all the children (chunk remove and setDead) when the parent is written to NBT, and then creating new children when the parent is read back out, but that didn't seem to get me much further (caused different problems, like occasional concurrent access throws).

 

Is anyone else using multipart entities?  How should the parent+child NBT writing/reading be handled?

 

Posted

Travel to the end in order to ask the Enderdragon, or just look in his java classes. It is he right?

No 5 row long documentation = account on the help forums

Posted

You would think... but, no. 

 

The EnderDragon takes many shortcuts, not the least of which is the parent recreates its children in the constructor, and the children written to NBT are orphaned.  Not a huge problem when you only have one dragon with half a dozen children, and the dragon only gets instantiated when the end loads, but it quickly becomes a problem when you're dealing with dozens of parents that have hundreds of children each.

 

The more I look through the vanilla code, the more I'm convinced that the child entitiy handling was an afterthought.

Posted

Had to do a very lazy late linkage in both directions. 

  • SPAWN ORDER IS NOT DEFINED.  When restoring from NBT, objects are spawned in random order.  parent will spawn before, in the middle of, or after all the children.
  • getUniqueID() in parent extended constructor.  UUID is passed in writeSpawnData and written to NBT
  • children track parent by parent UUID (and of course, written to NBT)
  • parent.getParts() has to check for all children loaded and linked, return null if some are missing
  • chlid.onUpdate checks for linkage to parent, attempts to link, bails out if not linked

 

Additional parent/child null checks, in places you wouldn't otherwise need them, will be required.  Under normal circumstances (children spawned by parent) the parent will be known when the child instantiates, but not after an NBT load.  Same for parent iterating through its child array -- until all children are relinked there will be uninitialized members.

 

Note that external code doesn't null check array members before trying to access them.  In order to avoid throw/crash you'll have to trick them into not running (getParts() returns null until all children are linked).

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Sorry for the late response, but the game opened and I made a world but it's stuck at 0% Here's the latest.log https://mclo.gs/peEb1R8 I disabled The Factory Must Grow and soulsweapons
    • Hey everyone! Two of my friends downloaded this modpack and are having an issue with the blocks loading in correctly. Grass looks like bamboo, waystones look like two barrels stacked on eachother, and wheat looks like water and stairs. What is this problem? How can we fix it? Neither of my other friends or myself had this issue. 
    • I removed Yung's cave biome mod and It wasnt in one of those biomes however the log file said the same line (([25Apr2025 21:20:15.500] [Flywheel Task Executor #5/WARN] [Embeddium-MixinTaintDetector/]: Mod(s) [oculus] are modifying Embeddium class me.jellysquid.mods.sodium.client.render.vertex.serializers.VertexSerializerRegistryImpl, which may cause instability.))
    • Note: i had a couple of ideas, but i just don't know how to execute them. The main idea was to make the new block (let's exemplify with a mixer) be essentially a clone of the mechanical mixer (different texture tho) that would have a different recipe type (instead of mixing, advanced_mixing) and i would copy all the create mod recipes and edit the processing time while also adding the tier dependent ones.
    • Hi! Before everything, thank you for even reading. I'm coming here in need of help making a few aspects for a create mod addon. It's an addon that aims to add almost the full periodic table with realistic ways of obtaining all the elements and capability of almost full automation. For what purpose? A techy armor and to go to the moon and rocky planets of the solar system. It'll have 3 different tiers of machines (mixer, millstone, crushing wheels): basic (just the normal create mod machines but renamed), advanced (25% faster and has recipes only possible for this tier and above) end elite (75% faster than basic and recipes only available for this tier). The problem is, I'm not a coder. I know less than the basics. I know how to do everything else but these machine tiers and i need some help if you can.
  • Topics

×
×
  • Create New...

Important Information

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