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

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

×
×
  • Create New...

Important Information

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