tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:blockentity [2019/05/27 19:09] – modmuss50 | 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() { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); |
- | | + | } |
} | } | ||
</ | </ | ||
- | Bellow | + | Below will show you how to create the '' |
- | You can simply add variables to this barebone class or implement interfaces such as '' | + | You can simply add variables to this barebone class or implement interfaces such as '' |
- | ===== Registring | + | ===== Registering |
- | Once you have created the '' | + | Once you have created the '' |
The '' | The '' | ||
Line 32: | Line 32: | ||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY, "modid:demo", | + | |
} | } | ||
</ | </ | ||
- | 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 47: | 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 value of the number | + | |
- | | + | private int number = 7; |
- | public DemoBlockEntity() { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); |
- | | + | } |
- | // Serialize the BlockEntity | + | |
- | | + | |
- | | + | |
- | + | // Save the current value of the number to the tag | |
- | | + | tag.putInt(" |
- | tag.putInt(" | + | |
- | + | | |
- | | + | } |
- | | + | |
} | } | ||
</ | </ | ||
- | In order to retrieve the data later, you will also need to override '' | + | In order to retrieve the data later, you will also need to override '' |
<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