java.lang.Object
net.ixdarklord.coolcatcore.api.network.Network

public final class Network extends Object
Play-phase payloads between client and server.

Register every payload while the mod initialises (NeoForge collects them once, early in loading), on both sides: a dedicated server still needs to know the payloads it sends.

A record payload can skip its StreamCodec: pass the record class and each component is encoded with the codec PayloadCodecs has for its type.


 public record PingPayload(BlockPos pos, List<String> tags, Optional<Component> note) implements CustomPacketPayload {
     public static final Type<PingPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath("mymod", "ping"));

     public Type<PingPayload> type() { return TYPE; }
 }

 Network.registerServerbound(PingPayload.class, (payload, context) -> ...);
 
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    canPlayerReceive(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type)
    Whether this player's connection accepts the payload: false for clients without the mod that registered it, and for connections that never negotiated payloads (e.g.
    static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerClientbound(Class<T> payloadClass, PayloadReceiver<T> receiver)
    A record payload the server sends and the client receives, with its codec derived from its components and its type read from its static CustomPacketPayload.Type field.
    static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerClientbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, Class<T> payloadClass, PayloadReceiver<T> receiver)
    A record payload the server sends and the client receives, with its codec derived from its components.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerClientbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf,T> codec, PayloadReceiver<T> receiver)
    A payload the server sends and the client receives.
    static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerServerbound(Class<T> payloadClass, PayloadReceiver<T> receiver)
    A record payload the client sends and the server receives, with its codec derived from its components and its type read from its static CustomPacketPayload.Type field.
    static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerServerbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, Class<T> payloadClass, PayloadReceiver<T> receiver)
    A record payload the client sends and the server receives, with its codec derived from its components.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    void
    registerServerbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf,T> codec, PayloadReceiver<T> receiver)
    A payload the client sends and the server receives.
    static void
    sendToPlayer(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
     
    static void
    sendToPlayers(Iterable<net.minecraft.server.level.ServerPlayer> players, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
     
    static void
    sendToServer(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
    Client only.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • registerServerbound

      public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerServerbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf,T> codec, PayloadReceiver<T> receiver)
      A payload the client sends and the server receives.
    • registerClientbound

      public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerClientbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf,T> codec, PayloadReceiver<T> receiver)
      A payload the server sends and the client receives. The receiver only runs on clients.
    • registerServerbound

      public static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerServerbound(Class<T> payloadClass, PayloadReceiver<T> receiver)
      A record payload the client sends and the server receives, with its codec derived from its components and its type read from its static CustomPacketPayload.Type field.
      Throws:
      IllegalArgumentException - if a component's type has no codec in PayloadCodecs, or the record has no (or more than one) static Type field
    • registerServerbound

      public static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerServerbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, Class<T> payloadClass, PayloadReceiver<T> receiver)
      A record payload the client sends and the server receives, with its codec derived from its components.
      Throws:
      IllegalArgumentException - if a component's type has no codec in PayloadCodecs
    • registerClientbound

      public static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerClientbound(Class<T> payloadClass, PayloadReceiver<T> receiver)
      A record payload the server sends and the client receives, with its codec derived from its components and its type read from its static CustomPacketPayload.Type field. The receiver only runs on clients.
      Throws:
      IllegalArgumentException - if a component's type has no codec in PayloadCodecs, or the record has no (or more than one) static Type field
    • registerClientbound

      public static <T extends Record & net.minecraft.network.protocol.common.custom.CustomPacketPayload> void registerClientbound(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, Class<T> payloadClass, PayloadReceiver<T> receiver)
      A record payload the server sends and the client receives, with its codec derived from its components. The receiver only runs on clients.
      Throws:
      IllegalArgumentException - if a component's type has no codec in PayloadCodecs
    • sendToServer

      public static void sendToServer(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
      Client only.
    • sendToPlayer

      public static void sendToPlayer(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
    • sendToPlayers

      public static void sendToPlayers(Iterable<net.minecraft.server.level.ServerPlayer> players, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
    • canPlayerReceive

      public static boolean canPlayerReceive(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type)
      Whether this player's connection accepts the payload: false for clients without the mod that registered it, and for connections that never negotiated payloads (e.g. GameTest mock players). NeoForge throws when sending such a player a payload, so check first when that can happen.