Sending Packets
Once a packet has been added to the network, it can be called to send a message to the side it refers to. Usually there are four different directions a packet can be sent:
Direction | Description |
---|---|
PLAY_TO_CLIENT |
A packet is sent from the server to the client during gameplay. |
PLAY_TO_SERVER |
A packet is sent from the client to the server during gameplay. |
LOGIN_TO_CLIENT |
A packet is sent to the client on initial login. |
LOGIN_TO_SERVER |
A packet is sent to the server on initial login. |
The two login packets are handled internally by forge itself. However, the other two need to be sent using SimpleChannel#send
for SimpleChannel#sendToServer
. Each method takes a new instance of the message to send.
Client Packet Locations
- It might not always be necessary to update every single client with a packet if they are not viewing the entity or are not affected by it. To specify which clients to send a packet from the server to, a
PacketDistributor$PacketTarget
must also be passed in as a parameter. There are many helpful fields that hold simple implementations of where to send which packet withinPacketDistributor
. However, they must be supplied with the required input either viaPacketDistributor#with
orPacketDistributor#noArg
if it is going to all players.
Here is a list of Forge provided PacketDistributor
s:
PacketDistributor |
Supplied Value | Description |
---|---|---|
PLAYER |
ServerPlayer |
Send to the specified player . |
DIMENSION |
ResourceKey<Level> |
Send to all players within the specified dimension. |
NEAR |
TargetPoint |
Send to all players within the range of the target point. |
ALL |
None | Send to all currently logged in players. |
SERVER |
None | Send from the player client to the server. This is the only distributor that should be used for client -> server communication. |
TRACKING_ENTITY |
Entity |
Send to all players currently tracking the specified entity. |
TRACKING_ENTITY_AND_SELF |
Entity |
Send to all players currently tracking the specified entity and the entity itself, if it is a player. |
TRACKING_CHUNK |
LevelChunk |
Send to all players tracking the specified chunk. |
NMLIST |
List<Connection> |
Send to the listed connections. Each connection is typically an individual player. |