Skip to content

How to send data from a player's game to the server (Minecraft mod)

On 1.20.5 and newer the client sends a CustomPacketPayload to the server: Fabric API registers it with PayloadTypeRegistry.playC2S() and a server receiver, NeoForge with playToServer in RegisterPayloadHandlersEvent; ClientPlayNetworking.send or PacketDistributor.sendToServer sends it. Older versions send a hand-written message through a FriendlyByteBuf on Fabric, a SimpleChannel (SimpleNetworkWrapper on 1.12.2) on Forge. The server must not trust it: check every value, as a modified client can send anything.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Make the message class: a payload record with its Type and StreamCodec on 1.20.5 and newer.
  2. Register it for client-to-server with the server’s handler.
  3. Send it from the client.
  4. On the server, check the values before using them.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Uses:

  • FabricPacket
  • PacketType.create
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

Before 1.20.5 there is no CustomPacketPayload and StreamCodec: Fabric API’s FabricPacket writes to a FriendlyByteBuf and a PacketType reads it back, and nothing needs registering in a PayloadTypeRegistry.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.fabricmc.fabric.api.networking.v1.FabricPacket;
import net.fabricmc.fabric.api.networking.v1.PacketType;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id and a reader) and how to write it (Fabric API's FabricPacket, 1.20.2 to 1.20.4).
public record PingPayload(int number) implements FabricPacket {
public static final PacketType<PingPayload> TYPE = PacketType.create(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), PingPayload::new);
public PingPayload(FriendlyByteBuf buf) {
this(buf.readVarInt());
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(number);
}
@Override
public PacketType<?> getType() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: its receiver runs on the server thread (Fabric API, 1.20.4).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayNetworking.registerGlobalReceiver(PingPayload.TYPE, (payload, player, sender) -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
player.sendSystemMessage(Component.literal("The server got " + number));
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.send(new PingPayload(42)));
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • CustomPacketPayload
  • PayloadTypeRegistry.playC2S
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
new ResourceLocation(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: registered for client to server; its receiver runs on the server thread (Fabric API, 1.21.1).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
PayloadTypeRegistry.playC2S().register(PingPayload.TYPE, PingPayload.STREAM_CODEC);
ServerPlayNetworking.registerGlobalReceiver(PingPayload.TYPE, (payload, context) -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.send(new PingPayload(42)));
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Fabric 1.21 to 1.21.1, Fabric 1.21.2 to 1.21.10

Section titled “Fabric 1.21 to 1.21.1, Fabric 1.21.2 to 1.21.10”

Uses:

  • CustomPacketPayload
  • PayloadTypeRegistry.playC2S
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: registered for client to server; its receiver runs on the server thread (Fabric API, 1.21.1).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
PayloadTypeRegistry.playC2S().register(PingPayload.TYPE, PingPayload.STREAM_CODEC);
ServerPlayNetworking.registerGlobalReceiver(PingPayload.TYPE, (payload, context) -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.send(new PingPayload(42)));
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.PacketDistributor;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
new ResourceLocation(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.PacketDistributor;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • IPayloadRegistrar.play
  • PacketDistributor.SERVER
  • ClientPlayerNetworkEvent.LoggingIn
  • PlayPayloadContext.player

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its id and how to write and read it (a FriendlyByteBuf).
public record PingPayload(int number) implements CustomPacketPayload {
public static final ResourceLocation ID = new ResourceLocation(ExampleMod.MOD_ID, "ping");
public static PingPayload read(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
@Override
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(number);
}
@Override
public ResourceLocation id() {
return ID;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent;
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlerEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlerEvent event) {
event.registrar("examplemod").versioned("1").play(PingPayload.ID, PingPayload::read, handler -> handler.server(PingNetwork::handle));
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, PlayPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().ifPresent(player -> player.sendSystemMessage(Component.literal("The server got " + number)));
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.PacketDistributor;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PacketDistributor.SERVER.noArg().send(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().displayClientMessage(Component.literal("The server got " + number), false);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.network.PacketDistributor;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

Since 1.21.6 the client sends with ClientPacketDistributor.sendToServer.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().displayClientMessage(Component.literal("The server got " + number), false);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;
import net.neoforged.neoforge.common.NeoForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
ClientPacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

Since 1.21.6 the client sends with ClientPacketDistributor.sendToServer.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().displayClientMessage(Component.literal("The server got " + number), false);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;
import net.neoforged.neoforge.common.NeoForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
ClientPacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

Since 1.21.6 the client sends with ClientPacketDistributor.sendToServer.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().displayClientMessage(Component.literal("The server got " + number), false);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;
import net.neoforged.neoforge.common.NeoForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
ClientPacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadTypeRegistry.playC2S
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: registered for client to server; its receiver runs on the server thread (Fabric API, 1.21.1).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
PayloadTypeRegistry.playC2S().register(PingPayload.TYPE, PingPayload.STREAM_CODEC);
ServerPlayNetworking.registerGlobalReceiver(PingPayload.TYPE, (payload, context) -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.send(new PingPayload(42)));
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • CustomPacketPayload
  • PayloadRegistrar.playToServer
  • PacketDistributor.sendToServer
  • ClientPlayerNetworkEvent.LoggingIn
  • IPayloadContext.player

Since 1.21.6 the client sends with ClientPacketDistributor.sendToServer.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.network.chat.Component;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.handling.IPayloadContext;
// PingPayload from a player's game to the server, registered in RegisterPayloadHandlersEvent.
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register(IEventBus modBus) {
modBus.addListener(PingNetwork::registerPayloads);
}
private static void registerPayloads(RegisterPayloadHandlersEvent event) {
event.registrar("1").playToServer(PingPayload.TYPE, PingPayload.STREAM_CODEC, PingNetwork::handle);
}
// Runs on the server, on the main thread by default. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, IPayloadContext context) {
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.network.ClientPacketDistributor;
import net.neoforged.neoforge.common.NeoForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
ClientPacketDistributor.sendToServer(new PingPayload(42));
}
}

In your main mod class, inside <init>:

PingNetwork.register(modEventBus);

In your main mod class, inside <init>:

if (FMLEnvironment.getDist() == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • CustomPacketPayload
  • PayloadTypeRegistry.playC2S
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.Identifier;
// What Ping carries over the network: its type (an id) and how to write and read it (a StreamCodec).
public record PingPayload(int number) implements CustomPacketPayload {
public static final CustomPacketPayload.Type<PingPayload> TYPE = new CustomPacketPayload.Type<>(
Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, "ping"));
public static final StreamCodec<RegistryFriendlyByteBuf, PingPayload> STREAM_CODEC = StreamCodec.composite(ByteBufCodecs.VAR_INT, PingPayload::number,
PingPayload::new);
@Override
public CustomPacketPayload.Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: registered for client to server; its receiver runs on the server thread (Fabric API, 1.21.1).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
PayloadTypeRegistry.serverboundPlay().register(PingPayload.TYPE, PingPayload.STREAM_CODEC);
ServerPlayNetworking.registerGlobalReceiver(PingPayload.TYPE, (payload, context) -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
context.player().sendSystemMessage(Component.literal("The server got " + number));
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> ClientPlayNetworking.send(new PingPayload(42)));
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggingIn

Forge 1.20.1 sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.FriendlyByteBuf;
// What Ping carries over the network, and how to write and read it.
public record PingPayload(int number) {
public static void encode(PingPayload payload, FriendlyByteBuf buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.ExampleMod;
import java.util.function.Supplier;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.fmllegacy.network.NetworkEvent;
import net.minecraftforge.fmllegacy.network.NetworkRegistry;
import net.minecraftforge.fmllegacy.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
ServerPlayer player = context.get().getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new TextComponent("The server got " + number), Util.NIL_UUID);
}
});
context.get().setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggedInEvent event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggingIn

Forge 1.20.1 sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.FriendlyByteBuf;
// What Ping carries over the network, and how to write and read it.
public record PingPayload(int number) {
public static void encode(PingPayload payload, FriendlyByteBuf buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.ExampleMod;
import java.util.function.Supplier;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
ServerPlayer player = context.get().getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new TextComponent("The server got " + number), Util.NIL_UUID);
}
});
context.get().setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggedInEvent event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggingIn

Forge 1.20.1 sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.FriendlyByteBuf;
// What Ping carries over the network, and how to write and read it.
public record PingPayload(int number) {
public static void encode(PingPayload payload, FriendlyByteBuf buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import java.util.function.Supplier;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
ServerPlayer player = context.get().getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendSystemMessage(Component.literal("The server got " + number));
}
});
context.get().setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggingIn

NeoForge 1.20.1 sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.FriendlyByteBuf;
// What Ping carries over the network, and how to write and read it.
public record PingPayload(int number) {
public static void encode(PingPayload payload, FriendlyByteBuf buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import java.util.function.Supplier;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
ServerPlayer player = context.get().getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendSystemMessage(Component.literal("The server got " + number));
}
});
context.get().setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggingIn

NeoForge 1.20.2 still sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id. The payload registry with CustomPacketPayload came with 1.20.4.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.FriendlyByteBuf;
// What Ping carries over the network, and how to write and read it.
public record PingPayload(int number) {
public static void encode(PingPayload payload, FriendlyByteBuf buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.network.NetworkEvent;
import net.neoforged.neoforge.network.NetworkRegistry;
import net.neoforged.neoforge.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, NetworkEvent.Context context) {
context.enqueueWork(() -> {
ServerPlayer player = context.getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendSystemMessage(Component.literal("The server got " + number));
}
});
context.setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.common.NeoForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
NeoForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • FriendlyByteBuf
  • PacketByteBufs.create
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

FabricPacket and PacketType only exist from 1.19.4; before that a message is a channel id and a FriendlyByteBuf you write and read yourself.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its channel id and how to write and read it (a FriendlyByteBuf, before 1.19.4's FabricPacket).
public record PingPayload(int number) {
public static final ResourceLocation ID = new ResourceLocation(ExampleMod.MOD_ID, "ping");
public static PingPayload read(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(number);
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: the receiver is called on the network thread, so it hands the work to the server thread (Fabric API).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayNetworking.registerGlobalReceiver(PingPayload.ID, (server, player, handler, buf, responseSender) -> {
PingPayload payload = PingPayload.read(buf);
server.execute(() -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
player.sendSystemMessage(Component.literal("The server got " + number));
});
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.FriendlyByteBuf;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
FriendlyByteBuf buf = PacketByteBufs.create();
new PingPayload(42).write(buf);
ClientPlayNetworking.send(PingPayload.ID, buf);
});
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • FriendlyByteBuf
  • PacketByteBufs.create
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its channel id and how to write and read it (a FriendlyByteBuf, before 1.19.4's FabricPacket).
public record PingPayload(int number) {
public static final ResourceLocation ID = new ResourceLocation(ExampleMod.MOD_ID, "ping");
public static PingPayload read(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(number);
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: the receiver is called on the network thread, so it hands the work to the server thread (Fabric API).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayNetworking.registerGlobalReceiver(PingPayload.ID, (server, player, handler, buf, responseSender) -> {
PingPayload payload = PingPayload.read(buf);
server.execute(() -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new TextComponent("The server got " + number), Util.NIL_UUID);
});
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.FriendlyByteBuf;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
FriendlyByteBuf buf = PacketByteBufs.create();
new PingPayload(42).write(buf);
ClientPlayNetworking.send(PingPayload.ID, buf);
});
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • FriendlyByteBuf
  • PacketByteBufs.create
  • ServerPlayNetworking.registerGlobalReceiver
  • ClientPlayNetworking.send
  • ClientPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
// What Ping carries over the network: its channel id and how to write and read it (a FriendlyByteBuf).
public final class PingPayload {
private final int number;
public PingPayload(int number) {
this.number = number;
}
public int number() {
return number;
}
public static final ResourceLocation ID = new ResourceLocation(ExampleMod.MOD_ID, "ping");
public static PingPayload read(FriendlyByteBuf buf) {
return new PingPayload(buf.readVarInt());
}
public void write(FriendlyByteBuf buf) {
buf.writeVarInt(number);
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.chat.Component;
// The server's side of PingPayload: the receiver is called on the network thread, so it hands the work to the server thread (Fabric API).
public final class PingNetwork {
private PingNetwork() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayNetworking.registerGlobalReceiver(PingPayload.ID, (server, player, handler, buf, responseSender) -> {
PingPayload payload = PingPayload.read(buf);
server.execute(() -> {
// A modified client can send anything: check the value before using it.
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new TextComponent("The server got " + number), Util.NIL_UUID);
});
});
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingPayload;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.network.FriendlyByteBuf;
// The client's side of PingPayload: sent to the server once the player has joined.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's client entrypoint.
public static void register() {
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
FriendlyByteBuf buf = PacketByteBufs.create();
new PingPayload(42).write(buf);
ClientPlayNetworking.send(PingPayload.ID, buf);
});
}
}

In your main mod class, inside onInitialize:

PingNetwork.register();

In your client setup class, inside onInitializeClient:

PingClientNetwork.register();

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggedInEvent

Forge 1.16.5 sends custom packets through a SimpleChannel: every message class has an encoder and a decoder and is registered with an id.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import net.minecraft.network.PacketBuffer;
// What Ping carries over the network, and how to write and read it.
public final class PingPayload {
private final int number;
public PingPayload(int number) {
this.number = number;
}
public int number() {
return number;
}
public static void encode(PingPayload payload, PacketBuffer buf) {
buf.writeVarInt(payload.number());
}
public static PingPayload decode(PacketBuffer buf) {
return new PingPayload(buf.readVarInt());
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import net.minecraft.util.text.StringTextComponent;
import com.example.examplemod.ExampleMod;
import java.util.function.Supplier;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Util;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.fml.network.NetworkEvent;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;
// PingPayload from a player's game to the server, sent through a SimpleChannel.
public final class PingNetwork {
private static final String VERSION = "1";
private static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ExampleMod.MOD_ID, "ping"), () -> VERSION, VERSION::equals, VERSION::equals);
private PingNetwork() {
}
// Called from the mod's constructor.
public static void register() {
CHANNEL.registerMessage(0, PingPayload.class, PingPayload::encode, PingPayload::decode, PingNetwork::handle);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server once queued with enqueueWork. A modified client can send anything: check the value before using it.
private static void handle(PingPayload payload, Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> {
ServerPlayerEntity player = context.get().getSender();
if (player != null) {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new StringTextComponent("The server got " + number), Util.NIL_UUID);
}
});
context.get().setPacketHandled(true);
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.common.MinecraftForge;
// The client's side of PingPayload: sent to the server once the player has logged in.
public final class PingClientNetwork {
private PingClientNetwork() {
}
// Called from the mod's constructor, in the client only.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(PingClientNetwork::onLoggingIn);
}
private static void onLoggingIn(ClientPlayerNetworkEvent.LoggedInEvent event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside <init>:

PingNetwork.register();

In your main mod class, inside <init>:

if (FMLEnvironment.dist == Dist.CLIENT) {
PingClientNetwork.register();
}

Uses:

  • SimpleChannel.registerMessage
  • SimpleChannel.sendToServer
  • NetworkEvent.Context.getSender
  • ClientPlayerNetworkEvent.LoggedInEvent

In 1.12.2 the message is an IMessage with toBytes and fromBytes, sent through a SimpleNetworkWrapper, and an IMessageHandler runs on the server.

src/main/java/com/example/examplemod/network/PingPayload.java

package com.example.examplemod.network;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
// What Ping carries over the network, and how to write and read it. A 1.12.2 message needs an empty constructor.
public class PingPayload implements IMessage {
private int number;
public PingPayload() {
}
public PingPayload(int number) {
this.number = number;
}
public int number() {
return number;
}
@Override
public void fromBytes(ByteBuf buf) {
number = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(number);
}
}

src/main/java/com/example/examplemod/network/PingNetwork.java

package com.example.examplemod.network;
import com.example.examplemod.ExampleMod;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;
// PingPayload from a player's game to the server, sent through a SimpleNetworkWrapper.
public final class PingNetwork {
private static final SimpleNetworkWrapper CHANNEL = NetworkRegistry.INSTANCE.newSimpleChannel(ExampleMod.MOD_ID);
private PingNetwork() {
}
// Called from preInit.
public static void register() {
CHANNEL.registerMessage(ServerHandler.class, PingPayload.class, 0, Side.SERVER);
}
// Called in the client.
public static void sendToServer(int number) {
CHANNEL.sendToServer(new PingPayload(number));
}
// Runs on the server: addScheduledTask moves the work to the main thread. A modified client can send anything: check the value before using it.
public static class ServerHandler implements IMessageHandler<PingPayload, IMessage> {
@Override
public IMessage onMessage(PingPayload payload, MessageContext context) {
EntityPlayerMP player = context.getServerHandler().player;
player.getServerWorld().addScheduledTask(() -> {
int number = Math.max(0, Math.min(100, payload.number()));
player.sendMessage(new TextComponentString("The server got " + number));
});
return null;
}
}
}

src/main/java/com/example/examplemod/client/PingClientNetwork.java

package com.example.examplemod.client;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.network.PingNetwork;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.relauncher.Side;
// The client's side of PingPayload: sent to the server once the player has connected. The class registers itself, in the client only.
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID, value = Side.CLIENT)
public final class PingClientNetwork {
private PingClientNetwork() {
}
@SubscribeEvent
public static void onConnected(FMLNetworkEvent.ClientConnectedToServerEvent event) {
PingNetwork.sendToServer(42);
}
}

In your main mod class, inside preInit:

PingNetwork.register();

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.