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/04/15 02:16] (current) – solidblock | ||
---|---|---|---|
Line 201: | 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.1643354036.txt.gz · Last modified: 2022/01/28 07:13 by solidblock