Difference between revisions of "Protocol FAQ"

From wiki.vg
Jump to navigation Jump to search
Line 108: Line 108:
 
You may be misusing the socket API. In particular, it is invalid to assume that the amount of data returned by calls to <code>recv</code> (or equivalent, depending on the API you're using) relates to packet boundaries in any way. There are no "borders" in a [[wikipedia:Transmission Control Protocol|TCP]] data stream, only bytes. Regardless of how any two consecutive packets are sent, the receiver may get one packet, then the other, both at once, half of one, then the rest of both, or any other permutation of buffer sizes adding up to the total size of the packets. The only correct way to know where one packet ends and another begins is the packet length field, and you need to be prepared to handle multiple packets in one buffer, packets split across multiple buffers, etc. (as well as length fields split across multiple buffers!)
 
You may be misusing the socket API. In particular, it is invalid to assume that the amount of data returned by calls to <code>recv</code> (or equivalent, depending on the API you're using) relates to packet boundaries in any way. There are no "borders" in a [[wikipedia:Transmission Control Protocol|TCP]] data stream, only bytes. Regardless of how any two consecutive packets are sent, the receiver may get one packet, then the other, both at once, half of one, then the rest of both, or any other permutation of buffer sizes adding up to the total size of the packets. The only correct way to know where one packet ends and another begins is the packet length field, and you need to be prepared to handle multiple packets in one buffer, packets split across multiple buffers, etc. (as well as length fields split across multiple buffers!)
  
Similarly, depending on the API being used, a <code>send</code> call may also not quarantee that its whole input buffer is sent (consult the relevant documentation for details). This may also cause the connection to appear to hang during the login process, since the server will be left waiting indefinitely for the rest of the packet to arrive.
+
Similarly, depending on the API being used, a <code>send</code> call may also not guarantee that its whole input buffer is sent (consult the relevant documentation for details). This may also cause the connection to appear to hang during the login process, since the server will be left waiting indefinitely for the rest of the packet to arrive.
  
 
One reason why packets sent separately may arrive at once is [[wikipedia:Nagle's algorithm|Nagle's algorithm]], a feature of many TCP implementations intended to improve efficiency particularly for applications making lots of small <code>send</code> calls. It is not the ''only'' reason, though, and disabling it should not be seen as a solution to the problem discussed here. Nonetheless, the delays it introduces are detrimental to real-time applications like Minecraft, so the Notchian client and server disable it, and you should too. This is typically done by enabling a socket option called <code>TCP_NODELAY</code>. Especially when disabling Nagle's algorithm, you should group multiple packets sent during the same tick in one send buffer for the best performance.
 
One reason why packets sent separately may arrive at once is [[wikipedia:Nagle's algorithm|Nagle's algorithm]], a feature of many TCP implementations intended to improve efficiency particularly for applications making lots of small <code>send</code> calls. It is not the ''only'' reason, though, and disabling it should not be seen as a solution to the problem discussed here. Nonetheless, the delays it introduces are detrimental to real-time applications like Minecraft, so the Notchian client and server disable it, and you should too. This is typically done by enabling a socket option called <code>TCP_NODELAY</code>. Especially when disabling Nagle's algorithm, you should group multiple packets sent during the same tick in one send buffer for the best performance.

Revision as of 19:47, 18 March 2024

People very, very often have questions regarding the Minecraft Modern Protocol, so we'll try to address some of the most common ones on this document. If you're still having trouble, join us on IRC, channel #mcdevs on irc.libera.chat.

Is the protocol documentation complete?

Depending on your definition, yes! All packet types are known and their layout documented. Some finer details are missing, but everything you need to make functional programs is present. We also collect information on the pre-release protocol changes, allowing us to quickly document new releases.

What's the normal login sequence for a client?

See Authentication for communication with Mojang's servers.

The recommended login sequence as of 1.20.4 looks like this, where C is the client and S is the server:

  1. Client connects to the server
  2. CS: Handshake State=2
  3. CS: Login Start
  4. SC: Encryption Request
  5. Client auth
  6. CS: Encryption Response
  7. Server auth, both enable encryption
  8. SC: Set Compression (Optional, enables compression)
  9. SC: Login Success
  10. CS: Login Acknowledged
  11. CS: Serverbound Plugin Message (Optional, minecraft:brand with the client's brand)
  12. CS: Client Information (Optional)
  13. SC: Clientbound Plugin Message (Optional, minecraft:brand with the server's brand)
  14. SC: Feature Flags (Optional)
  15. SC: Registry Data
  16. SC: Update Tags (Optional)
  17. SC: Finish Configuration
  18. CS: Acknowledge Finish Configuration
  19. SC: Login (play)
  20. SC: Change Difficulty (Optional)
  21. SC: Player Abilities (Optional)
  22. SC: Set Held Item (Optional)
  23. SC: Update Recipes (Optional)
  24. SC: Entity Event (Optional, for the OP permission level; see Entity statuses#Player)
  25. SC: Commands (Optional)
  26. SC: Update Recipe Book (Optional)
  27. SC: Synchronize Player Position
  28. CS: Confirm Teleportation
  29. CS: Set Player Position and Rotation (Optional, to confirm the spawn position)
  30. SC: Server Data (Optional)
  31. SC: Player Info Update (Add Player action, all players except the one joining (the Notchian server separates these, you don't need to))
  32. SC: Player Info Update (Add Player action, joining player)
  33. SC: Initialize World Border (Optional)
  34. SC: Update Time (Optional)
  35. SC: Set Default Spawn Position (Optional, “home” spawn, not where the client will spawn on login)
  36. SC: Game Event (Start waiting for level chunks event, required for the client to spawn)
  37. SC: Set Ticking State (Optional)
  38. SC: Step Tick (Optional, the Notchian server sends this regardless of ticking state)
  39. SC: Set Center Chunk
  40. SC: Chunk Data and Update Light (One sent for each chunk in a circular area centered on the player's position)
  41. SC: inventory, entities, etc.

What does the normal status ping sequence look like?

When a Notchian client and server exchange information in a status ping, the exchange of packets will be as follows:

  1. CS: Handshake with Next State set to 1 (Status)
  2. Client and Server set protocol state to Status.
  3. CS: Status Request
  4. SC: Status Response
  5. CS: Ping Request
  6. SC: Ping Response
  7. Both sides close the connection

(Note that C is the Notchian client and S is the Notchian server).

Offline mode

If the server is in offline mode, it will not send the Encryption Request packet, and likewise, the client should not send Encryption Response. In this case, encryption is never enabled, and no authentication is performed.

Clients can tell that a server is in offline mode if the server sends a Login Success without sending Encryption Request.

I think I've done everything right, but…

…my player isn't spawning!

The Minecraft client will wait at the "Loading Terrain..." screen until late in the login sequence. As of 1.20.4, in order for the client to spawn, it must have received a Game Event packet with event 13 ("Start waiting for level chunks"), and at least one of the following conditions must be met:

In past versions, you could either (1.19.3 through 1.20.1) send the default spawn position packet or (pre-1.19.3) send the player position packet. In general, try sending packets that inform the client about the player's position in the world in order to get past the loading terrain screen.

As of 1.20.4, the minimum packets that need to be received and sent by the server in order to get the client past the loading terrain screen and into the world appear to be:

  1. Receive Handshake
  2. Receive Login Start
  3. Send Login Success
  4. Receive Login Acknowledged
  5. Send Registry Data
  6. Send Finish Configuration
  7. Receive Acknowledge Finish Configuration
  8. Send Login (Play)
  9. Send Game Event, Start waiting for level chunks
  10. Send Chunk Data and Update Light and/or Synchronize Player Position (see above)

The most difficult part of this may be sending any necessary NBTs in the Registry Data packet. You will probably need to record one from the standard server and replay it. Or you can find someone who has done that already, for example in Norbiros' gist. You can also find JSON representation of this packet in PrismarineJS/minecraft-data repo on GitHub.

…chunks are randomly showing and disappearing!

The Notchian client only reliably renders chunks surrounded by other loaded chunks on all sides. See Chunk Format#Tips and notes.

…the client is trying to send an invalid packet that begins with 0xFE01

The client is attempting a legacy ping, this happens if your server did not respond to the Server List Ping properly, including if it sent malformed JSON.

…the client disconnects after some time with a "Timed out" error

The server is expected to send a Keep Alive packet every 1-15 seconds (if the server does not send a keep alive within 20 seconds of state change to Play, the client will disconnect from the server for Timed Out), and the client should respond with the serverbound version of that packet. If either party does not receive keep alives for some period of time, they will disconnect.

...some of the packets I expect to receive seem to be missing or too short

You may be misusing the socket API. In particular, it is invalid to assume that the amount of data returned by calls to recv (or equivalent, depending on the API you're using) relates to packet boundaries in any way. There are no "borders" in a TCP data stream, only bytes. Regardless of how any two consecutive packets are sent, the receiver may get one packet, then the other, both at once, half of one, then the rest of both, or any other permutation of buffer sizes adding up to the total size of the packets. The only correct way to know where one packet ends and another begins is the packet length field, and you need to be prepared to handle multiple packets in one buffer, packets split across multiple buffers, etc. (as well as length fields split across multiple buffers!)

Similarly, depending on the API being used, a send call may also not guarantee that its whole input buffer is sent (consult the relevant documentation for details). This may also cause the connection to appear to hang during the login process, since the server will be left waiting indefinitely for the rest of the packet to arrive.

One reason why packets sent separately may arrive at once is Nagle's algorithm, a feature of many TCP implementations intended to improve efficiency particularly for applications making lots of small send calls. It is not the only reason, though, and disabling it should not be seen as a solution to the problem discussed here. Nonetheless, the delays it introduces are detrimental to real-time applications like Minecraft, so the Notchian client and server disable it, and you should too. This is typically done by enabling a socket option called TCP_NODELAY. Especially when disabling Nagle's algorithm, you should group multiple packets sent during the same tick in one send buffer for the best performance.

How do I open/save a command block?

The process to actually open the command block window clientside is somewhat complex; the client actually uses the Update Block Entity (0x09) packet to open it.

First, the client must have at least an OP permission level of 2, or else the client will refuse to open the command block. (The op permission level is set with the Entity Status packet)

To actually open the command block:

  1. CS: Player Block Placement (0x1C), with the position being the command block that was right-clicked.
  2. SC: Update Block Entity (0x09), with the NBT of the command block.

And to save it, use the MC|AutoCmd plugin channel.