User Tools

Site Tools


zh_cn:tutorial:networking

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:networking [2022/01/28 07:13] – [追踪的概念以及为什么你只看到高亮的方块] solidblockzh_cn:tutorial:networking [2024/05/28 08:52] (current) solidblock
Line 1: Line 1:
 **注意:**本页已经取代旧版页面。建议使用本页描述的新的网络API。旧的页面参见[[tutorial:legacy:networking-v0|此处(英文)]]。 **注意:**本页已经取代旧版页面。建议使用本页描述的新的网络API。旧的页面参见[[tutorial:legacy:networking-v0|此处(英文)]]。
 +
 +对于 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 packetsAs some of these more advanced topics are long, here are links to their specific pages:+===== 1.20.5 中的网络通信 ===== 
 +自 1.20.5 开始,网络通信的逻辑被大改。在 1.20.5 中,你需要定义一个 ''Payload''。首先,定义一个包含了 ''BlockPos'' 的 ''Payload''
  
-^ Networking Topic ^ Description ^ +<code java> 
-| [[tutorial:networking:connection_events|Connection Network connection events]] | Events related to the the lifecycle of a connection to a client or server | +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<BlockHighlightPayload> ID = CustomPayload.id("tutorial:block_highlight"); 
-| [[tutorial:networking:login|Login phase networking]]| Sending requests to a client during loginand allowing delay of login for a short amount of time | +  public static final PacketCodec<PacketByteBuf, BlockHighlightPayload> CODEC = PacketCodec.tuple(BlockPos.PACKET_CODEC, BlockHighlightPayload::blockPos, BlockHighlightPayload::new)
-| [[tutorial:networking:dynamic_handlers|Dynamic registration of channel handlers]]| Allowing for a connection to receive a packet with a special handler |+  // 或者,你也可以这样写: 
 +  // public static final PacketCodec<PacketByteBuf, BlockHighlightPayload> CODEC = PacketCodec.of((value, buf) -> buf.writeBlockPos(value.blockPos), buf -> new BlockHighlightPayload(buf.readBlockPos()));
  
 +  @Override
 +  public Id<? extends CustomPayload> getId() {
 +    return ID;
 +  }
 +}
 +</code>
 +
 +然后,像这样注册 receiver:
 +<code java>
 +PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC);
 +ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, (payload, context) -> {
 +  context.client().execute(() -> {
 +    ClientBlockHighlighting.highlightBlock(client, target);
 +  });
 +});
 +</code>
 +
 +现在,在服务器一端,你可以像这样把数据包发送给玩家:
 +<code java>
 +    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
 +        if (world.isClient()) return super.use(world, user, hand);
 +
 +        // ...
 +
 +        for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) {
 +            ServerPlayNetworking.send(player, new BlockHighlightPayload(blockPos));
 +        }
 +
 +        return TypedActionResult.success(user.getHandStack(hand));
 +    }
 +</code>
zh_cn/tutorial/networking.1643354036.txt.gz · Last modified: 2022/01/28 07:13 by solidblock