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 [2021/10/01 03:00] voleil
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