Jump to content

Recommended Posts

Posted (edited)

Here's a few problems I've ran into.

1. I have a piece of code which checks if a stack has a specific key every tick. However, the NBTTagCompoud#hasKey method seems to be functioning incorrectly. I've debugged it and it looks like it always returns false.

 

public void onUpdate(ItemStack stack, World world, Entity entity, int itemSlot, boolean isSelected)
{
	if(!world.isRemote)
	{
		NBTTagCompound nbt = stack.hasTagCompound() ? stack.getTagCompound() : new NBTTagCompound();	
		System.out.println(nbt.getUniqueId("Identifier"));
		if(!nbt.hasKey("Identifier"))
		{
			nbt.setUniqueId("Identifier", UUID.randomUUID());
			stack.setTagCompound(nbt);
		}
	}
} 

 

2. From what I've heard NBT is handled on the server side only. How would I add a string from an item's NBT to its lore? Trying to retrieve tags on the client side in Item#addInformation always returns null.

3. In what case would I want to use capabilities instead of item NBT?

Edited by Melonslise
Posted
8 hours ago, Melonslise said:

1. I have a piece of code which checks if a stack has a specific key every tick. However, the NBTTagCompoud#hasKey method seems to be functioning incorrectly. I've debugged it and it looks like it always returns false.

 

If you look at the implementation of NBTTagCompound#setUniqueId, you'll see that it doesn't use the key you give it directly; it uses two different keys based on it. This means that NBTTagCompound#hasKey will never return true for the key you passed to NBTTagCompound#setUniqueId, you need to use NBTTagCompound#hasUniqueId instead.

 

 

8 hours ago, Melonslise said:

2. From what I've heard NBT is handled on the server side only. How would I add a string from an item's NBT to its lore? Trying to retrieve tags on the client side in Item#addInformation always returns null.

 

ItemStack NBT is automatically synced to the client. If you're setting on the server but can't access it from Item#addInformation, you're doing something wrong. Post your code.

 

 

8 hours ago, Melonslise said:

3. In what case would I want to use capabilities instead of item NBT?

 

To create your own API or implement an existing one like IItemHandler/IFluidHandler; or to store data without having to serialise it to/deserialise it from NBT every time you want to access/modify it.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted (edited)

Thanks a lot! For some reason I automatically thought a UUID would be stored just like any other data type. Thanks for clearing things up, everything works now.

Edited by Melonslise

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.