Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

saving custom classes in nbt?


Atijaf
 Share

Recommended Posts

I've messed with the code a little bit and created a method called saveData.

I call this method after I add data to nbt.

 

 

 

public void saveData(World world){

WorldData data = (WorldData) world.loadItemData(WorldData.class, Identifier);

world.setItemData(Identifier, data);

data.markDirty();

world.getPerWorldStorage().saveAllData();

}

 

 

 

I run that bit of code, save and quit, reload, and data is null again.  The only thing that I can think of that would be wrong is the retrieval of the information..  Or maybe even saving it.  Is there anything wrong with this bit of code.

 

I am coding in 1.8, if that makes a difference.

 

Link to comment
Share on other sites

It will save when you either exist the world or open the ingame menu. In multiplayer it will save at the intervals set in the config.

 

Thanks for explaining that.  That's what I was worried about...

I am thinking that I probably missed something obvious near the beginning.

After I use markDirty(), it sets dirty to true.  I then save it. Re enter the command to add more data and before it even gets to the mothod, markDirty(), dirty is still true. (I'm gussing dirty is suppose to be switched back to false when it saves)

 

I will look at my code for a while and possibly make a new thread that deals more with WorldSavedData.

Link to comment
Share on other sites

All I can figure out is that the method "readFromNBT(NBTTagCompound nbt) is not getting called automatically, but when I manually call it, nbt has nothing saved in it.

 

Am I suppose to register WorldData?  or just use the method "public static WorldData get(World world).

 

Also, when the command to set a few variables is used and I use the method "nbt.setTag(Identifier, tagList)"  nbt gets data, and then closes.  If I call the method again, nbt has no data again, as if I never used the command once.  It's weird...

Link to comment
Share on other sites

Am I suppose to register WorldData?  or just use the method "public static WorldData get(World world).
No need to register it. But whenever you need access to the data you call the get() method.

Also, when the command to set a few variables is used and I use the method "nbt.setTag(Identifier, tagList)"  nbt gets data, and then closes.  If I call the method again, nbt has no data again, as if I never used the command once.  It's weird...

What what what? Your command should not set NBT! NBT is for saving to disk only!

Say you want to keep an integer, then you make an

int

field in your WorldSavedData. This field you then modify from your command (and call markDirty). writeToNBT will then eventually be called (by minecraft, never call it yourself!) and in there you encode whatever data you have (in this case the int field) into NBT (in this case by using nbt.setInteger()).

Link to comment
Share on other sites

You probably want to make all variables statis, make setters for them and in the setters call markDirty().

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.