tutorial:blockentity_sync_itemstack
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
tutorial:blockentity_sync_itemstack [2023/06/18 11:15] – created terra | tutorial:blockentity_sync_itemstack [2023/09/24 18:54] (current) – Use new minecraft wiki mattidragon | ||
---|---|---|---|
Line 5: | Line 5: | ||
When you create a '' | When you create a '' | ||
- | Before proceeding, you will need a [[tutorial: | + | Before proceeding, you will need a [[tutorial: |
===== Block Drops with data ===== | ===== Block Drops with data ===== | ||
Line 49: | Line 49: | ||
===== Reading saved data from ItemStack ===== | ===== Reading saved data from ItemStack ===== | ||
+ | To get the BlockEntity' | ||
+ | <code java> | ||
+ | public class DemoBlock extends Block implements BlockEntityProvider { | ||
+ | |||
+ | [...] | ||
+ | |||
+ | @Override | ||
+ | public void appendTooltip(ItemStack stack, BlockView world, List< | ||
+ | NbtCompound nbt = BlockItem.getBlockEntityNbt(stack); | ||
+ | if (nbt == null) return; | ||
+ | |||
+ | tooltip.add(Text.literal(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Changing ItemStack' | ||
+ | |||
+ | We can change our tooltip to always display some default value even if the stack does not have any BlockEntity data. | ||
+ | |||
+ | <code java> | ||
+ | public class DemoBlock extends Block implements BlockEntityProvider { | ||
+ | |||
+ | [...] | ||
+ | |||
+ | @Override | ||
+ | public void appendTooltip(ItemStack stack, BlockView world, List< | ||
+ | NbtCompound nbt = BlockItem.getBlockEntityNbt(stack); | ||
+ | if (nbt == null){ | ||
+ | NbtCompound nbt = new NbtCompound(); | ||
+ | nbt.putInt(" | ||
+ | | ||
+ | BlockItem.setBlockEntityNbt(stack, | ||
+ | } | ||
+ | |||
+ | tooltip.add(Text.literal(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Placing Block with data ===== | ||
+ | |||
+ | **Is automatic!** As long as the data is in the //" | ||
+ | |||
+ | ===== Helpful Reference ===== | ||
+ | |||
+ | If something still isn't clear or you want more examples, I highly recommend looking at the Minecraft implementation of '' | ||
+ | |||
+ | ==== Loot Tables ==== | ||
+ | |||
+ | |||
+ | More info on Loot tables on [[https:// | ||
+ | |||
+ | Vanilla loot tables: .minecraft\versions\// | ||
+ | |||
+ | Also, this [[https:// | ||
tutorial/blockentity_sync_itemstack.1687086938.txt.gz · Last modified: 2023/06/18 11:15 by terra