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:27] – [Reading saved data from ItemStack] 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 129: | Line 129: | ||
</ | </ | ||
- | ===== Changing ItemStack's BlockEntity | + | Similarly, you can also modify the '' |
+ | <code java DemoBlockItem.class> | ||
+ | public class DemoBlockItem extends BlockItem { | ||
+ | public DemoBlockItem(Block block, Settings settings) { | ||
+ | super(block, | ||
+ | } | ||
- | We can change our tooltip to always display some default value even if the stack does not have any BlockEntity data. | + | @Override |
+ | public Text getName(ItemStack stack) { | ||
+ | final MutableText name = Text.translatable(stack.getTranslationKey()); | ||
+ | | ||
+ | name.append(" | ||
+ | } | ||
+ | return name; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ===== Pick item with data ===== | ||
+ | In Creative Mode, when you pick item (by pressing the mouse wheel) while holding '' | ||
<code java> | <code java> | ||
- | public class DemoBlock extends Block implements BlockEntityProvider { | + | |
- | + | public | |
- | [...] | + | final ItemStack pickStack |
- | + | final BlockEntity blockEntity | |
- | | + | |
- | public | + | |
- | | + | |
- | if (nbt == null){ | + | |
- | NbtCompound nbt = new NbtCompound(); | + | |
- | nbt.putInt(" | + | |
- | + | ||
- | BlockItem.setBlockEntityNbt(stack, ExampleMod.DEMO_BLOCK_ENTITY, | + | |
- | } | + | |
- | + | ||
- | | + | |
} | } | ||
- | } | + | return pickStack; |
+ | | ||
</ | </ | ||
- | ===== Placing Block with data ===== | + | > **Note:** When '' |
- | + | ||
- | **Is automatic!** As long as the data is in the //" | + | |
===== Helpful Reference ===== | ===== Helpful Reference ===== | ||
- | If something still isn't clear or you want more examples, | + | More examples |
- | + | ||
- | ==== Loot Tables ==== | + | |
- | + | ||
- | + | ||
- | More info on Loot tables on [[https:// | + | |
- | + | ||
- | Vanilla loot tables: .minecraft\versions\// | + | |
- | 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.1724639235.txt.gz · Last modified: 2024/08/26 02:27 by solidblock