tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockentity [2019/05/21 11:16] – created falseresync | tutorial:blockentity [2022/05/26 09:21] – Add a prompt to actively trigger BlockEntity synchronization moheng | ||
---|---|---|---|
Line 11: | Line 11: | ||
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | public DemoBlockEntity(BlockEntityType<?> | + | |
- | super(type); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, |
- | | + | } |
} | } | ||
</ | </ | ||
- | You can simply add variables | + | Below will show you how to create the '' |
- | ===== Registring | + | You can simply add variables to this barebone class or implement interfaces such as '' |
- | Once you have created the '' | + | ===== Registering your BlockEntity ===== |
+ | |||
+ | Once you have created the '' | ||
+ | |||
+ | The '' | ||
<code java> | <code java> | ||
- | public static BlockEntityType< | + | public static BlockEntityType< |
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | | ||
+ | } | ||
</ | </ | ||
- | Once your '' | + | ==== Connecting a Block Entity to a Block ==== |
+ | |||
+ | Once your '' | ||
<code java> | <code java> | ||
- | @Override | + | public class DemoBlock extends Block implements BlockEntityProvider { |
- | public BlockEntity createBlockEntity(BlockView blockView) { | + | |
- | | + | [...] |
+ | |||
+ | | ||
+ | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { | ||
+ | return new DemoBlockEntity(pos, state); | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 38: | Line 54: | ||
===== Serializing Data ===== | ===== Serializing Data ===== | ||
- | If you want to store any data in your '' | + | If you want to store any data in your '' |
- | '' | + | '' |
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | // Store the current | + | |
- | | + | private int number = 7; |
- | public DemoBlockEntity(BlockEntityType< | + | |
- | super(type); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, |
- | | + | } |
- | // Serialize the BlockEntity | + | |
- | | + | |
- | | + | |
- | + | // Save the current value of the number to the tag | |
- | | + | tag.putInt(" |
- | tag.putInt(" | + | |
- | + | | |
- | | + | } |
- | | + | |
} | } | ||
</ | </ | ||
- | In order to retrive | + | In order to retrieve |
<code java> | <code java> | ||
// Deserialize the BlockEntity | // Deserialize the BlockEntity | ||
- | public void fromTag(CompoundTag | + | @Override |
- | | + | public void readNbt(NbtCompound |
- | | + | super.readNbt(tag); |
+ | |||
+ | | ||
} | } | ||
</ | </ | ||
- | Once you have implemented the '' | + | Once you have implemented the '' |
- | If you need to sync some of your '' | + | ===== Sync data from server to client ===== |
+ | The data is read in the server world usually. Sometimes | ||
+ | |||
+ | For version 1.17.1 and below, | ||
+ | |||
+ | For version 1.18 and above, override '' | ||
+ | <code java> | ||
+ | @Nullable | ||
+ | @Override | ||
+ | public Packet< | ||
+ | return BlockEntityUpdateS2CPacket.create(this); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public NbtCompound toInitialChunkDataNbt() { | ||
+ | return createNbt(); | ||
+ | } | ||
+ | </ | ||
+ | **Warning**: | ||
+ | |||
+ | ===== Block Entity Ticking ===== | ||
+ | 1.17 has added static ticking, where before you'd implement the '' | ||
+ | |||
+ | |||
+ | In your '' | ||
+ | <code java> | ||
+ | public class DemoBlock extends BlockWithEntity { | ||
+ | [...] | ||
+ | @Override | ||
+ | public BlockRenderType getRenderType(BlockState state) { | ||
+ | // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! | ||
+ | return BlockRenderType.MODEL; | ||
+ | } | ||
+ | @Override | ||
+ | public <T extends BlockEntity> | ||
+ | return checkType(type, | ||
+ | } | ||
+ | </ | ||
+ | And in your '' | ||
+ | <code java> | ||
+ | public class DemoBlockEntity extends BlockEntity { | ||
+ | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
+ | super(ExampleMod.DEMO_BLOCK_ENTITY, | ||
+ | } | ||
+ | public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity be) { | ||
+ | [...] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
===== Overview ===== | ===== Overview ===== | ||
- | You should now have your very own '' | + | You should now have your very own '' |
tutorial/blockentity.txt · Last modified: 2024/08/23 17:30 by psjahn