Jump to content

[1.12.2] Updating and Syncing Tooltips


Sheker

Recommended Posts

@SideOnly(Side.CLIENT)
  public void addInformation(ItemStack stack, @Nullable World worldIn, java.util.List<String> tooltip, ITooltipFlag flagIn)
{
  super.addInformation(stack, worldIn, (java.util.List<String>) tooltip, flagIn);

  IItemHandler itemHandler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  for (int i = 0; i < itemHandler.getSlots() - 1; i++) {
    ItemStack displayedStack = itemHandler.getStackInSlot(i);
    if (!displayedStack.isEmpty() && displayedStack.getItem() != Item.getItemFromBlock(Blocks.AIR)) {
      tooltip.add(TextFormatting.GRAY + displayedStack.getDisplayName());
    }
  }
}

Hi everyone, I have an item that opens its contents on right-click. I've been trying to have a tooltip similar to how Shulker Boxes have a tooltip listing all its contents. It's close to how I want it to work on singleplayer, but the tooltip only updates when I right-click and open up the item GUI again. I also have the problem of not being able to see the tooltip on a server. I've looked around and the closest solution I've found is to override getNBTShareTag(), but I have no idea where to start and how it works as none of my data seems to be saved. Here is my code for my attempt to utilise getNBTShareTag to solve the problem:

@Override
	public NBTTagCompound getNBTShareTag(ItemStack stack)
	{
		IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
		NBTBase capTag = CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.getStorage().writeNBT(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, handler, null);
		NBTTagCompound tag = stack.getTagCompound();
		
		if (tag == null) {	
			tag = new NBTTagCompound();
		}
		
		if (capTag != null) {
			tag.setTag("GemInventory", capTag);
		}
		
		return tag;
}
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack stack, @Nullable World worldIn, java.util.List<String> tooltip, ITooltipFlag flagIn)
  {
  super.addInformation(stack, worldIn, (java.util.List<String>) tooltip, flagIn);


  if (!stack.hasTagCompound()) {
  stack.setTagCompound(new NBTTagCompound());
  }

  NBTTagCompound itemTagCompound = stack.getTagCompound();
  NBTTagList list = itemTagCompound.getTagList("GemInventory", 10);;

	for (int i = 0; i < list.tagCount(); i++)
    {
      ItemStack slotStack = new ItemStack(list.getCompoundTagAt(i));
      if (!slotStack.isEmpty())
      {
          tooltip.add("Test");
      }
	}

}

 

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.

Announcements



×
×
  • Create New...

Important Information

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