Class CodecUtils

java.lang.Object
net.ixdarklord.coolcatcore.api.utils.CodecUtils

public final class CodecUtils extends Object
Utility class providing helper methods for working with Mojang Codec objects.

This class simplifies encoding and decoding between Java objects and NBT (Tag) structures such as CompoundTag and ListTag. It also includes predefined codecs for common Minecraft types like Item and Color.

All methods are static and thread-safe.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.mojang.serialization.Codec<Color>
    A codec for serializing and deserializing Color objects to and from a 3-element integer array ([red, green, blue]).
    static final com.mojang.serialization.Codec<net.minecraft.world.item.Item>
    A codec for serializing and deserializing Item instances using their Identifier identifiers in the BuiltInRegistries.ITEM registry.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    decode(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.Tag tag)
    Decodes an object from the given NBT Tag using the provided Codec.
    static <T> Optional<T>
    decodeSafe(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.Tag tag)
    Decodes an object from NBT without throwing on failure.
    static <T> net.minecraft.nbt.Tag
    encode(com.mojang.serialization.Codec<T> codec, T value)
    Encodes an object into an NBT Tag using the provided Codec.
    static <T> List<T>
    fromListTag(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag)
    Decodes a ListTag into a Java List using the provided element codec.
    static <T> Optional<List<T>>
    fromListTagSafe(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag)
    Safely decodes a ListTag into a Java List, returning an Optional.
    static <K,V> Map<K,V>
    fromMapTag(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, net.minecraft.nbt.CompoundTag tag)
    Decodes a CompoundTag into a Map using codecs for both keys and values.
    static <K,V> Optional<Map<K,V>>
    fromMapTagSafe(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, net.minecraft.nbt.CompoundTag tag)
    Safely decodes a CompoundTag into a Map, returning an Optional.
    static <T> T
    fromTag(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag)
    Decodes a value from a CompoundTag created by toTag(Codec, Object).
    static <T> Optional<T>
    fromTagSafe(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag)
    Safely decodes a value from a CompoundTag, returning an Optional.
    static <T> net.minecraft.nbt.ListTag
    toListTag(com.mojang.serialization.Codec<T> elementCodec, List<T> list)
    Encodes a List of elements into a ListTag using the provided element codec.
    static <K,V> net.minecraft.nbt.CompoundTag
    toMapTag(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, Map<K,V> map)
    Encodes a Map into a CompoundTag using codecs for both keys and values.
    static <T> net.minecraft.nbt.CompoundTag
    toTag(com.mojang.serialization.Codec<T> codec, T value)
    Encodes a value into a CompoundTag.

    Methods inherited from class Object

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

    • ITEM_CODEC

      public static final com.mojang.serialization.Codec<net.minecraft.world.item.Item> ITEM_CODEC
      A codec for serializing and deserializing Item instances using their Identifier identifiers in the BuiltInRegistries.ITEM registry.
    • COLOR_CODEC

      public static final com.mojang.serialization.Codec<Color> COLOR_CODEC
      A codec for serializing and deserializing Color objects to and from a 3-element integer array ([red, green, blue]).

      The encoded form is a list of three integers between 0–255.

  • Constructor Details

    • CodecUtils

      public CodecUtils()
  • Method Details

    • encode

      public static <T> net.minecraft.nbt.Tag encode(com.mojang.serialization.Codec<T> codec, T value)
      Encodes an object into an NBT Tag using the provided Codec.

      If encoding fails, this method throws a CodecException.

      Type Parameters:
      T - the type of the object
      Parameters:
      codec - the codec used for encoding
      value - the object to encode
      Returns:
      the resulting NBT tag (maybe a CompoundTag, ListTag, or primitive)
      Throws:
      io.netty.handler.codec.CodecException - if encoding fails
    • decode

      public static <T> T decode(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.Tag tag)
      Decodes an object from the given NBT Tag using the provided Codec.

      If decoding fails, this method throws a CodecException.

      Type Parameters:
      T - the target object type
      Parameters:
      codec - the codec used for decoding
      tag - the NBT tag to decode
      Returns:
      the decoded object
      Throws:
      io.netty.handler.codec.CodecException - if decoding fails
    • decodeSafe

      public static <T> Optional<T> decodeSafe(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.Tag tag)
      Decodes an object from NBT without throwing on failure.

      Returns an Optional.empty() if decoding fails.

      Type Parameters:
      T - the target object type
      Parameters:
      codec - the codec used for decoding
      tag - the NBT tag to decode
      Returns:
      an Optional containing the decoded object if successful
    • toTag

      public static <T> net.minecraft.nbt.CompoundTag toTag(com.mojang.serialization.Codec<T> codec, T value)
      Encodes a value into a CompoundTag. If the encoded result is not already a compound, it is wrapped in one under the key "value".
      Type Parameters:
      T - the object type
      Parameters:
      codec - the codec used for encoding
      value - the object to encode
      Returns:
      a compound tag containing the encoded data
    • fromTag

      public static <T> T fromTag(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag)
      Decodes a value from a CompoundTag created by toTag(Codec, Object).

      Automatically unwraps the "value" field if present.

      Type Parameters:
      T - the target object type
      Parameters:
      codec - the codec used for decoding
      tag - the compound tag to decode
      Returns:
      the decoded object
    • fromTagSafe

      public static <T> Optional<T> fromTagSafe(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag)
      Safely decodes a value from a CompoundTag, returning an Optional.

      Automatically unwraps the "value" field if present.

      Type Parameters:
      T - the target object type
      Parameters:
      codec - the codec used for decoding
      tag - the compound tag to decode
      Returns:
      an Optional containing the decoded value if successful
    • toListTag

      public static <T> net.minecraft.nbt.ListTag toListTag(com.mojang.serialization.Codec<T> elementCodec, List<T> list)
      Encodes a List of elements into a ListTag using the provided element codec.
      Type Parameters:
      T - the element type
      Parameters:
      elementCodec - the codec for list elements
      list - the list of elements to encode
      Returns:
      the encoded list tag
    • fromListTag

      public static <T> List<T> fromListTag(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag)
      Decodes a ListTag into a Java List using the provided element codec.
      Type Parameters:
      T - the element type
      Parameters:
      elementCodec - the codec for list elements
      listTag - the list tag to decode
      Returns:
      the decoded list
      Throws:
      io.netty.handler.codec.CodecException - if decoding fails
    • fromListTagSafe

      public static <T> Optional<List<T>> fromListTagSafe(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag)
      Safely decodes a ListTag into a Java List, returning an Optional.
      Type Parameters:
      T - the element type
      Parameters:
      elementCodec - the codec for list elements
      listTag - the list tag to decode
      Returns:
      an Optional containing the decoded list if successful
    • toMapTag

      public static <K,V> net.minecraft.nbt.CompoundTag toMapTag(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, Map<K,V> map)
      Encodes a Map into a CompoundTag using codecs for both keys and values.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      keyCodec - the codec used to encode keys
      valueCodec - the codec used to encode values
      map - the map to encode
      Returns:
      a compound tag representing the encoded map
    • fromMapTag

      public static <K,V> Map<K,V> fromMapTag(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, net.minecraft.nbt.CompoundTag tag)
      Decodes a CompoundTag into a Map using codecs for both keys and values.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      keyCodec - the codec used to decode keys
      valueCodec - the codec used to decode values
      tag - the compound tag to decode
      Returns:
      the decoded map
      Throws:
      io.netty.handler.codec.CodecException - if decoding fails
    • fromMapTagSafe

      public static <K,V> Optional<Map<K,V>> fromMapTagSafe(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, net.minecraft.nbt.CompoundTag tag)
      Safely decodes a CompoundTag into a Map, returning an Optional.
      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      keyCodec - the codec used to decode keys
      valueCodec - the codec used to decode values
      tag - the compound tag to decode
      Returns:
      an Optional containing the decoded map if successful