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 159: | Line 161: | ||
首先,将数据包发送到服务器是通过 '' | 首先,将数据包发送到服务器是通过 '' | ||
- | ===== 追踪的概念以及为什么你只看到高亮的方块 ===== | + | ===== 追踪的概念以及为什么只有你看到高亮的方块 ===== |
现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 '' | 现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 '' | ||
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.1643354016.txt.gz · Last modified: 2022/01/28 07:13 by solidblock