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:13] – [追踪的概念以及为什么你只看到高亮的方块] 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: | ||
这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | ||
- | ====== Advanced Networking topics ====== | ||
- | The Networking system Fabric API supplies is very flexible and supports additional features other than just sending and receiving simple packets. As some of these more advanced topics are long, here are links to their specific pages: | + | ===== 1.20.5 中的网络通信 ===== |
+ | 自 1.20.5 开始,网络通信的逻辑被大改。在 1.20.5 中,你需要定义一个 '' | ||
- | ^ Networking Topic ^ Description ^ | + | <code java> |
- | | [[tutorial:networking: | + | public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload { |
- | | [[tutorial:networking:channel_events|Channel registration events]] | Events related to a server of client declaring the ability to receive a packet on a channel of a specific name | | + | public static final Id< |
- | | [[tutorial:networking:login|Login phase networking]]| Sending requests to a client during login; and allowing delay of login for a short amount of time | | + | |
- | | [[tutorial: | + | // 或者,你也可以这样写: |
+ | // 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 : PlayerLookup.tracking((ServerWorld) world, target)) { | ||
+ | ServerPlayNetworking.send(player, | ||
+ | } | ||
+ | |||
+ | return TypedActionResult.success(user.getHandStack(hand)); | ||
+ | } | ||
+ | </ |
zh_cn/tutorial/networking.1643354036.txt.gz · Last modified: 2022/01/28 07:13 by solidblock