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
Next revisionBoth sides next revision
tutorial:networking [2021/01/20 13:51] ytg1234tutorial:networking [2022/01/28 06:28] – [How do I fix the crash?] fix type solidblock
Line 51: Line 51:
 [[Minecraft's Logical Sides|{{tutorial:sides.png?700}}]] [[Minecraft's Logical Sides|{{tutorial:sides.png?700}}]]
  
-The diagram above shows that the game client and dedicated server art separate systems bridged together using packets. This packet bridge does not only exist between a game client and dedicated server, but also between your client and another client connected over LAN. The packet bridge is also present even in singleplayer! This is because the game client will spin up a special integrated server instance to run the game on. The key difference between the three types of connections that are shown in the table below:+The diagram above shows that the game client and dedicated server are separate systems bridged together using packets. This packet bridge does not only exist between a game client and dedicated server, but also between your client and another client connected over LAN. The packet bridge is also present even in singleplayer! This is because the game client will spin up a special integrated server instance to run the game on. The key difference between the three types of connections that are shown in the table below:
  
 ^ Connection Type               ^ Access to game client       ^ ^ Connection Type               ^ Access to game client       ^
Line 95: Line 95:
 <code java [enable_line_numbers="true", highlight_lines_extra="2"]> <code java [enable_line_numbers="true", highlight_lines_extra="2"]>
     ....     ....
-    ServerPlayNetworking.send((ServerPlayerEntity) user, ModNetworkingConstants.HIGHLIGHT_PACKET_ID, PacketByteBufs.empty());+    ServerPlayNetworking.send((ServerPlayerEntity) user, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, PacketByteBufs.empty());
     return TypedActionResult.success(user.getHandStack(hand));     return TypedActionResult.success(user.getHandStack(hand));
 } }
Line 105: Line 105:
  
 To receive a packet from a server on the game client, your mod needs to specify how it will handle the incoming packet. To receive a packet from a server on the game client, your mod needs to specify how it will handle the incoming packet.
-In your client entrypoint, you will register the receiver for your packet using ''PlayClientNetworking.registerGlobalReceiver(Identifier channelName, ChannelHandler channelHandler)''+In your client entrypoint, you will register the receiver for your packet using ''ClientPlayNetworking.registerGlobalReceiver(Identifier channelName, ChannelHandler channelHandler)''
  
 The ''Identifier'' should match the same Identifier you use to send the packet to the client. The ''ChannelHandler'' is the functional interface you will use to implement how the packet is handled. **Note the ''ChannelHandler'' should be the one that is a nested interface of ''ClientPlayNetworking''** The ''Identifier'' should match the same Identifier you use to send the packet to the client. The ''ChannelHandler'' is the functional interface you will use to implement how the packet is handled. **Note the ''ChannelHandler'' should be the one that is a nested interface of ''ClientPlayNetworking''**
Line 112: Line 112:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {+ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {
     ...     ...
 }); });
Line 122: Line 122:
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {+ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {
     client.execute(() -> {     client.execute(() -> {
         // Everything in this lambda is run on the render thread         // Everything in this lambda is run on the render thread
Line 152: Line 152:
 In the end, the client's handler would look like this: In the end, the client's handler would look like this:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-ClientPlayNetworking.registerGlobalReceiver(ModNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {+ClientPlayNetworking.registerGlobalReceiver(TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, (client, handler, buf, responseSender) -> {
     // Read packet data on the event loop     // Read packet data on the event loop
     BlockPos target = buf.readBlockPos();     BlockPos target = buf.readBlockPos();
Line 183: Line 183:
         buf.writeBlockPos(target);         buf.writeBlockPos(target);
  
-        ServerPlayNetworking.send((ServerPlayerEntity) user, ModNetworkingConstants.HIGHLIGHT_PACKET_ID, buf);+        ServerPlayNetworking.send((ServerPlayerEntity) user, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, buf);
         return TypedActionResult.success(user.getHandStack(hand));         return TypedActionResult.success(user.getHandStack(hand));
     }     }
Line 204: Line 204:
         // Iterate over all players tracking a position in the world and send the packet to each player         // 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, ModNetworkingConstants.HIGHLIGHT_PACKET_ID, buf);+            ServerPlayNetworking.send(player, TutorialNetworkingConstants.HIGHLIGHT_PACKET_ID, buf);
         }         }
  
tutorial/networking.txt · Last modified: 2024/05/04 19:51 by bluemeanial