User Tools

Site Tools


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
tutorial:networking [2024/05/04 19:51] – Call out the 1.20.5 networking section at the top so people can know to skip to it bluemeanialtutorial:networking [2024/10/26 20:39] (current) – [How do I fix the crash?] double "be" fix skycatminepokie
Line 58: Line 58:
 | Singleplayer (or LAN host)    | Yes -> Full access          | | Singleplayer (or LAN host)    | Yes -> Full access          |
  
-It may seem complicated to have communication with the server in three different ways. However, you don't need to communicate in three different ways with the game client. Since all three connection types communicate with the game client using packets, you only need to communicate with the game client like you are always running on a dedicated server. Connection to a server over LAN or Singleplayer can be also be treated like the server is a remote dedicated server; so your game client cannot directly access the server instance.+It may seem complicated to have communication with the server in three different ways. However, you don't need to communicate in three different ways with the game client. Since all three connection types communicate with the game client using packets, you only need to communicate with the game client like you are always running on a dedicated server. Connection to a server over LAN or Singleplayer can also be treated like the server is a remote dedicated server; so your game client cannot directly access the server instance.
  
 ===== An introduction to networking ===== ===== An introduction to networking =====
Line 80: Line 80:
 </code> </code>
  
-Next, we need to send the packet to the game client. First, you need to define an ''Identifier'' used to identify your packet. For this example our Identifier will be ''wiki_example:highlight_block''. In order to send the packet to the game client, you need to specify which player's game client you want the packet to be received by. Since the action is occurring on the logical server, we may upcast the ''player'' to a ''ServerPlayerEntity''.+Next, we need to send the packet to the game client. First, you need to define an ''Identifier'' used to identify your packet. For this example our Identifier will be ''tutorial:highlight_block''. In order to send the packet to the game client, you need to specify which player's game client you want the packet to be received by. Since the action is occurring on the logical server, we may upcast the ''player'' to a ''ServerPlayerEntity''.
 <code java> <code java>
 public class TutorialNetworkingConstants { public class TutorialNetworkingConstants {
     // Save the id of the packet so we can reference it later     // Save the id of the packet so we can reference it later
-    public static final Identifier HIGHLIGHT_PACKET_ID = new Identifier("wiki_example", "highlight_block");+    public static final Identifier HIGHLIGHT_PACKET_ID = Identifier.of("tutorial", "highlight_block");
 } }
 </code> </code>
Line 131: Line 131:
     client.execute(() -> {     client.execute(() -> {
         // Everything in this lambda is run on the render thread         // Everything in this lambda is run on the render thread
-        ClientBlockHighlighting.highlightBlock(client, target); 
     });     });
 }); });
Line 221: Line 220:
  
 ===== Networking in 1.20.5 ===== ===== Networking in 1.20.5 =====
-Since 1.20.5, the logic of networking has been totally rewritten. In 1.20.5, you have to define a custom ''Payload''. First, define the payload that includes a ''BlockPos'':+Since 1.20.5, the logic of networking has been totally rewritten. In 1.20.5, ''RegistryByteBuf'' is now used in PLAY-phase networking and you have to define a custom ''Payload''. First, define the payload that includes a ''BlockPos'':
  
 <code java> <code java>
 public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload { public record BlockHighlightPayload(BlockPos blockPos) implements CustomPayload {
-  public static final Id<BlockHighlightPayload> ID = CustomPayload.id("tutorial:block_highlight"); +    public static final CustomPayload.Id<BlockHighlightPayload> ID = new CustomPayload.Id<>(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID); 
-  public static final PacketCodec<PacketByteBuf, BlockHighlightPayload> CODEC = PacketCodec.tuple(BlockPos.PACKET_CODEC, BlockHighlightPayload::blockPos, BlockHighlightPayload::new); +    public static final PacketCodec<RegistryByteBuf, BlockHighlightPayload> CODEC = PacketCodec.tuple(BlockPos.PACKET_CODEC, BlockHighlightPayload::blockPos, BlockHighlightPayload::new); 
-  // or you can also write like this+    // should you need to send more data, add the appropriate record parameters and change your codec
-  // public static final PacketCodec<PacketByteBuf, BlockHighlightPayload> CODEC = PacketCodec.of((value, buf) -> buf.writeBlockPos(value.blockPos)buf -> new BlockHighlightPayload(buf.readBlockPos()));+    // public static final PacketCodec<RegistryByteBuf, BlockHighlightPayload> CODEC = PacketCodec.tuple( 
 +    //         BlockPos.PACKET_CODEC, BlockHighlightPayload::blockPos, 
 +    //         PacketCodecs.INTEGER, BlockHighlightPayload::myInt, 
 +    //         Uuids.PACKET_CODEC, BlockHighlightPayload::myUuid, 
 +    //         BlockHighlightPayload::new 
 +    // );
  
-  @Override +    @Override 
-  public Id<? extends CustomPayload> getId() { +    public CustomPayload.Id<? extends CustomPayload> getId() { 
-    return ID; +        return ID; 
-  }+    }
 } }
 </code> </code>
Line 239: Line 243:
 And then, register the receiver like this: And then, register the receiver like this:
 <code java> <code java>
 +// NOTE: PayloadTypeRegistry has 2 functions:
 +//       - playS2C is for server -> client communication
 +//       - playC2S is for client -> server communication
 +
 +// In your common initializer method
 PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC); PayloadTypeRegistry.playS2C().register(BlockHighlightPayload.ID, BlockHighlightPayload.CODEC);
 +
 +// In your client-only initializer method
 ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, (payload, context) -> { ClientPlayNetworking.registerGlobalReceiver(BlockHighlightPayload.ID, (payload, context) -> {
-  context.client().execute(() -> { +    context.client().execute(() -> { 
-    ClientBlockHighlighting.highlightBlock(client, target); +        ClientBlockHighlighting.highlightBlock(client, payload.blockPos()); 
-  });+    });
 }); });
 </code> </code>
Line 255: Line 266:
  
         for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) {         for (ServerPlayerEntity player : PlayerLookup.tracking((ServerWorld) world, target)) {
-            ServerPlayNetworking.send(player, new BlockHighlightPayload(blockPos));+            ServerPlayNetworking.send(player, new BlockHighlightPayload(target));
         }         }
  
tutorial/networking.1714852299.txt.gz · Last modified: 2024/05/04 19:51 by bluemeanial