Jump to content

[1.7.10] [Solved] How do I get nbt data from a TileEntity?


Recommended Posts

Posted

Hi. I'm making a fluid storage system that uses a TileEntity to store fluids as NBT data and also for rendering. I looked at the TileEntity class and found that readFromNBT doesn't do what I thought it did. It reads the coordinates from the specified NBTTagCompund, it doesn't retrieve NBT data from memory like I previously thought. So I was wondering how I would do that.

Posted

I'm a little confused as to what exactly you're asking. readFromNBT is called when the tile entity is loaded from memory on the nbt compound tag that represents the tile entity. So while it doesn't access the data in and of itself, it is what is used to turn that memory into the relevant information for the tile entity. As far as 'retrieving NBT data from memory' A. I don't know if you can directly (not at my desktop so I can't really look through the code to check) but B., more importantly, I don't know why you'd want or need to. The data of a tile entity isn't stored automatically- whatever you want to store, you have to do so yourself in the writeToNBT function. So any data that is stored in the tile entity's nbt you should already have direct access to from the tile entity, because otherwise you wouldn't be able to store it in the nbt in the first place. So if you wanted to store, for example, the unlocalized name of the liquid and #mb in your tile entity, it would look something like this:

@Override
public void writeToNBT(NBTTagCompound tag) {
	super.writeToNBT(tag);
                tag.setString("liquidName", liquidName);
                tag.setInteger("millibuckets", millibuckets);
                //or whatever your field names might be
} 

and you'd retrieve the information by modifying the readFromNBT function, which would be called when the tile entity is loaded form memory

@Override
public void readFromNBT(NBTTagCompound tag) {
	super.readFromNBT(tag);
                liquidName = tag.getString("liquidName");
                millibuckets = tag.getInteger("millibuckets");
                //or whatever your field names might be
} 

Other than your readFromNBT and writeFromNBT you can basically ignore the nbt. If you wanted to access the number of millibuckets for example, you'd just use the millibuckets field. Hopefully that answers your question as, as I said, I'm not 100% sure about what you meant by it.

Posted

Thanks. Basically what I'm saying is nbt saved somewhere, right? The readFromNBT method, in the Minecraft source, just retrieves the position from the specified tag. But the thing is, I don't know where to get that tag to put in that method. But I'll try what you said.

Posted

You should never call readFromNBT or writeToNBT yourself.

 

Unless you are doing Dirty Things and know what you're trying to do.

 

E.g. configure an entity that is placed as part of structure generation, and the values you want to set aren't public (for example, making a spawner spawn a custom mob on a much longer than default timer or place a specific painting).

 

NBT can give you access to values that aren't, and shouldn't be, public but sometimes you have to go "I need this to be like this for a good reason."

 

(Note: if either of those two examples can be done without modifying the NBT, using reflection, or ASM, I'd be happy to update the code).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

So what you're saying is I'm supposed to use NBT unless I absolutely have to? How else am I supposed to store information?

Posted

make sure your tile entity has public fields, or getter/setter methods, and then edit those fields instead of accessing the NBT

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted

Ok, will those fields save from one play session to the next? I am storing fluids so if the fields don't save then all the fluids are gone.

Posted

in your writeNBT and readNBT, your are given an NBT compound object, simply write/read into the NBT object all your variables, and then minecraft will call the writeNBT and readNBT for you when loading/saving the world. the NBT compound it gives you is saved in the world file, so when you load back up the world your variables are restored.

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted

Ok, will those fields save from one play session to the next? I am storing fluids so if the fields don't save then all the fluids are gone.

No, only nbt is saved on drive. How do you think mc would now that this filed in this class made by him needs to be saved???

That's why you have write and read to nbt methods. Use those to store your information... Actually enough of information is given above...

Posted

in your writeNBT and readNBT, your are given an NBT compound object, simply write/read into the NBT object all your variables, and then minecraft will call the writeNBT and readNBT for you when loading/saving the world. the NBT compound it gives you is saved in the world file, so when you load back up the world your variables are restored.

 

Oh, that makes sense. Thanks everyone. I think I know what to do now. :)

Posted

Your welcome :P

If I ever say something stupid, or simply incorrect, please excuse me. I don't know anything about 1.8 modding, and I don't know much about entities either, But I try to help when I can.

Posted

The emphasis here is that you implement writeNBT and readNBT, you don't call them (the only common exception being getDescriptionPacket and onDataPacket, as that's a form of save/load except that it's the sync between server and client rather than to disk*).

 

*IMO the override implementation used is something that should have been in the base TileEntity class, but which isn't because very few of the Vanilla TEs use it as they have special packets that sync their data, rather than the generic packet used for mod TEs.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.