Jump to content

[1.11.2] Saving to NBT in entities


BeardlessBrady

Recommended Posts

I'm having a bit of trouble successfully reading/writing things to NBT in an entity. Specifically what I'm trying to do is save the entity my entity is attached to. Below is the console error that is output, I understand its a nullPointerException I just don't see why it's doing so unless Im writing the entity wrong?

 

https://pastebin.com/5S4F3gQA

 

https://github.com/BeardlessBrady/Carts-Mod/blob/master/src/main/java/gunn/modcarts/mod/entity/EntityHorseCart.java#L155

Link to comment
Share on other sites

A NullPointerException means you tried to call a method on or access a field of a null value. The only value on that line that can be null is the attachedEntity field. You need to check that it's not null before you try to call INBTSerializable#serializeNBT on it.

 

This is basic Java knowledge.

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

Yes I know what it means and I understand that. But It isn't null before saving it so I don't see why it is when it writes it

EDIT: At some point I was checking if an entity was attached but it didn't make a difference, the error still occures

Edited by BeardlessBrady
Link to comment
Share on other sites

7 minutes ago, BeardlessBrady said:

Thats where the error was before.

Oh, my mistake - I misread your initial post.

Quote

So how do I check if an NBT tag is null. It obviously shouldn't be as the only way its set if it wasn't null when it was saved

No, it's not the tag that's null, (even if that was possible, it wouldn't be causing an NPE at that line). The source of the exception is that attachedEntity is null there. Think about it - when your entity is being loaded, nothing has set attachedEntity to be anything other than null, so there's nothing there to deserialize. I think you'll need to store the type of attached entity in your own NBT, then reconstruct it before calling deserializeNBT on it (something like storing the entity ID or name and then getting the class from the registry? not sure of the precise way to achieve that).

 

Edit: actually, constructing the attached entity yourself isn't the solution either, because that will make a new instance while the original attached entity gets reloaded by the game itself, so I think it would cause duplication. Something like getting the attached entity by its UUID? 

Edited by Jay Avery
Link to comment
Share on other sites

In my mod I have my spells store who the spellcaster is by uuid and I store the player entity in a weak reference so you don't have to get the entity by uuid every time you want to use it and it's a weak reference, instead of a normal EntityLivingBase, to prevent possible memory leaks.

Here is where I read/write the uuid to nbt:

https://github.com/Kriptikz/Archmage/blob/1.11.2-Refactor/src/main/java/kriptikz/archmage/entity/EntitySpellBase.java#L302-L326

 

Then anytime you need to do something based on the entity you can either use, also make sure to check for null if using this directly:

https://github.com/Kriptikz/Archmage/blob/1.11.2-Refactor/src/main/java/kriptikz/archmage/entity/EntitySpellBase.java#L235-L248

 

Or you can do what I do, which is every time onUpdate runs I store the caster in a local EntityLivingBase and if the caster is offline getCaster() will return null so I just set my entity to dead and return:

https://github.com/Kriptikz/Archmage/blob/1.11.2-Refactor/src/main/java/kriptikz/archmage/entity/EntitySpellBase.java#L118-L124

 

Edited by Kriptikz
Link to comment
Share on other sites

The UUID.toString saves fine but when I try to getEntityFromUuid with the UUID.fromString from the NBT it comes out null, does the UUID for mobs change when reloading a world?

 

EDIT: Ya so the UUID doesn't change. But for some reason when I put the UUID into the 'getEntityFromUuid' method it outputs null.

 

Here's where I try and find the entity:

https://github.com/BeardlessBrady/Carts-Mod/blob/master/src/main/java/gunn/modcarts/mod/entity/EntityHorseCart.java#L162

Edited by BeardlessBrady
Link to comment
Share on other sites

if getEntityFromUuid() return null when you use it in readNBT(), then don't use it there, do what I recommended above.

The only thing you would need to change from how I do it in my code would be instead of:

caster = this.world.getPlayerEntityByUUID(this.casterId);

 

you would do something like:

caster = this.world.getMinecraftServer().getEntityFromUuid(this.casterId);

I didn't test this out so no idea if you need isRemote() before using this or honestly if this is how you would actually get WorldServer in order to use getEntityFromUuid() but this is what I would try.

Edited by Kriptikz
Link to comment
Share on other sites

  • 2 weeks later...

Sorry its been awhile. I don't see what the difference from doing it in onUpdate and readfromNBT. Either way it doesn't work for me. Not sure what to try next here.

 

EDIT: I think it has something to do with the cart loading in before the horse does

Edited by BeardlessBrady
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.