Class Network
java.lang.Object
net.ixdarklord.coolcatcore.api.network.Network
Play-phase payloads between client and server.
Register every payload while the mod initialises, on both sides: a dedicated server still needs to know the payloads it sends. On Forge they all travel through CoolCatLib's channel, told apart by their type's id; on Fabric each type's id is its own channel.
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<>(new ResourceLocation("mymod", "ping"));
public Type<PingPayload> type() { return TYPE; }
}
Network.registerServerbound(PingPayload.class, (payload, context) -> ...);
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanPlayerReceive(net.minecraft.server.level.ServerPlayer player, 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 & CustomPacketPayload>
voidregisterClientbound(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 staticCustomPacketPayload.Typefield.static <T extends Record & CustomPacketPayload>
voidregisterClientbound(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 CustomPacketPayload>
voidregisterClientbound(CustomPacketPayload.Type<T> type, StreamCodec<? super net.minecraft.network.FriendlyByteBuf, T> codec, PayloadReceiver<T> receiver) A payload the server sends and the client receives.static <T extends Record & CustomPacketPayload>
voidregisterServerbound(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 staticCustomPacketPayload.Typefield.static <T extends Record & CustomPacketPayload>
voidregisterServerbound(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 CustomPacketPayload>
voidregisterServerbound(CustomPacketPayload.Type<T> type, StreamCodec<? super net.minecraft.network.FriendlyByteBuf, T> codec, PayloadReceiver<T> receiver) A payload the client sends and the server receives.static voidsendToPlayer(net.minecraft.server.level.ServerPlayer player, CustomPacketPayload payload) static voidsendToPlayers(Iterable<net.minecraft.server.level.ServerPlayer> players, CustomPacketPayload payload) static voidsendToServer(CustomPacketPayload payload) Client only.
-
Method Details
-
registerServerbound
public static <T extends CustomPacketPayload> void registerServerbound(CustomPacketPayload.Type<T> type, StreamCodec<? super net.minecraft.network.FriendlyByteBuf, T> codec, PayloadReceiver<T> receiver) A payload the client sends and the server receives. -
registerClientbound
public static <T extends CustomPacketPayload> void registerClientbound(CustomPacketPayload.Type<T> type, StreamCodec<? super net.minecraft.network.FriendlyByteBuf, 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 & 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 staticCustomPacketPayload.Typefield.- Throws:
IllegalArgumentException- if a component's type has no codec inPayloadCodecs, or the record has no (or more than one) staticTypefield
-
registerServerbound
public static <T extends Record & CustomPacketPayload> void registerServerbound(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 inPayloadCodecs
-
registerClientbound
public static <T extends Record & 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 staticCustomPacketPayload.Typefield. The receiver only runs on clients.- Throws:
IllegalArgumentException- if a component's type has no codec inPayloadCodecs, or the record has no (or more than one) staticTypefield
-
registerClientbound
public static <T extends Record & CustomPacketPayload> void registerClientbound(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 inPayloadCodecs
-
sendToServer
Client only. -
sendToPlayer
public static void sendToPlayer(net.minecraft.server.level.ServerPlayer player, CustomPacketPayload payload) -
sendToPlayers
public static void sendToPlayers(Iterable<net.minecraft.server.level.ServerPlayer> players, CustomPacketPayload payload) -
canPlayerReceive
public static boolean canPlayerReceive(net.minecraft.server.level.ServerPlayer player, 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). Check first when that can happen.
-