Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author
1 hour ago, [NoOneButNo] said:

Take a look at the shulker box code. I believe it might help you.

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

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.

  • Author
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);
            }
        }
    }

}

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.