zh_cn:tutorial:blockentity
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| zh_cn:tutorial:blockentity [2024/08/25 23:51] – 修复笔误 solidblock | zh_cn:tutorial:blockentity [2025/04/01 12:20] (current) – [方块实体刻] solidblock | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ===== 介绍 ===== | ===== 介绍 ===== | ||
| - | 方块实体主要用于在方块内存储数据。创建之前,您需要一个[[zh_cn: | + | **方块实体**主要用于在方块内存储数据。创建之前,您需要一个[[blocks|方块]]。本教程将介绍 BlockEntity 类的创建及其注册。 | 
| ===== 创建方块实体 ===== | ===== 创建方块实体 ===== | ||
| - | 最简单的方块实体仅继承 '' | + | 最简单的方块实体仅继承 '' | 
| - | <code java> | + | < | 
| public class DemoBlockEntity extends BlockEntity { | public class DemoBlockEntity extends BlockEntity { | ||
| public DemoBlockEntity(BlockPos pos, BlockState state) { | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
| - | super(ExampleMod.DEMO_BLOCK_ENTITY, pos, state); | + | super(TutorialBlockEntityTypes.DEMO_BLOCK, pos, state); | 
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | 请确保这个构造方法只接收这两个参数,否则我们后面写的方法引用 '' | + | 请确保这个构造方法只接收这两个参数,否则我们后面写的方法引用 '' | 
| - | + | ||
| - | 您可以简单地向此准系统类添加变量,或实现诸如 '' | + | |
| 方块实体支持一系列方法以支持一些功能,例如与 NBT 之间的序列化和反序列化、提供物品栏等。本教程提供方块实体功能的最常见一些实现。 | 方块实体支持一系列方法以支持一些功能,例如与 NBT 之间的序列化和反序列化、提供物品栏等。本教程提供方块实体功能的最常见一些实现。 | ||
| - | ===== 注册你的方块实体 ===== | + | ===== 注册方块和方块实体 ===== | 
| - | 一旦创建了 '' | + | 一旦创建了 '' | 
| - | '' | + | '' | 
| + | <code java TutorialBlocks.java> | ||
| + | public final class TutorialBlocks { | ||
| + | [...] | ||
| + |  | ||
| + | // 对于 1.21.2 之前的版本 | ||
| + | // public static final DemoBlock DEMO_BLOCK = register(" | ||
| + |  | ||
| + | // 对于 1.21.2 及之后的版本 | ||
| + | public static final DemoBlock DEMO_BLOCK = register(" | ||
| + |  | ||
| + | [...] | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code java TutorialBlockEntityTypes.java> | ||
| + | public class TutorialBlockEntityTypes { | ||
| + | public static <T extends BlockEntityType<?>> | ||
| + | return Registry.register(Registries.BLOCK_ENTITY_TYPE, | ||
| + | } | ||
| + | |||
| + | public static final BlockEntityType< | ||
| + | " | ||
| + |  | ||
| + | // 对于 1.21.2 之前的版本,请使用 BlockEntityType.Builder。 | ||
| + | FabricBlockEntityTypeBuilder.create(DemoBlockEntity:: | ||
| + | ); | ||
| + |  | ||
| + | public static void initialize() { | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | 记得要在 '' | ||
| <code java ExampleMod.java> | <code java ExampleMod.java> | ||
| - | public class ExampleMod | + | public class ExampleMod | 
| - |  | + |  | 
| - | Registries.BLOCK, | + | |
| - | Identifier.of(" | + | |
| - | new DemoBlock(AbstractBlock.Settings.create()) | + | |
| - | ); | + | |
| - | public static final BlockEntityType< | + | |
| - | Registries.BLOCK_ENTITY_TYPE, | + | |
| - | Identifier.of(" | + | |
| - | BlockEntityType.Builder.create(DemoBlockEntity:: | + | |
| - | ); | + | |
|  |  | ||
| @Override | @Override | ||
| public void onInitialize() { | public void onInitialize() { | ||
| [...] | [...] | ||
| + |  | ||
| + | TutorialBlockEntityTypes.initialize(); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | > :!: 在这个例子中,为了简化,我们只把方块和方块实体置于 '' | ||
| 对于旧版本,如果无法访问 '' | 对于旧版本,如果无法访问 '' | ||
| - | 这个方块实体类型定义了只有 '' | + | 这个方块实体类型定义了只有 '' | 
| + | |||
| + | > **注意:**和其他方块一样,这个方块也需要方块模型和物品模型,可能也需要战利品表,关于如何创建请参见 [[blocks]]。对于战利品表,有[[blockentity_sync_itemstac|后续教程]]会提到如何改进战利品表表以包含方块实体数据。 | ||
| ==== 将方块实体连接到方块 ==== | ==== 将方块实体连接到方块 ==== | ||
| Line 73: | Line 98: | ||
| return new DemoBlockEntity(pos, | return new DemoBlockEntity(pos, | ||
| } | } | ||
| - | } | ||
| - | </ | ||
| - | ===== 序列化数据 ===== | ||
| - | |||
| - | 如果您想在 '' | ||
| - | |||
| - | '' | ||
| - | |||
| - | 知道了这一点,下面的示例演示了如何将 '' | ||
| - | |||
| - | <code java> | ||
| - | public class DemoBlockEntity extends BlockEntity { | ||
| - | |||
| - | // 储存数字的当前值 | ||
| - | private int number = 7; | ||
| - | |||
| - | public DemoBlockEntity(BlockPos pos, BlockState state) { | ||
| - | super(ExampleMod.DEMO_BLOCK_ENTITY, | ||
| - | } | ||
| - | |||
| - | // 序列化方块实体 | ||
| @Override | @Override | ||
| - |  | + |  | 
| - |  | + |  | 
| - | nbt.putInt(" | + | |
| - | + | ||
| - | super.writeNbt(nbt, | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | 为了以后读取数据,您还需要覆盖 '' | + | 覆盖'' | 
| - | + | ||
| - | <code java> | + | |
| - | // 反序列化方块实体 | + | |
| - | @Override | + | |
| - | public void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapper) { | + | |
| - | super.readNbt(nbt, | + | |
| - | + | ||
| - | number = nbt.getInt(" | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | 一旦实现了 '' | + | |
| - | + | ||
| - | ===== 将服务器数据同步至客户端 ===== | + | |
| - | 数据通常是在服务器世界读取的。大多数数据都是客户端不需要知道的,例如客户端并不需要知道箱子和熔炉里面有什么,除非打开它。但对于某些方块实体,例如告示牌和旗帜,你需要将所有或者部分数据告知客户端,比如用于渲染。 | + | |
| - | + | ||
| - | 对于 1.17.1 及以下版本,请实现 Fabric API 中的 '' | + | |
| - | + | ||
| - | 对于 1.18 及以上版本,请覆盖 '' | + | |
| - | <code java> | + | |
| - | @Nullable | + | |
| - | @Override | + | |
| - | public Packet< | + | |
| - | return BlockEntityUpdateS2CPacket.create(this); | + | |
| - | } | + | |
| - | + | ||
| - | @Override | + | |
| - | public NbtCompound toInitialChunkDataNbt() { | + | |
| - | return createNbt(); | + | |
| - | } | + | |
| - | </ | + | |
| - | **警告**: 需要调用 '' | + | |
| ===== 方块实体刻 ===== | ===== 方块实体刻 ===== | ||
| Line 156: | Line 125: | ||
| 在你的 '' | 在你的 '' | ||
| <code java DemoBlockEntity.java> | <code java DemoBlockEntity.java> | ||
| - | public class DemoBlockEntity extends BlockEntity { | + | public class DemoBlockEntity extends BlockEntity | 
| [...] | [...] | ||
|  |  | ||
| - | public static void tick(World world, BlockPos pos, BlockState state, DemoBlockEntity | + |  | 
| + |  | ||
| [...] | [...] | ||
| } | } | ||
| Line 165: | Line 135: | ||
| </ | </ | ||
| - | ===== 概览 | + | ===== 下一步 | 
| + | |||
| + | 现在,您应该拥有自己的 '' | ||
| - | 现在,您应该拥有自己的 '' | + | 你也学习了如何为它添加课刻。下一步,可以尝试对方块实体进行一些复杂的操作,例如: | 
| + | * [[blockentity_modify_data|修改方块实体数据]] | ||
| + | * [[inventory|作为物品栏在方块实体存储物品]] | ||
| + | * [[blockentityrenderers|使用方块实体渲染器动态渲染]] | ||
| + | * [[screenhandler|创建容器方块]] | ||
zh_cn/tutorial/blockentity.1724629898.txt.gz · Last modified: 2024/08/25 23:51 by solidblock
                
                