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:21] – [For versions before 1.20.5] fix typo 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 89: | Line 89: | ||
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 120: | 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.1724638899.txt.gz · Last modified: 2024/08/26 02:21 by solidblock