Jump to content

[1.9.4] Custom Player NBT Tag not working currectly


arie2002

Recommended Posts

Hello!

I am working on a mod where custom data is attached to the player.

Currently, this is my code:

public static boolean isResearched(String tech, EntityPlayer player) {
NBTTagCompound tag = player.getEntityData();
NBTTagList list = tag.getTagList("FTGU", NBT.TAG_STRING);
for (int i = 0; i < list.tagCount(); i++) {
	String name = list.getStringTagAt(i);
	if (name == tech) {
		return true;
	}
}
return false;
}

public static void putResearched(String tech, EntityPlayer player) {
NBTTagCompound tag = player.getEntityData();
if (!tag.hasKey("FTGU"))
	tag.setTag("FTGU", new NBTTagList());

NBTTagList list = tag.getTagList("FTGU", NBT.TAG_STRING);
list.appendTag(new NBTTagString(tech));
}

Basically, I am adding a new String List tag to the player NBT Compound. This code is run by the client and the server, so they should be synced up...

...but they aren't

 

Right after the code is executed by both sides, I used the isResearched function and both sides said that the specific research was researched.

However, later on I tested it again, and now only the server says it has been researched.

Then I saved the world and reloaded it and now suddenly both sides say it hasn't been researched!

 

I have never edited entity NBT before so I may be doing something entirely wrong

I never post on the forums, but when I do, I have no clue how to do a certain thing.

Link to comment
Share on other sites

I don't think client even allows storing stuff in its NBT (for long) - meaning only server should have NBT to load/save, normal data (alredy loaded) should be stored as objects.

 

And yeah - you are doing everything wrong.

 

1. You are not supposed to access entity's NBT directly (unless its yours).

2. Use @Capability to assign data to player (on both sides) - perform change (research) on server only and then send packet to client.

Yes - if you want you could perform changes on both sides, BUT - there is no NBT involved here. Only server shouold even have NBT.

 

http://mcforge.readthedocs.io/en/latest/datastorage/capabilities/

https://github.com/MinecraftForge/MinecraftForge/blob/1.9/src/test/java/net/minecraftforge/test/NoBedSleepingTest.java

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

2. Use @Capability to assign data to player (on both sides) - perform change (research) on server only and then send packet to client.

Thanks for the quick reply! I looked through the documentation, and I don't really understand how and what capabilities are... Reading through it it almost seems capabilities are just temporary information, because the data is lost when the player dies or something. It might just be that I don't know many java/minecraft terms, but the documentation is just a bunch of gibberish to me

I never post on the forums, but when I do, I have no clue how to do a certain thing.

Link to comment
Share on other sites

In simple terms:

Capabilities allow you to assign instance of some (your) interface (which you register as capability) to given entity (or other things like ItemStacks, but that's not the case).

You can think of it as mapping <Interface->Object> inside entity. That object can hold more objects (whatever you want) and can be also serialized into entity's NBT as call it "storage" of capability.

 

NBT should be used only (well, generally) to store data to HDD. What you are after is basically:

1. Assign capability to server and client player instance.

2. Store data you want (as normal fields) in that capability.

3. Edit them (can be done one both sides) to your needs.

4. When time comes (saving) your capability can be serialized to NBT data and will be saved into player's NBT.

5. When player is loaded - given capability will attempt to load itself from that NBT. If something was saved for it - it will deserialize it into fields stored in its (capability's) instance.

 

Notes: (if you didn't know)

* for each entity there are 2 objects (client, server) - same can be said about capability, which again - is just an object of your data assigned to given entity's instance.

* You can perform any task on capabilities on any side.

* If you want syncing - you basically pull data from server entity's capability and send it to client to set client entity's capability data.

* As to how it all works... some dynamic java stuff, you don't even need to know that xD

 

Any questions?

1.7.10 is no longer supported by forge, you are on your own.

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.