Posted April 3, 20178 yr 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
April 3, 20178 yr 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.
April 3, 20178 yr Author 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 April 3, 20178 yr by BeardlessBrady
April 3, 20178 yr 4 minutes ago, BeardlessBrady said: EDIT: At some point I was checking if an entity was attached but it didn't to make a difference Do you mean it still gives you the exact same exception at the exact same line? Because that seems impossible.
April 3, 20178 yr Author I check to see if the entity is null before writing it. I updated it in the code now so you can check here: https://github.com/BeardlessBrady/Carts-Mod/blob/master/src/main/java/gunn/modcarts/mod/entity/EntityHorseCart.java#L155 This is the error that occurs when reloading the world https://pastebin.com/s0EkVu1M (Same as before)
April 3, 20178 yr Author That's where the error was before. So how do I check if an NBT tag is null. It obviously shouldn't be null as the only way it's set is if it wasn't null when it was saved Edited April 3, 20178 yr by BeardlessBrady
April 3, 20178 yr 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 April 3, 20178 yr by Jay Avery
April 3, 20178 yr Author So I should save the UUID of the attached entity to NBT? But what do I do with it when it reads it?
April 4, 20178 yr 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 April 4, 20178 yr by Kriptikz
April 4, 20178 yr Author That is a good idea however, I'm trying to store an entityHorse. The world doesn't have a ' getEntityByUUID ' like it does for the player so your method wouldn't work.
April 4, 20178 yr Author 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 April 4, 20178 yr by BeardlessBrady
April 5, 20178 yr I don't think you're actually setting attachEntity to anything right there. Also idk if that works with other entities but you cant set the entity in readNBT if its a player Edited April 5, 20178 yr by Kriptikz
April 5, 20178 yr Author Well no because when I try to get the entity with getEntityFromUuid it returns null. But why the UUID is correct. I am not trying to set it to a player, I'm setting it to a horse. Edited April 5, 20178 yr by BeardlessBrady
April 5, 20178 yr 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 April 5, 20178 yr by Kriptikz
April 15, 20178 yr Author 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 April 15, 20178 yr by BeardlessBrady
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.