Class MycelNetwork

java.lang.Object
my.celium.org.network.MycelNetwork

public final class MycelNetwork extends Object
Loader-independent networking.

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 Type
    Method
    Description
    static boolean
    canSend(net.minecraft.server.level.ServerPlayer player, PacketDefinition<?> definition)
    Whether player's client can receive definition's channel, i.e.
    static boolean
    Whether the server this client is connected to has definition's channel, i.e.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    register(ModMetadata mod, PacketDefinition<T> definition)
    Registers a packet channel.
    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.
    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.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    sendToServer(PacketDefinition<T> definition, T payload)
    Sends a serverbound packet.
    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).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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)
      Whether player's client can receive definition'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 (see canSend(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

      public static boolean canSendToServer(PacketDefinition<?> definition)
      Whether the server this client is connected to has definition's channel, i.e. runs the mod. false when 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 (see canSendToServer(PacketDefinition)). Must only be called from client code: the method references client classes and is never invoked on a server.