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/06/29 23:49] – spelling fixes draylar | tutorial:blockentity [2022/08/08 04:27] – [Serializing Data] avoid ambiguity in translation solidblock | ||
---|---|---|---|
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 will show you how to create | + | Please ensure that the constructor only takes the two parameters, otherwise the method reference '' |
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 '' |
<code java> | <code java> | ||
- | public static BlockEntityType< | + | |
- | + | | |
- | @Override | + | new Identifier("tutorial", |
- | public void onInitialize() { | + | FabricBlockEntityTypeBuilder.create(DemoBlockEntity:: |
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY, "modid:demo", | + | |
- | } | + | |
</ | </ | ||
- | Once your '' | + | The block entity type defines that only the '' |
+ | |||
+ | ==== 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 55: | ||
===== Serializing Data ===== | ===== Serializing Data ===== | ||
- | If you want to store any data in your '' | + | If you want to store any data in your '' |
- | '' | + | '' |
+ | |||
+ | Knowing this, the example below demonstrates saving an integer from 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 nbt | |
- | | + | nbt.putInt(" |
- | tag.putInt(" | + | |
- | + | | |
- | | + | } |
- | | + | |
} | } | ||
</ | </ | ||
- | In order to retrieve | + | In order to read the data, you will also need to override '' |
<code java> | <code java> | ||
// Deserialize the BlockEntity | // Deserialize the BlockEntity | ||
- | public void fromTag(CompoundTag tag) { | + | @Override |
- | | + | public void readNbt(NbtCompound nbt) { |
- | | + | super.readNbt(nbt); |
+ | |||
+ | | ||
} | } | ||
</ | </ | ||
- | Once you have implemented the '' | + | Once you have implemented the '' |
- | If you need to sync some of your '' | + | ===== Sync data from server |
+ | The data is read in the server world usually. Most data are not needed by the client, for example, your client does not need to know what's in the chest or furnace, until you open the GUI. But for some block entities, | ||
+ | |||
+ | For version 1.17.1 and below, implement | ||
+ | |||
+ | 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/26 08:11 by solidblock