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:10] – [The concept of tracking and why you only see the highlighted block] solidblock | zh_cn:tutorial:networking [2024/04/15 02:16] (current) – solidblock | ||
---|---|---|---|
Line 159: | Line 159: | ||
首先,将数据包发送到服务器是通过 '' | 首先,将数据包发送到服务器是通过 '' | ||
- | ===== 跟踪的概念以及为什么你只看到高亮的方块 ===== | + | ===== 追踪的概念以及为什么只有你看到高亮的方块 ===== |
现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 '' | 现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 '' | ||
- | |||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public TypedActionResult< | public TypedActionResult< | ||
- | // Verify we are processing the use action on the logical server | + | // 确认我们是否是在逻辑服务器上进行操作 |
if (world.isClient()) return super.use(world, | if (world.isClient()) return super.use(world, | ||
- | // Raycast and find the block the user is facing at | + | // 视线追踪并找到玩家朝向的方块 |
BlockPos target = ... | BlockPos target = ... | ||
PacketByteBuf buf = PacketByteBufs.create(); | PacketByteBuf buf = PacketByteBufs.create(); | ||
Line 184: | Line 183: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
public TypedActionResult< | public TypedActionResult< | ||
- | // Verify we are processing the use action on the logical server | + | // 确认我们是否是在逻辑服务器上进行操作 |
if (world.isClient()) return super.use(world, | if (world.isClient()) return super.use(world, | ||
- | // Raycast and find the block the user is facing at | + | // 视线追踪并找到玩家朝向的方块 |
BlockPos target = ... | BlockPos target = ... | ||
PacketByteBuf buf = PacketByteBufs.create(); | PacketByteBuf buf = PacketByteBufs.create(); | ||
buf.writeBlockPos(target); | buf.writeBlockPos(target); | ||
- | // Iterate over all players tracking a position in the world and send the packet to each player | + | // 迭代世界上所有追踪位置的玩家,并将数据包发送给每个玩家 |
for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) { | for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) { | ||
ServerPlayNetworking.send(player, | ServerPlayNetworking.send(player, | ||
Line 202: | Line 201: | ||
这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 | ||
- | ====== Advanced Networking topics ====== | ||
- | The Networking | + | ===== Networking |
+ | Since 1.20.5, the logic of networking is totally rewrited. In 1.20.5, you have to define a custom '' | ||
- | ^ 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:networking: | + | // 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 : PlayerLookup.tracking((ServerWorld) world, target)) { | ||
+ | ServerPlayNetworking.send(player, | ||
+ | } | ||
+ | |||
+ | return TypedActionResult.success(user.getHandStack(hand)); | ||
+ | } | ||
+ | </ |
zh_cn/tutorial/networking.1643353840.txt.gz · Last modified: 2022/01/28 07:10 by solidblock