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/04/15 02:16] (current) solidblock
Line 159: Line 159:
  
 首先,将数据包发送到服务器是通过 ''ClientPlayNetworking.send'' 完成的。在服务器接收数据包与在客户端接收数据包比较类似,使用 ''ServerPlayNetworking.registerGlobalReceiver(Identifier channelName, ChannelHandler channelHandler)'' 方法。用于服务器网络通信的 ''ChannelHandler'' 也会通过 ''player''参数传入发送该数据包的 ''ServerPlayerEntity''(玩家)。 首先,将数据包发送到服务器是通过 ''ClientPlayNetworking.send'' 完成的。在服务器接收数据包与在客户端接收数据包比较类似,使用 ''ServerPlayNetworking.registerGlobalReceiver(Identifier channelName, ChannelHandler channelHandler)'' 方法。用于服务器网络通信的 ''ChannelHandler'' 也会通过 ''player''参数传入发送该数据包的 ''ServerPlayerEntity''(玩家)。
-===== 追踪的概念以及为什么只看到高亮的方块 =====+===== 追踪的概念以及为什么只有你看到高亮的方块 =====
  
 现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 ''use'' 代码: 现在,高亮魔杖恰当地使用了网络,因此专用服务器不会崩溃。你邀请你的朋友回到服务器上来炫耀高亮魔杖,你使用魔杖,并且该方块也在你的客户端上高亮了,并且服务器没有崩溃。但是,你的朋友没有看到高亮。这是你在此处已有的代码有意为之的。为解决此问题,看一下物品的 ''use'' 代码:
Line 201: Line 201:
  
 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。 这样修改之后,当你使用魔杖时,你的朋友也应该在他们自己的客户端上看到高亮方块。
-====== 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 longhere are links to their specific pages:+===== Networking in 1.20.5 ===== 
 +Since 1.20.5, the logic of networking is totally rewrited. In 1.20.5you have to define a custom ''Payload''. First, define the payload that includes a ''BlockPos'':
  
-^ 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 |+  // or you can also write like this: 
 +  // 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>
 +
 +And then, register the receiver like this:
 +<code java>
 +PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC);
 +ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, (payload, context) -> {
 +  context.client().execute(() -> {
 +    ClientBlockHighlighting.highlightBlock(client, target);
 +  });
 +});
 +</code>
 +
 +Now, on the server side, you can send the packet to players like this:
 +<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.1643354016.txt.gz · Last modified: 2022/01/28 07:13 by solidblock