Class CodecUtils
java.lang.Object
net.ixdarklord.coolcatcore.api.utils.CodecUtils
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
FieldsModifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec<Color> A codec for serializing and deserializingColorobjects 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 deserializingIteminstances using theirIdentifieridentifiers in theBuiltInRegistries.ITEMregistry. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tdecode(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.Tag tag) Decodes an object from the given NBTTagusing the providedCodec.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.Tagencode(com.mojang.serialization.Codec<T> codec, T value) Encodes an object into an NBTTagusing the providedCodec.static <T> List<T> fromListTag(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag) Decodes aListTaginto a JavaListusing the provided element codec.fromListTagSafe(com.mojang.serialization.Codec<T> elementCodec, net.minecraft.nbt.ListTag listTag) 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 aCompoundTaginto aMapusing codecs for both keys and values.fromMapTagSafe(com.mojang.serialization.Codec<K> keyCodec, com.mojang.serialization.Codec<V> valueCodec, net.minecraft.nbt.CompoundTag tag) static <T> TfromTag(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag) Decodes a value from aCompoundTagcreated bytoTag(Codec, Object).static <T> Optional<T> fromTagSafe(com.mojang.serialization.Codec<T> codec, net.minecraft.nbt.CompoundTag tag) Safely decodes a value from aCompoundTag, returning anOptional.static <T> net.minecraft.nbt.ListTagEncodes aListof elements into aListTagusing 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 aMapinto aCompoundTagusing codecs for both keys and values.static <T> net.minecraft.nbt.CompoundTagtoTag(com.mojang.serialization.Codec<T> codec, T value) Encodes a value into aCompoundTag.
-
Field Details
-
ITEM_CODEC
public static final com.mojang.serialization.Codec<net.minecraft.world.item.Item> ITEM_CODECA codec for serializing and deserializingIteminstances using theirIdentifieridentifiers in theBuiltInRegistries.ITEMregistry. -
COLOR_CODEC
-
-
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 NBTTagusing the providedCodec.If encoding fails, this method throws a
CodecException.- Type Parameters:
T- the type of the object- Parameters:
codec- the codec used for encodingvalue- 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 NBTTagusing the providedCodec.If decoding fails, this method throws a
CodecException.- Type Parameters:
T- the target object type- Parameters:
codec- the codec used for decodingtag- 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 decodingtag- the NBT tag to decode- Returns:
- an
Optionalcontaining 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 aCompoundTag. 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 encodingvalue- 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 aCompoundTagcreated bytoTag(Codec, Object).Automatically unwraps the
"value"field if present.- Type Parameters:
T- the target object type- Parameters:
codec- the codec used for decodingtag- 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 aCompoundTag, returning anOptional.Automatically unwraps the
"value"field if present.- Type Parameters:
T- the target object type- Parameters:
codec- the codec used for decodingtag- the compound tag to decode- Returns:
- an
Optionalcontaining the decoded value if successful
-
toListTag
public static <T> net.minecraft.nbt.ListTag toListTag(com.mojang.serialization.Codec<T> elementCodec, List<T> list) Encodes aListof elements into aListTagusing the provided element codec.- Type Parameters:
T- the element type- Parameters:
elementCodec- the codec for list elementslist- 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 aListTaginto a JavaListusing the provided element codec.- Type Parameters:
T- the element type- Parameters:
elementCodec- the codec for list elementslistTag- 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) - Type Parameters:
T- the element type- Parameters:
elementCodec- the codec for list elementslistTag- the list tag to decode- Returns:
- an
Optionalcontaining 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 aMapinto aCompoundTagusing codecs for both keys and values.- Type Parameters:
K- the key typeV- the value type- Parameters:
keyCodec- the codec used to encode keysvalueCodec- the codec used to encode valuesmap- 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 aCompoundTaginto aMapusing codecs for both keys and values.- Type Parameters:
K- the key typeV- the value type- Parameters:
keyCodec- the codec used to decode keysvalueCodec- the codec used to decode valuestag- 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) - Type Parameters:
K- the key typeV- the value type- Parameters:
keyCodec- the codec used to decode keysvalueCodec- the codec used to decode valuestag- the compound tag to decode- Returns:
- an
Optionalcontaining the decoded map if successful
-