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 [2024/08/27 02:37] (current) – [Block drops with data] solidblock | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | When you create a '' | + | When you create a block with block entity, you might want to place the block with predefined |
- | Before proceeding, you will need a [[tutorial:blocks|Block (with BlockItem)]] and a [[tutorial: | + | In this tutorial, we assume |
- | ===== Block Drops with data ===== | + | ===== Block drops with data ===== |
- | For a block to drop an '' | + | For a block to drop an '' |
+ | |||
+ | ==== For versions since 1.20.5 ==== | ||
+ | As data components have been introduced, you need to store the block entity data in the data components of the block item. This requires you to store nbt data as data components. See [[blockentity_modify_data# | ||
+ | |||
+ | **If you' | ||
+ | |||
+ | <code JavaScript src/ | ||
+ | { | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ], | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | ], | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | where: | ||
+ | * '' | ||
+ | |||
+ | ==== For versions before 1.20.5 ==== | ||
+ | Before version 1.20.5, data components were not introduced. Therefore, just copy nbt. | ||
<code JavaScript src/ | <code JavaScript src/ | ||
Line 41: | Line 83: | ||
</ | </ | ||
- | Where: | + | where: |
- | * //**source**// is the Tag key String | + | * '' |
- | * //**target**// is the Tag key hierarchy in the dropped '' | + | * '' |
+ | |||
+ | To save more fields, just add more replace operations (with source, target and op) to the '' | ||
+ | |||
+ | ===== Reading saved data in the tooltip ===== | ||
+ | |||
+ | For versions since 1.20.5, to get the BlockEntity' | ||
+ | |||
+ | **For versions since 1.20.5:** | ||
+ | <code java DemoBlock.class> | ||
+ | |||
+ | public class DemoBlock extends BlockWithEntity { | ||
+ | |||
+ | [...] | ||
+ | |||
+ | @Override | ||
+ | public void appendTooltip(ItemStack stack, Item.TooltipContext context, List< | ||
+ | final Integer i = stack.get(TutorialDataComponentTypes.NUMBER); | ||
+ | if (i == null) return; | ||
+ | |||
+ | tooltip.add(Text.literal(" | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | For versions before 1.20.5, we call '' | ||
+ | |||
+ | **For versions before 1.20.5:** | ||
+ | |||
+ | <code java DemoBlock.class> | ||
+ | public class DemoBlock extends BlockWithEntity { | ||
+ | |||
+ | [...] | ||
+ | |||
+ | @Override | ||
+ | public void appendTooltip(ItemStack stack, BlockView world, List< | ||
+ | NbtCompound nbt = BlockItem.getBlockEntityNbt(stack); | ||
+ | if (nbt == null) return; | ||
+ | |||
+ | tooltip.add(Text.literal(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Similarly, you can also modify the '' | ||
+ | <code java DemoBlockItem.class> | ||
+ | public class DemoBlockItem extends BlockItem { | ||
+ | public DemoBlockItem(Block block, Settings settings) { | ||
+ | super(block, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public Text getName(ItemStack stack) { | ||
+ | final MutableText name = Text.translatable(stack.getTranslationKey()); | ||
+ | if (stack.contains(TutorialDataComponentTypes.NUMBER)) { | ||
+ | name.append(" | ||
+ | } | ||
+ | return name; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Pick item with data ===== | ||
+ | In Creative Mode, when you pick item (by pressing the mouse wheel) while holding '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public ItemStack getPickStack(WorldView world, BlockPos pos, BlockState state) { | ||
+ | final ItemStack pickStack = super.getPickStack(world, | ||
+ | final BlockEntity blockEntity = world.getBlockEntity(pos); | ||
+ | if (blockEntity instanceof DemoBlockEntity demoBlockEntity) { | ||
+ | pickStack.applyComponentsFrom(demoBlockEntity.createComponentMap()); | ||
+ | } | ||
+ | return pickStack; | ||
+ | } | ||
+ | </code> | ||
- | To save more fields, just add more replace operations (with source, target | + | > **Note:** When '' |
- | ===== Reading saved data from ItemStack | + | ===== Helpful Reference |
+ | More examples can bee seen in vanilla codes, such as '' | ||
+ | * [[https:// | ||
+ | * [[datagen_loot|Data generation of loot tables]] | ||
tutorial/blockentity_sync_itemstack.1687086938.txt.gz · Last modified: 2023/06/18 11:15 by terra