zh_cn:tutorial:networking
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
zh_cn:tutorial:networking [2022/01/28 07:18] – [Advanced Networking topics] solidblock | zh_cn:tutorial:networking [2024/04/15 02:16] (current) – solidblock | ||
---|---|---|---|
Line 201: | Line 201: | ||
这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | ||
- | ====== 高级网络主题 ====== | ||
- | 网络系统 Fabric API 提供的功能非常灵活,并支持除了发送和接收简单数据包之外的其他功能。由于其中一些更高级的主题很长,以下是指向其特定页面的链接: | + | ===== Networking in 1.20.5 ===== |
+ | Since 1.20.5, the logic of networking is totally rewrited. In 1.20.5, you have to define a custom '' | ||
- | ^ 网络通信主题 ^ 描述 ^ | + | <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:tutorial:networking:login|Login phase networking]]| 在登录期间向客户端发送请求;并允许在短时间内延迟登录 | | + | |
- | | [[zh_cn: | + | // or you can also write like this: |
+ | // public static final PacketCodec< | ||
+ | |||
+ | @Override | ||
+ | public Id<? extends CustomPayload> | ||
+ | return ID; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | And then, register the receiver like this: | ||
+ | <code java> | ||
+ | PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, | ||
+ | ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, | ||
+ | context.client().execute(() -> { | ||
+ | ClientBlockHighlighting.highlightBlock(client, | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | Now, on the server side, you can send the packet to players like this: | ||
+ | <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.txt · Last modified: 2024/04/15 02:16 by solidblock