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 [2023/09/24 18:54] – Use new minecraft wiki mattidragon | 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)]], a [[tutorial:blockentity|BlockEntity]] and a way to [[tutorial:blockentity_modify_data|modify the BlockEntity' | + | 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 //" | + | To save more fields, just add more replace operations (with source, target and op) to the '' |
- | ===== Reading saved data from ItemStack | + | ===== Reading saved data in the tooltip |
- | To get the BlockEntity' | + | For versions since 1.20.5, to get the BlockEntity' |
- | <code java> | + | **For versions since 1.20.5:** |
- | public class DemoBlock extends | + | <code java DemoBlock.class> |
+ | |||
+ | public class DemoBlock extends | ||
[...] | [...] | ||
- | + | | |
@Override | @Override | ||
- | public void appendTooltip(ItemStack stack, | + | public void appendTooltip(ItemStack stack, |
- | | + | |
- | if (nbt == null) return; | + | if (i == null) return; |
- | tooltip.add(Text.literal(" | + | tooltip.add(Text.literal(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ===== Changing ItemStack's BlockEntity data===== | + | For versions before 1.20.5, we call '' |
- | We can change our tooltip to always display some default value even if the stack does not have any BlockEntity data. | + | **For versions before 1.20.5:** |
- | <code java> | + | <code java DemoBlock.class> |
- | public class DemoBlock extends | + | public class DemoBlock extends |
[...] | [...] | ||
Line 78: | Line 122: | ||
public void appendTooltip(ItemStack stack, BlockView world, List< | public void appendTooltip(ItemStack stack, BlockView world, List< | ||
NbtCompound nbt = BlockItem.getBlockEntityNbt(stack); | NbtCompound nbt = BlockItem.getBlockEntityNbt(stack); | ||
- | if (nbt == null){ | + | if (nbt == null) return; |
- | NbtCompound nbt = new NbtCompound(); | + | |
- | nbt.putInt(" | + | |
- | + | ||
- | BlockItem.setBlockEntityNbt(stack, | + | |
- | } | + | |
- | tooltip.add(Text.literal(" | + | tooltip.add(Text.literal(" |
} | } | ||
} | } | ||
</ | </ | ||
- | ===== Placing Block with data ===== | + | Similarly, you can also modify the '' |
+ | <code java DemoBlockItem.class> | ||
+ | public class DemoBlockItem extends BlockItem { | ||
+ | public DemoBlockItem(Block block, Settings settings) { | ||
+ | super(block, | ||
+ | } | ||
- | **Is automatic!** As long as the data is in the //" | + | @Override |
+ | public Text getName(ItemStack stack) { | ||
+ | final MutableText name = Text.translatable(stack.getTranslationKey()); | ||
+ | if (stack.contains(TutorialDataComponentTypes.NUMBER)) { | ||
+ | name.append(" | ||
+ | } | ||
+ | return name; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
- | ===== Helpful Reference | + | ===== 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; | ||
+ | } | ||
+ | </ | ||
- | If something still isn't clear or you want more examples, I highly recommend looking at the Minecraft implementation of '' | + | > **Note:** When '' |
- | ==== Loot Tables | + | ===== Helpful Reference ===== |
- | + | ||
- | + | ||
- | More info on Loot tables on [[https:// | + | |
- | Vanilla loot tables: .minecraft\versions\// | + | More examples can bee seen in vanilla codes, such as '' |
- | Also, this [[https://misode.github.io/loot-table/ | + | * [[https://minecraft.wiki/w/Loot_table|Loot table page in Minecraft Wiki]] |
+ | * [[datagen_loot|Data generation of loot tables]] | ||
tutorial/blockentity_sync_itemstack.1695581655.txt.gz · Last modified: 2023/09/24 18:54 by mattidragon