July 10, 201312 yr Any method where you have an ItemStack parameter, you can assign data to it/get data from it using stack.getTagCompound().
July 10, 201312 yr Author That's rather convenient, int length = stack.getTagCompound().getInteger("Length"); However, yields a null pointer exception. I assume I would need to set the integer first, but where would I do that?
July 10, 201312 yr Author Right, but where would I throw that? Would I need to extend ItemStack and put it in the constructor?
July 10, 201312 yr ItemStack can't be extended, it is final. However, getting an integer shouldn't throw NPE as it will always either return a value if it has one or 0 if it does not. Look at the getInteger(String) method in NBTTagCompound and you'll see that it always returns something. The NPE is coming from one of two things (most likely the second) - The stack is null or it's tag compound is null. ItemStack tag compounds are protected against NPE, so first do this: NBTTagCompound nbt = stack.getTagCompound(); if(nbt == null) { nbt = new NBTTagCompound(); stack.setTagCompound(nbt); } A specific example of what you're trying to do with the NBT would be helpful, that way I can tell you where to set the int value for retrieval later.
July 10, 201312 yr Author A specific example of what you're trying to do with the NBT would be helpful, that way I can tell you where to set the int value for retrieval later. Not needed, your example works perfectly, thank you.
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.