Posted December 12, 201410 yr 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...
December 12, 201410 yr 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.
December 12, 201410 yr @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.
December 12, 201410 yr 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.