User Tools

Site Tools


tutorial:blockentity_sync_itemstack

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tutorial:blockentity_sync_itemstack [2023/06/18 11:15] – created terratutorial:blockentity_sync_itemstack [2023/09/24 18:54] (current) – Use new minecraft wiki mattidragon
Line 5: Line 5:
 When you create a ''Block'' with ''BlockEntity'', you might want to place the block with predefined Nbt data from an ''ItemStack'' (of your ''BlockItem''), or save the ''BlockEntity'' data in the ''ItemStack'' after breaking the block. When you create a ''Block'' with ''BlockEntity'', you might want to place the block with predefined Nbt data from an ''ItemStack'' (of your ''BlockItem''), or save the ''BlockEntity'' data in the ''ItemStack'' after breaking the block.
  
-Before proceeding, you will need a [[tutorial:blocks|Block (with BlockItem)]] and a [[tutorial:blockentity|BlockEntity]].+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's data]] for testing purposes.
  
 ===== Block Drops with data ===== ===== Block Drops with data =====
Line 49: Line 49:
 ===== Reading saved data from ItemStack ===== ===== Reading saved data from ItemStack =====
  
 +To get the BlockEntity's **Nbt**, we can use the helper ''BlockItem.getBlockEntityNbt(stack)'' method, which just calls ''stack.getSubNbt(BLOCK_ENTITY_TAG_KEY)'' internally. (BLOCK_ENTITY_TAG_KEY being "BlockEntityTag", already mentioned in the //loot table//).
  
 +<code java>
 +public class DemoBlock extends Block implements BlockEntityProvider {
 + 
 +    [...]
 + 
 +    @Override
 +    public void appendTooltip(ItemStack stack, BlockView world, List<Text> tooltip, TooltipContext context) {
 +        NbtCompound nbt = BlockItem.getBlockEntityNbt(stack);
 +        if (nbt == null) return;
 +
 +        tooltip.add(Text.literal("Number: "+nbt.getInt("number"))
 +    }
 +}
 +</code>
 +
 +===== Changing ItemStack's BlockEntity data=====
 +
 +We can change our tooltip to always display some default value even if the stack does not have any BlockEntity data.
 +
 +<code java>
 +public class DemoBlock extends Block implements BlockEntityProvider {
 + 
 +    [...]
 + 
 +    @Override
 +    public void appendTooltip(ItemStack stack, BlockView world, List<Text> tooltip, TooltipContext context) {
 +        NbtCompound nbt = BlockItem.getBlockEntityNbt(stack);
 +        if (nbt == null){
 +            NbtCompound nbt = new NbtCompound();
 +            nbt.putInt("number", 0);
 +            
 +            BlockItem.setBlockEntityNbt(stack, ExampleMod.DEMO_BLOCK_ENTITY, nbt);
 +        }
 +
 +        tooltip.add(Text.literal("Number: "+nbt.getInt("number"))
 +    }
 +}
 +</code>
 +
 +===== Placing Block with data =====
 +
 +**Is automatic!** As long as the data is in the //"BlockEntityTag"// subNbt (e.g. by using the ''BlockItem.getBlockEntityNbt'' and ''BlockItem.setBlockEntityNbt'' helper methods) under tag keys recognized by the ''readNbt'' method of the ''BlockEntity''.
 +
 +===== Helpful Reference =====
 +
 +If something still isn't clear or you want more examples, I highly recommend looking at the Minecraft implementation of ''ShulkerBoxBlock'' and ''ShulkerBoxBlockEntity''. That for example also implements ''Nameable'' interface and has code to **save custom names** through ''ItemStack'' and ''BlockEntity''.
 +
 +==== Loot Tables ====
 +
 +
 +More info on Loot tables on [[https://minecraft.wiki/w/Loot_table|Minecraft Wiki]]. 
 +
 +Vanilla loot tables: .minecraft\versions\//[version]//\//[version]//.jar\data\minecraft\loot_tables\
 +
 +Also, this [[https://misode.github.io/loot-table/|Loot Table Generator]] might be useful.
  
tutorial/blockentity_sync_itemstack.1687086938.txt.gz · Last modified: 2023/06/18 11:15 by terra