Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

 

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.

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

  • Author

Thank you, both of you. The helper was helpful ;)

 

But it is heavily related to Creative Mode as well. While in Creative Mode, the itemstack will not save anything. As soon as I turned to Survival Mode, it worked!

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.