Send data from server to client
This guide sends a server’s path settings to each player who joins and to every player when an operator reloads them, and lets a client ask for them when the player opens the mod’s settings screen, so the client learns whether paths wear on that server and how fast, even when its own settings file says otherwise.
Declare the channel and the packets
Section titled “Declare the channel and the packets”In your mod’s common initialization, which runs on both sides, declare the channel and its
two packets from the
NetworkChannel example:
SETTINGS carries a ServerSettings record from the server to the client, and QUERY asks
the server, which answers with SETTINGS. The values come from Settings, the record from
Add a config file and settings screen.
Receive the settings and ask for them on the client
Section titled “Receive the settings and ask for them on the client”In your mod’s client code, add the lines from the
ClientNetworking example.
Client initialization keeps the latest settings in serverSettings, a static
ServerSettings field in a client-only class, and drops them when the player leaves the
world. Put the query line at the start of settingsScreen, the method that builds the
settings screen, so the client asks each time the player opens the screen.
Send the settings when they change
Section titled “Send the settings when they change”The server sends the settings to every player once an operator reloads the file. This command replaces the reload command from the config guide. In your mod’s common initializer:
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> dispatcher.register( Commands.literal("mymod") .requires(source -> source.hasPermission(2)) .then(Commands.literal("reload").executes(context -> { ServerSettings sent = ServerSettings.of(Settings.FILE.load()); for (ServerPlayer player : context.getSource().getServer().getPlayerList().getPlayers()) { NET.sendToPlayer(player, SETTINGS, sent); } Component done = Component.translatable("commands.mymod.reload"); context.getSource().sendSuccess(() -> done, true); return Command.SINGLE_SUCCESS; }))));Players without the mod can be on the same server; what a send to them does is in
NetworkChannel’s Javadoc.
Send the settings when a player joins
Section titled “Send the settings when a player joins”A player who joins after the last reload needs the settings too. Add the listener from the
PlayerJoinedCallback example
to your mod’s common initializer; it sends the loaded settings to each player as they join.
Change the packet later
Section titled “Change the packet later”A change to ServerSettings’ fields changes what the packet sends. Before you ship one, read
what NetworkChannel’s Javadoc says about the protocol version, the second argument of
NetworkChannel#create.
See also
Section titled “See also”- Networking: channels, packets, and the client side.
- Config files and settings screens: the settings file these packets carry.
- Lifecycle events: the command registration, player join, and client disconnect events.