Class MycelNetwork
Mods define CustomPacketPayload records, describe them with
PacketDefinition, and register them here:
MycelNetwork.register(META, PacketDefinition.toClient(GreetPacket.TYPE, GreetPacket.CODEC, (packet, ctx) -> {
// runs on the client game thread
}));
MycelNetwork.sendToPlayer(player, GREET, new GreetPacket("hi"));
Sending uses plain vanilla packets on Fabric and NeoForge and Forge's
channel on Forge (which refuses unwrapped payloads); callers never notice.
Receiving is wired by each loader module (Fabric entrypoints, Forge/NeoForge
payload events) which iterate definitions(); handlers always run on
the game thread.
Channels are optional on every loader: a player with your mod can join a
server without it, and the other way round. The send* methods
therefore skip players whose client cannot receive a channel; use
canSend(ServerPlayer, PacketDefinition) (or canSendToServer(PacketDefinition) on the client) to find out
yourself, e.g. to fall back to chat text.
On Forge every Mycel packet shares Mycel's own channel, so there
canSend reports whether the client has Mycel at all.
On Fabric, packets registered after Mycel's entrypoint ran need one extra
FabricNetworking.flush() call from the mod's main entrypoint and
FabricNetworking.flushClient() from its client entrypoint - see the
example mod. Both are idempotent. On Forge/NeoForge the payload event always
fires after every mod constructor, so nothing extra is needed.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanSend(net.minecraft.server.level.ServerPlayer player, PacketDefinition<?> definition) Whetherplayer's client can receivedefinition's channel, i.e.static booleancanSendToServer(PacketDefinition<?> definition) Whether the server this client is connected to hasdefinition's channel, i.e.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
voidregister(ModMetadata mod, PacketDefinition<T> definition) Registers a packet channel.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
voidsendToAll(net.minecraft.server.MinecraftServer server, PacketDefinition<T> definition, T payload) Sends a clientbound packet to every connected player that can receive it.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
voidsendToPlayer(net.minecraft.server.level.ServerPlayer player, PacketDefinition<T> definition, T payload) Sends a clientbound packet to one player.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
voidsendToServer(PacketDefinition<T> definition, T payload) Sends a serverbound packet.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
voidsendToTracking(net.minecraft.server.level.ServerLevel level, net.minecraft.world.level.ChunkPos pos, PacketDefinition<T> definition, T payload) Sends a clientbound packet to players near a chunk (same dimension, within roughly view distance).
-
Method Details
-
register
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void register(ModMetadata mod, PacketDefinition<T> definition) Registers a packet channel. Duplicate registrations are ignored with a warning. Safe to call during mod init on any loader. -
canSend
public static boolean canSend(net.minecraft.server.level.ServerPlayer player, PacketDefinition<?> definition) Whetherplayer's client can receivedefinition's channel, i.e. has the mod installed. Call on the server thread. -
sendToPlayer
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void sendToPlayer(net.minecraft.server.level.ServerPlayer player, PacketDefinition<T> definition, T payload) Sends a clientbound packet to one player. Call on the server thread. Does nothing when the player's client lacks the channel (seecanSend(ServerPlayer, PacketDefinition)). -
sendToAll
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void sendToAll(net.minecraft.server.MinecraftServer server, PacketDefinition<T> definition, T payload) Sends a clientbound packet to every connected player that can receive it. Call on the server thread. -
sendToTracking
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void sendToTracking(net.minecraft.server.level.ServerLevel level, net.minecraft.world.level.ChunkPos pos, PacketDefinition<T> definition, T payload) Sends a clientbound packet to players near a chunk (same dimension, within roughly view distance). Call on the server thread. -
canSendToServer
Whether the server this client is connected to hasdefinition's channel, i.e. runs the mod.falsewhen not connected. Client only. -
sendToServer
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void sendToServer(PacketDefinition<T> definition, T payload) Sends a serverbound packet. Does nothing when the server lacks the channel (seecanSendToServer(PacketDefinition)). Must only be called from client code: the method references client classes and is never invoked on a server.
-