zh_cn:tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:networking [2022/01/28 07:18] – [Advanced Networking topics] solidblock | zh_cn:tutorial:networking [2024/05/28 08:52] (current) – solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
**注意:**本页已经取代旧版页面。建议使用本页描述的新的网络API。旧的页面参见[[tutorial: | **注意:**本页已经取代旧版页面。建议使用本页描述的新的网络API。旧的页面参见[[tutorial: | ||
+ | |||
+ | 对于 1.20.5 引入的新的网络通信 API,请参见 [[#1.20.5 中的网络通信]]。 | ||
====== 网络通信 ====== | ====== 网络通信 ====== | ||
Line 201: | Line 203: | ||
这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | ||
- | ====== 高级网络主题 ====== | ||
- | 网络系统 Fabric API 提供的功能非常灵活,并支持除了发送和接收简单数据包之外的其他功能。由于其中一些更高级的主题很长,以下是指向其特定页面的链接: | + | ===== 1.20.5 中的网络通信 ===== |
+ | 自 1.20.5 开始,网络通信的逻辑被大改。在 1.20.5 | ||
- | ^ 网络通信主题 ^ 描述 ^ | + | <code java> |
- | | [[zh_cn:tutorial:networking: | + | public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload { |
- | | [[zh_cn:tutorial:networking:channel_events|Channel registration events]] | 与客户端或服务器相关的事件,声明在特定名称的通道上接收数据包的能力 | | + | public static final Id< |
- | | [[zh_cn: | + | |
- | | [[zh_cn: | + | // 或者,你也可以这样写: |
+ | // public static final PacketCodec< | ||
+ | |||
+ | @Override | ||
+ | public Id<? extends CustomPayload> | ||
+ | return ID; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 然后,像这样注册 receiver: | ||
+ | <code java> | ||
+ | PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, | ||
+ | ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, | ||
+ | context.client().execute(() -> { | ||
+ | ClientBlockHighlighting.highlightBlock(client, | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | 现在,在服务器一端,你可以像这样把数据包发送给玩家: | ||
+ | <code java> | ||
+ | public TypedActionResult< | ||
+ | if (world.isClient()) return super.use(world, | ||
+ | |||
+ | // ... | ||
+ | |||
+ | for (ServerPlayerEntity player | ||
+ | ServerPlayNetworking.send(player, | ||
+ | } | ||
+ | |||
+ | return TypedActionResult.success(user.getHandStack(hand)); | ||
+ | } | ||
+ | </ |
zh_cn/tutorial/networking.1643354288.txt.gz · Last modified: 2022/01/28 07:18 by solidblock