Class PayloadCodecs

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

public final class PayloadCodecs extends Object
Stream codecs found by type, so a record payload needs no hand-written StreamCodec: each record component is encoded in declaration order with the codec of its declared type.

Out of the box: primitives and their boxes, String, UUID, Identifier, BlockPos, ChunkPos, GlobalPos, Vec3, Vector3f(c), Quaternionf(c), Component, ItemStack (may be empty), CompoundTag, Tag, BlockState, Item, Block, EntityType, OptionalInt, byte[], long[], plus, built from their parts: enums (by ordinal), records, arrays, Optional, List/Collection, Set, Map, Either and ResourceKey.

Any other type must be registered before a payload using it is registered; registering such a payload throws otherwise. Components may not be null: use Optional.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Builds the codec of a generic type from its type arguments.
    static interface 
    Resolves the codecs of type arguments inside a PayloadCodecs.Factory.
  • Method Summary

    Modifier and Type
    Method
    Description
    static <R extends Record>
    net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf, R>
    forRecord(Class<R> recordClass)
    The codec of a record, derived from its components.
    static net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf, ?>
    get(Type type)
    The codec of any supported type, such as String.class or a ParameterizedType from reflection.
    static boolean
    has(Type type)
    Whether get(Type) would find a codec for the type.
    static <V> void
    register(Class<V> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf, V> codec)
    Makes type usable in record payloads.
    static void
    Makes a generic type usable in record payloads, e.g.

    Methods inherited from class Object

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

    • register

      public static <V> void register(Class<V> type, net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.RegistryFriendlyByteBuf, V> codec)
      Makes type usable in record payloads. A type can only be registered once.
    • registerFactory

      public static void registerFactory(Class<?> rawType, PayloadCodecs.Factory factory)
      Makes a generic type usable in record payloads, e.g. a container whose codec depends on its type arguments. The factory receives the type arguments as declared (e.g. [String, Integer] for Pair<String, Integer>) and resolves the codecs it needs through the PayloadCodecs.Lookup.
    • forRecord

      public static <R extends Record> net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf, R> forRecord(Class<R> recordClass)
      The codec of a record, derived from its components.
      Throws:
      IllegalArgumentException - if a component's type (at any depth) has no codec
    • get

      public static net.minecraft.network.codec.StreamCodec<net.minecraft.network.RegistryFriendlyByteBuf, ?> get(Type type)
      The codec of any supported type, such as String.class or a ParameterizedType from reflection.
      Throws:
      IllegalArgumentException - if the type, or a type it's built from, has no codec
    • has

      public static boolean has(Type type)
      Whether get(Type) would find a codec for the type.