tutorial:blockentity_sync_itemstack
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:blockentity_sync_itemstack [2024/08/26 02:49] – removed [Changing ItemStack's BlockEntity data] because showing tooltips should not change data; improved [Helpful Reference] solidblock | tutorial:blockentity_sync_itemstack [2024/08/27 02:37] (current) – [Block drops with data] solidblock | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| When you create a block with block entity, you might want to place the block with predefined NBT data from an '' | When you create a block with block entity, you might want to place the block with predefined NBT data from an '' | ||
| - | In this tutorial, we assume you have created a [[block]] (and block item) and the [[blockentity|block entity]], and [[blockentity_modifying_data|stored data for it]]. | + | In this tutorial, we assume you have created a [[blocks|block]] (and block item) and the [[blockentity|block entity]], and [[blockentity_modify_data|stored data for it]]. |
| - | ===== 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 ==== | ==== For versions since 1.20.5 ==== | ||
| Line 128: | Line 128: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | 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; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | > **Note:** When '' | ||
| ===== Helpful Reference ===== | ===== Helpful Reference ===== | ||
tutorial/blockentity_sync_itemstack.1724640542.txt.gz · Last modified: 2024/08/26 02:49 by solidblock