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:04] – [Sending packets to the server and receiving packets on the server] solidblock | zh_cn:tutorial:networking [2024/04/15 02:16] (current) – solidblock | ||
---|---|---|---|
Line 159: | Line 159: | ||
首先,将数据包发送到服务器是通过 '' | 首先,将数据包发送到服务器是通过 '' | ||
- | ===== The concept of tracking and why you only see the highlighted block ===== | + | ===== 追踪的概念以及为什么只有你看到高亮的方块 |
- | + | ||
- | Now that the highlighting wand properly uses networking so the dedicated server does not crash, you invite your friend back on the server to show off the highlighting wand. You use the wand and the block is highlighted on your client and the server does not crash. However, your friend does not see the highlighted block. This is intentional with the code that you already have here. To solve this issue let us take a look at the item's use code: | + | |
+ | 现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 '' | ||
<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 178: | Line 177: | ||
</ | </ | ||
- | You may notice the item will only send the packet to the player who used the item. To fix this, we can use the utility methods in '' | + | 你可能注意到了,该物品只会将数据包发送给使用了该物品的玩家。为解决此问题,我们可以使用 |
- | Since we know where the highlight will occur, we can use '' | + | 因为我们知道高亮会出现在哪个位置,所以我们可以使用 |
<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 201: | Line 200: | ||
</ | </ | ||
- | After this change, when you use the wand, your friend should also see the highlighted block on their own client. | + | 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 |
- | ====== Advanced | + | ===== Networking |
+ | Since 1.20.5, the logic of networking is totally rewrited. In 1.20.5, you have to define a custom '' | ||
- | 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: | + | <code java> |
+ | public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload { | ||
+ | public static final Id< | ||
+ | public static final PacketCodec< | ||
+ | // or you can also write like this: | ||
+ | // public static final PacketCodec< | ||
- | ^ Networking Topic ^ Description ^ | + | @Override |
- | | [[tutorial: | + | |
- | | [[tutorial: | + | |
- | | [[tutorial: | + | } |
- | | [[tutorial: | + | } |
+ | </ | ||
+ | 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.1643353465.txt.gz · Last modified: 2022/01/28 07:04 by solidblock