Jump to content

[SOLVED][1.7.10] ItemStack not saving NBT data


Visitor_404

Recommended Posts

After reading multiple tutorials about NBT in ItemStacks, I wonder, why NBT is not saved for me.

 

This is my code:

 

class itemMyItem extends Item
{
public void itemMyItem()
{
	setUnlocalizedName("MyItem");
	setMaxStacksize(1);
}

public ItemStack onItemRightClick(ItemStack var1, World var2, EntityPlayer var3)
{
	NBTTagCompound varData = new NBTTagCompound();
	varData.setString( "test", "TEST");

	if (var1.stackTagCompound == null)
	{
		var1.setTagCompound(varData);
		System.out.println("New NBT = " + var1.stackTagCompound.getString("test"));		
	}
	else
	{
		System.out.println("Old NBT = " + var1.stackTagCompound.getString("test"));
	}
        	return var1;
}
}

 

What I expect from this code:

First click on itemstack should result in text "New NBT = TEST".

Every click on the itemstack after the first click, should result in “Old NBT = TEST”, because the first click overwrote the default-nulled stackTagCompound to a newly initialized NBT with real values.

But instead I always get "New NBT = TEST", no matter how many times I click. By this, it is clear, that data is written to stackTagCompound, but not saved.

 

From the tutorials and documentations, I understand, that stackTagCompound is available in every itemstack by default, but set to “null”. Obviously, my code successfully writes to stackTagCompound and it is not “null” anymore until end of the current procedure. Why is it not saved?

In no tutorial is mentioned, that saving/loading stackTagCompound needs to be done manually. I presumed the stackTagCompound is handled by MC automatically.

 

I am sure, I miss something essential.

While thinking about it: Is this related to creative mode? I haven’t tried in survival mode and can’t do so at the moment...

 

Link to comment
Share on other sites

if (var1.stackTagCompound == null) { var1.setTagCompound(varData); //this will crash }

else { //do nothing }

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.

Link to comment
Share on other sites

@Draco: It actually won't.

 

oops, you're right.  The badly named variable names were making me see it as "var1.stackTagCompound == null; var1.stackTagCompound.setTag(...)"

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.

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.