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, ResourceLocation, 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.

  • Method Details

    • register

      public static <V> void register(Class<V> type, StreamCodec<? super net.minecraft.network.FriendlyByteBuf,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> StreamCodec<net.minecraft.network.FriendlyByteBuf,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 StreamCodec<net.minecraft.network.FriendlyByteBuf,?> 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(java.lang.reflect.Type) would find a codec for the type.