Skip to content

Network and Packets

kirderf1 edited this page May 8, 2024 · 3 revisions

Minecraft is built around two logical sides: client and server. The logical server side handles things like world generation and most any simulation/game logic, while the logical client side handles rendering, screen elements and playing sounds.

More details on sides can be found here.

To communicate between sides, network packets are used. Often, vanilla handles it for you, for example sending packets to relevant players when a block is updated, or when an entity moves. But sometimes, you want to transfer data or trigger some kind of event that vanilla doesn't handle for you. For that, you need to create your own network packet (which you can find in the "network" package).

To send a minestuck packet, you need to create an instance of the packet that you want to send, and then send it using PacketDistributor.

Under the hood, the packet will then be converted to bytes, transferred over the network, converted back into a packet object on the other logical side, and then handled in a way depending on the type of the packet. (If the two logical sides is located in the same program, it might skip the byte and network step)

So to create a new type of packet, a couple things are needed:

  • A packet class that defines what type of data that the packet may contain.
  • A packet id that is unique to that type of packet.
  • A write function that converts the packet to bytes.
  • A read function that creates the packet from bytes.
  • An execute function that determines what happens when the packet has arrived.
  • The packet has to be registered in MSPayloads to link all these together, and so that the packet type is recognized.

To help implement a packet, MSPacket.PlayToClient and MSPacket.PlayToServer exists to help implement packets going to the client and server side respectively.