Jump to content

Writing to player NBT


Bydon873

Recommended Posts

I'm trying to save player NBT in an external file and be able to load it later when the player logs in.

 

In a PlayerLoggedInEvent listener I put the code:

NBTInputStream inputStream = new NBTInputStream(new GZIPInputStream(new FileInputStream(savedFile)));
CompoundTag savedCompound = (CompoundTag) inputStream.readTag();

for(Map.Entry<String, ITag> entry : savedCompound.getTags().entrySet()){
     ITag tag = entry.getValue();
     event.player.getEntityData().setTag(tag.getName(), getType(tag));
}

I implemented an NBT reader to parse through the data in the file, as you can't get the value or name of tags with the minecraft classes, so that's what ITag and CompoundTag are. getType(tag) just returns a new NBTBase of the correct type (NBTTagByte, NBTTagInt, etc) with the value in the ITag.

 

The NBT file is read just fine, however the player's NBT remains the same after this, as displayed by their inventory remaining empty rather than filling up with the saved inventory. How can I make it update with the new data?

Link to comment
Share on other sites

Well, getEntityData doesn't seem to be saving the changes I made to it for some reason. I don't want to add new data to the player, but rather change the values of the fields that it already has, such as inventory, health, etc. Could I do this with IExtendedEntityProperties?

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.