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 revision Previous revision
tutorial:networking [2020/12/14 00:57]
zulrang Transposed words in identifier PlayClientNetworking
tutorial:networking [2021/01/20 13:51] (current)
ytg1234
Line 2: Line 2:
  
 This page has replaced the old networking page. This page has replaced the old networking page.
-It is recommended to use the new networking ​api described on this page.+It is recommended to use the new networking ​API described on this page.
 The old page can be [[tutorial:​legacy:​networking-v0|found here]]. The old page can be [[tutorial:​legacy:​networking-v0|found here]].
  
Line 58: Line 58:
 | Singleplayer (or LAN host)    | Yes -> Full access ​         | | Singleplayer (or LAN host)    | Yes -> Full access ​         |
  
-It may seem complicated to have to 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 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. Howeveryou 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 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.
  
 ===== 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 to packet to be received by. Since the action is occurring on the logical server, we may upcast the ''​player''​ to a ''​ServerPlayerEntity''​.+Nextwe need to send the packet to the game client. Firstyou 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 to packet to be received by. Since the action is occurring on the logical server, we may upcast the ''​player''​ to a ''​ServerPlayerEntity''​.
  
 To send the packet to the player, we will use some of the methods inside of ''​ServerPlayNetworking''​. We will use the following method inside of that class: To send the packet to the player, we will use some of the methods inside of ''​ServerPlayNetworking''​. We will use the following method inside of that class:
Line 91: Line 91:
 The player in this method is the player the packet will be sent to. The channel name is the ''​Identifier''​ you decided on earlier to identify your packet. The ''​PacketByteBuf''​ is what stores the data for the packet. We will return later to writing data to the packet'​s payload via the buf. The player in this method is the player the packet will be sent to. The channel name is the ''​Identifier''​ you decided on earlier to identify your packet. The ''​PacketByteBuf''​ is what stores the data for the packet. We will return later to writing data to the packet'​s payload via the buf.
  
-Since we are not writing any data to the packet for now, we will send the packet with an empty payload. A buf with an empty payload may be created using ''​PacketByteBufs.empty()''​.+Since we are not writing any data to the packetfor now, we will send the packet with an empty payload. A buf with an empty payload may be created using ''​PacketByteBufs.empty()''​.
  
 <code java [enable_line_numbers="​true",​ highlight_lines_extra="​2"​]>​ <code java [enable_line_numbers="​true",​ highlight_lines_extra="​2"​]>​
Line 117: Line 117:
 </​code>​ </​code>​
  
-However you cannot draw the highlight box immediately. This is because the receiver is called on the netty event loop. The event loop runs on another thread, and you must draw the highlight box on the render thread.+Howeveryou cannot draw the highlight box immediately. This is because the receiver is called on the netty event loop. The event loop runs on another thread, and you must draw the highlight box on the render thread.
  
 In order to draw the highlight box, you need to schedule the task on the game client. This may be done with the ''​client''​ field that is provided in the channel handler. Typically you will run the task on the client by using the ''​execute''​ method: In order to draw the highlight box, you need to schedule the task on the game client. This may be done with the ''​client''​ field that is provided in the channel handler. Typically you will run the task on the client by using the ''​execute''​ method:
Line 130: Line 130:
 </​code>​ </​code>​
  
-You may have noticed you are not told where the block to highlight is. You can write this data to the packet byte buf. Instead of sending ''​PacketByteBufs.empty()''​ to the game client in your item's ''​use''​ method, instead you will create a new packet byte buf and send that instead.+You may have noticed you are not told where the block to highlight is. You can write this data to the packet byte buf. Instead of sending ''​PacketByteBufs.empty()''​ to the game client in your item's ''​use''​ method, insteadyou will create a new packet byte buf and send that instead.
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
Line 136: Line 136:
 </​code>​ </​code>​
  
-Next you need to write the data to the packet byte buf. It should be noted that you must read data in the same order you write it.+Nextyou need to write the data to the packet byte buf. It should be noted that you must read data in the same order you write it.
  
 <code java [enable_line_numbers="​true",​ highlight_lines_extra="​3"​]>​ <code java [enable_line_numbers="​true",​ highlight_lines_extra="​3"​]>​
Line 144: Line 144:
 </​code>​ </​code>​
  
-Afterwards you will send the ''​buf''​ field through the ''​send''​ method.+Afterwardsyou will send the ''​buf''​ field through the ''​send''​ method.
  
 To read this block position on the game client, you can use ''​PacketByteBuf.readBlockPos()''​. To read this block position on the game client, you can use ''​PacketByteBuf.readBlockPos()''​.
Line 165: Line 165:
 ==== Sending packets to the server and receiving packets on the server ==== ==== Sending packets to the server and receiving packets on the server ====
  
-Sending packets to a server and receiving a packet on the server ​are very similar to how you would on the client. However there are a few key differences.+Sending packets to a server and receiving a packet on the server ​is very similar to how you would on the client. Howeverthere are a few key differences.
  
 Firstly sending a packet to the server is done through ''​ClientPlayNetworking.send''​. Receiving a packet on the server is similar to receiving a packet on the client, using the ''​ServerPlayNetworking.registerGlobalReceiver(Identifier channelName,​ ChannelHandler channelHandler)''​ method. The ''​ChannelHandler''​ for the server networking also passes the ''​ServerPlayerEntity''​ (player) who sent the packet through the ''​player''​ parameter. Firstly sending a packet to the server is done through ''​ClientPlayNetworking.send''​. Receiving a packet on the server is similar to receiving a packet on the client, using the ''​ServerPlayNetworking.registerGlobalReceiver(Identifier channelName,​ ChannelHandler channelHandler)''​ method. The ''​ChannelHandler''​ for the server networking also passes the ''​ServerPlayerEntity''​ (player) who sent the packet through the ''​player''​ parameter.
Line 171: Line 171:
 ===== The concept of tracking and why you only see the highlighted block ===== ===== The concept of tracking and why you only see the highlighted block =====
  
-Now that the highlighting wand properly uses networking so the dedicated server does not crash, you invite your friend back on the server to show off the highlighting wand. You use the wand and the block is highlighted on your client and the server does not crash. However your friend does not see the highlighted block. This is intentional with the code that you already have here. To solve this issue let us take a look at the item's use code:+Now that the highlighting wand properly uses networking so the dedicated server does not crash, you invite your friend back on the server to show off the highlighting wand. You use the wand and the block is highlighted on your client and the server does not crash. Howeveryour friend does not see the highlighted block. This is intentional with the code that you already have here. To solve this issue let us take a look at the item's use code:
  
 <code java [enable_line_numbers="​true"​]>​ <code java [enable_line_numbers="​true"​]>​
Line 192: Line 192:
 Since we know where the highlight will occur, we can use ''​PlayerLookup.tracking(ServerWorld world, BlockPos pos)''​ to get a collection of all players who can see that position in the world. Then you would simply iterate through all players in the returned collection and send the packet to each player: Since we know where the highlight will occur, we can use ''​PlayerLookup.tracking(ServerWorld world, BlockPos pos)''​ to get a collection of all players who can see that position in the world. Then you would simply iterate through all players in the returned collection and send the packet to each player:
  
-<code java [enable_line_numbers="​true",​ highlight_lines_extra="​2,3"]>+<code java [enable_line_numbers="​true",​ highlight_lines_extra="​10,11,12,13"]>
     public TypedActionResult<​ItemStack>​ use(World world, PlayerEntity user, Hand hand) {     public TypedActionResult<​ItemStack>​ use(World world, PlayerEntity user, Hand hand) {
         // Verify we are processing the use action on the logical server         // Verify we are processing the use action on the logical server
Line 211: Line 211:
 </​code>​ </​code>​
  
-After this change, when you use the wand, you friend should also see the highlighted block on their own client.+After this change, when you use the wand, your friend should also see the highlighted block on their own client.
  
 ====== Advanced Networking topics ====== ====== Advanced Networking topics ======
tutorial/networking.txt ยท Last modified: 2021/01/20 13:51 by ytg1234