zh_cn: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 | ||
zh_cn:tutorial:blockentity [2021/05/18 05:05] – [介绍] solidblock | zh_cn:tutorial:blockentity [2022/05/26 09:24] – Add a prompt to actively trigger BlockEntity synchronization moheng | ||
---|---|---|---|
Line 7: | Line 7: | ||
===== 创建一个方块实体 ===== | ===== 创建一个方块实体 ===== | ||
- | 最简单的方块实体仅扩展'' | + | 最简单的方块实体仅继承'' |
<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); |
- | | + | } |
} | } | ||
</ | </ | ||
Line 23: | Line 23: | ||
===== 注册你的方块实体 ===== | ===== 注册你的方块实体 ===== | ||
- | 一旦创建了'' | + | 一旦创建了'' |
'' | '' | ||
Line 29: | Line 29: | ||
<code java> | <code java> | ||
public static BlockEntityType< | public static BlockEntityType< | ||
+ | |||
@Override | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, | + | |
} | } | ||
</ | </ | ||
Line 41: | Line 41: | ||
<code java> | <code java> | ||
- | public class MyBlock | + | public class DemoBlock |
- | [...] | + | |
- | @Override | + | |
- | | + | public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { |
- | return new DemoBlockEntity(); | + | return new DemoBlockEntity(pos, state); |
- | | + | } |
} | } | ||
</ | </ | ||
Line 54: | Line 54: | ||
===== 序列化数据 ===== | ===== 序列化数据 ===== | ||
- | 如果要将任何数据存储在'' | + | 如果要将任何数据存储在 '' |
- | '' | + | '' |
<code java> | <code java> | ||
public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
- | // 储存数字的当前值 | + | |
- | | + | private int number = 7; |
- | public DemoBlockEntity() { | + | |
- | super(ExampleMod.DEMO_BLOCK_ENTITY); | + | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); |
- | | + | } |
- | // 序列化方块实体 | + | |
- | | + | @Override |
- | | + | public |
- | | + | |
- | + | ||
- | // Save the current value of the number to the tag | + | // Save the current value of the number to the tag |
- | tag.putInt(" | + | tag.putInt(" |
- | + | } | |
- | return tag; | + | |
- | } | + | |
} | } | ||
</ | </ | ||
- | 为了以后检索数据,您还需要覆盖'' | + | 为了以后检索数据,您还需要覆盖 '' |
<code java> | <code java> | ||
// 取消序列化方块实体 | // 取消序列化方块实体 | ||
@Override | @Override | ||
- | public void fromTag(BlockState state, CompoundTag | + | public void readNbt(NbtCompound |
- | | + | super.readNbt(tag); |
- | | + | number = tag.getInt(" |
} | } | ||
</ | </ | ||
- | 一旦实现了'' | + | 一旦实现了 '' |
+ | |||
+ | ===== 将服务器数据同步至客户端 ===== | ||
+ | 数据通常是在服务器世界读取的。有时候你需要将所有或者部分数据同步到客户端,比如用于渲染。 | ||
+ | |||
+ | 对于 1.17.1 及以下版本,请实现 Fabric API 中的'' | ||
+ | |||
+ | 对于 1.18 及以上版本,请覆盖 '' | ||
+ | <code java> | ||
+ | @Nullable | ||
+ | @Override | ||
+ | public Packet< | ||
+ | return BlockEntityUpdateS2CPacket.create(this); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public NbtCompound toInitialChunkDataNbt() { | ||
+ | return createNbt(); | ||
+ | } | ||
+ | </ | ||
+ | **警告**: 需要调用'' | ||
+ | ===== 方块实体刻 ===== | ||
+ | 1.17 添加了静态的刻,然后你就可以实现 '' | ||
+ | |||
+ | 在你的 '' | ||
+ | <code java> | ||
+ | public class DemoBlock extends BlockWithEntity { | ||
+ | [...] | ||
+ | @Override | ||
+ | public BlockRenderType getRenderType(BlockState state) { | ||
+ | // 由于继承了BlockWithEntity,这个默认为INVISIBLE,所以我们需要更改它! | ||
+ | return BlockRenderType.MODEL; | ||
+ | } | ||
+ | @Override | ||
+ | public <T extends BlockEntity> | ||
+ | return checkType(type, | ||
+ | } | ||
+ | </ | ||
+ | 在你的 '' | ||
+ | <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 | + | |
- | 现在,您应该拥有自己的'' | + | 现在,您应该拥有自己的 '' |
zh_cn/tutorial/blockentity.txt · Last modified: 2023/09/03 01:37 by wjz_p