Jump to content

Help with tooltip and NBT 1.14


AlexVR

Recommended Posts

I have an block that stores its content when broken via the loot table functions, like this:
 

Spoiler

{
  "type": "minecraft:block",
  "pools": [
    {
      "name":"pool1",
      "rolls": 1,
      "entries": [
        {
          "type": "minecraft:item",
          "functions": [
            {
              "function": "minecraft:copy_name",
              "source": "block_entity"
            },
            {
              "function": "minecraft:copy_nbt",
              "source": "block_entity",
              "ops": [
                {
                  "source": "inv",
                  "target": "BlockEntityTag.inv",
                  "op": "replace"
                }
              ]
            },
            {
              "function": "minecraft:set_contents",
              "entries": [
                {
                  "type": "minecraft:dynamic",
                  "name": "minecraft:contents"
                }
              ]
            }
          ],
          "name": "bedres:ritual_pedestal"
        }
      ],
      "conditions": [
        {
          "condition": "minecraft:survives_explosion"
        }
      ]
    }
  ]
}


im not sure how to acces that information from the ItemStack#addInformation method, at the moment i have this in my tile, which all works, breaking and placing stays with the same item and variable values:

Spoiler

@Override
public void read(CompoundNBT tag) {
    CompoundNBT compound = tag.getCompound("inv");
    handler.ifPresent(h -> {
        ((INBTSerializable<CompoundNBT>) h).deserializeNBT(compound);
    });
    item = compound.getString("name");
    super.read(tag);
}

@Override
public CompoundNBT write(CompoundNBT tag) {
    handler.ifPresent(h ->{
        CompoundNBT compound = ((INBTSerializable<CompoundNBT>)h).serializeNBT();
        tag.put("inv",compound);

        compound.put("items", ((INBTSerializable<CompoundNBT>) h).serializeNBT());
        compound.putString("name",item);

    });

    return super.write(tag);
}



and on the add info i have this, which i knew it wouldn't work, but im not sure how to approach it:

Spoiler

@Override
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {

    CompoundNBT compoundNBT = stack.getTag();
    if(compoundNBT != null) {
        String s = compoundNBT.getString("name");
        tooltip.add(new StringTextComponent(TextFormatting.DARK_GRAY + ("Item: " +s)));
    }
    super.addInformation(stack, worldIn, tooltip, flagIn);
}

 

Edited by AlexVR
Link to comment
Share on other sites

2 minutes ago, AlexVR said:

But the shulker box doesn't tell you it's content on the tool tip, does it? 

It does, look at ShulkerBoxBlock#addInformation, which itself gets called inside of BlockItem to add the block's tooltips to its item.

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

Link to comment
Share on other sites

12 minutes ago, imacatlolol said:

It does, look at ShulkerBoxBlock#addInformation, which itself gets called inside of BlockItem to add the block's tooltips to its item.

Just checked, it does, thanks so much!

Will post code for future searchers:

 

Spoiler

@OnlyIn(Dist.CLIENT)
public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
    super.addInformation(stack, worldIn, tooltip, flagIn);
    CompoundNBT compoundnbt = stack.getChildTag("BlockEntityTag");
    if (compoundnbt != null) {
        if (compoundnbt.contains("LootTable", 8)) {
            tooltip.add(new StringTextComponent("???????"));
        }
        if (compoundnbt.contains("Items", 9)) {
            NonNullList<ItemStack> nonnulllist = NonNullList.withSize(1, ItemStack.EMPTY);
            ItemStackHelper.loadAllItems(compoundnbt, nonnulllist);
            if (!nonnulllist.get(0).isEmpty()) {
                ITextComponent itextcomponent = nonnulllist.get(0).getDisplayName().deepCopy();
                itextcomponent.appendText(" x").appendText(String.valueOf(nonnulllist.get(0).getCount()));
                tooltip.add(itextcomponent);
            }
        }
    }

}

 

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.