zh_cn:tutorial:blockentity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
zh_cn:tutorial:blockentity [2019/12/19 05:19] – [序列化数据] lightcolour | zh_cn:tutorial:blockentity [2022/12/16 02:14] – [注册你的方块实体] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 添加一个方块实体 ====== | + | ====== 添加方块实体 ====== |
===== 介绍 ===== | ===== 介绍 ===== | ||
- | + | 方块实体主要用于在方块内存储数据。创建之前,您需要一个[[zh_cn:tutorial: | |
- | BlockEntity主要用于在块内存储数据。 | + | |
===== 创建一个方块实体 ===== | ===== 创建一个方块实体 ===== | ||
- | 最简单的块实体仅扩展'' | + | 最简单的方块实体仅继承 |
<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将向您展示如何创建'' | + | 请确保这个构造方法只接收这两个参数,否则我们后面写的方法引用 '' |
- | + | ||
- | 您可以简单地向此准系统类添加变量,或实现诸如'' | + | 您可以简单地向此准系统类添加变量,或实现诸如 '' |
===== 注册你的方块实体 ===== | ===== 注册你的方块实体 ===== | ||
- | 一旦创建了'' | + | 一旦创建了 '' |
- | '' | + | '' |
<code java> | <code java> | ||
- | public static BlockEntityType< | + | |
- | + | Registries.BLOCK_ENTITY_TYPE, | |
- | @Override | + | new Identifier("tutorial", |
- | public void onInitialize() { | + | FabricBlockEntityTypeBuilder.create(DemoBlockEntity:: |
- | DEMO_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY, "modid:demo", | + | |
- | } | + | |
</ | </ | ||
- | 如上所示,一旦创建并注册了'' | + | 这个方块实体类型定义了只有 '' |
+ | |||
+ | ==== 将方块实体连接到方块 ==== | ||
+ | |||
+ | 一旦创建并注册了 '' | ||
<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: | ||
===== 序列化数据 ===== | ===== 序列化数据 ===== | ||
- | 如果要将任何数据存储在'' | + | 如果要将任何数据存储在 '' |
- | '' | + | '' |
+ | |||
+ | 知道了这一点,下面的示例演示了如何将 '' | ||
<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 | |
- | | + | nbt.putInt(" |
- | tag.putInt(" | + | |
- | + | | |
- | | + | } |
- | | + | |
} | } | ||
</ | </ | ||
- | 为了以后检索数据,您还需要覆盖'' | + | 为了以后读取数据,您还需要覆盖 '' |
<code java> | <code java> | ||
- | // Deserialize the BlockEntity | + | // 反序列化方块实体 |
- | public void fromTag(CompoundTag tag) { | + | @Override |
- | | + | public void readNbt(NbtCompound nbt) { |
- | | + | super.readNbt(nbt); |
+ | |||
+ | | ||
} | } | ||
</ | </ | ||
- | 一旦实现了'' | + | 一旦实现了 '' |
+ | |||
+ | ===== 将服务器数据同步至客户端 ===== | ||
+ | 数据通常是在服务器世界读取的。大多数数据都是客户端不需要知道的,例如客户端并不需要知道箱子和熔炉里面有什么,除非打开它。但对于某些方块实体,例如告示牌和旗帜,你需要将所有或者部分数据告知客户端,比如用于渲染。 | ||
+ | |||
+ | 对于 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