Class NeoForgeExtraCodecs
Codec
-related helper functions that are not in ExtraCodecs
, but useful to NeoForge and other mods.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final record
private static class
private static final class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> com.mojang.serialization.MapCodec
<T> aliasedFieldOf
(com.mojang.serialization.Codec<T> codec, String... names) static <A> com.mojang.serialization.Codec
<A> decodeOnly
(com.mojang.serialization.Decoder<A> decoder) Creates a codec from a decoder.static <A,
E, B> com.mojang.serialization.MapCodec <com.mojang.datafixers.util.Either<E, B>> dispatchMapOrElse
(com.mojang.serialization.Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends com.mojang.serialization.MapCodec<? extends E>> codec, com.mojang.serialization.MapCodec<B> fallbackCodec) Map dispatch codec with an alternative.static <A,
E, B> com.mojang.serialization.MapCodec <com.mojang.datafixers.util.Either<E, B>> dispatchMapOrElse
(String key, com.mojang.serialization.Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends com.mojang.serialization.MapCodec<? extends E>> codec, com.mojang.serialization.MapCodec<B> fallbackCodec) Map dispatch codec with an alternative.static <A> com.mojang.serialization.Codec
<List<A>> listWithOptionalElements
(com.mojang.serialization.Codec<Optional<A>> elementCodec) Creates a codec for a list from a codec of optional elements.static <A> com.mojang.serialization.Codec
<List<A>> listWithoutEmpty
(com.mojang.serialization.Codec<List<Optional<A>>> codec) Creates a codec for a list of optional elements, that removes empty values when decoding.static <T> com.mojang.serialization.MapCodec
<T> mapWithAlternative
(com.mojang.serialization.MapCodec<T> mapCodec, com.mojang.serialization.MapCodec<? extends T> alternative) static <T> com.mojang.serialization.MapCodec
<T> optionalFieldAlwaysWrite
(com.mojang.serialization.Codec<T> codec, String name, T defaultValue) Similar toCodec.optionalFieldOf(String, Object)
, except that the default value is always written.static <T> com.mojang.serialization.Codec
<Set<T>> setOf
(com.mojang.serialization.Codec<T> codec) static <T> com.mojang.serialization.MapCodec
<Set<T>> singularOrPluralCodec
(com.mojang.serialization.Codec<T> codec, String singularName) static <T> com.mojang.serialization.MapCodec
<Set<T>> singularOrPluralCodec
(com.mojang.serialization.Codec<T> codec, String singularName, String pluralName) static <T> com.mojang.serialization.MapCodec
<Set<T>> singularOrPluralCodecNotEmpty
(com.mojang.serialization.Codec<T> codec, String singularName) static <T> com.mojang.serialization.MapCodec
<Set<T>> singularOrPluralCodecNotEmpty
(com.mojang.serialization.Codec<T> codec, String singularName, String pluralName) static <K,
V> com.mojang.serialization.Codec <Map<K, V>> unboundedMapAsList
(String keyName, com.mojang.serialization.Codec<K> keyCodec, String elementName, com.mojang.serialization.Codec<V> elementCodec) Creates a codec for an unbounded map whose underlying representation is a list of maps, with the given names for each key-element entry.static <T> com.mojang.serialization.Codec
<T> withAlternative
(com.mojang.serialization.Codec<T> codec, com.mojang.serialization.Codec<T> alternative) Codec with two alternatives.static <T> com.mojang.serialization.MapCodec
<T> withAlternative
(com.mojang.serialization.MapCodec<T> codec, com.mojang.serialization.MapCodec<T> alternative) MapCodec with two alternatives.static <F,
S> com.mojang.serialization.MapCodec <com.mojang.datafixers.util.Either<F, S>> xor
(com.mojang.serialization.MapCodec<F> first, com.mojang.serialization.MapCodec<S> second) Codec that matches exactly one out of two map codecs.
-
Constructor Details
-
NeoForgeExtraCodecs
public NeoForgeExtraCodecs()
-
-
Method Details
-
aliasedFieldOf
public static <T> com.mojang.serialization.MapCodec<T> aliasedFieldOf(com.mojang.serialization.Codec<T> codec, String... names) -
optionalFieldAlwaysWrite
public static <T> com.mojang.serialization.MapCodec<T> optionalFieldAlwaysWrite(com.mojang.serialization.Codec<T> codec, String name, T defaultValue) Similar toCodec.optionalFieldOf(String, Object)
, except that the default value is always written. -
mapWithAlternative
public static <T> com.mojang.serialization.MapCodec<T> mapWithAlternative(com.mojang.serialization.MapCodec<T> mapCodec, com.mojang.serialization.MapCodec<? extends T> alternative) -
singularOrPluralCodec
-
singularOrPluralCodec
-
singularOrPluralCodecNotEmpty
-
singularOrPluralCodecNotEmpty
-
setOf
public static <T> com.mojang.serialization.Codec<Set<T>> setOf(com.mojang.serialization.Codec<T> codec) -
decodeOnly
public static <A> com.mojang.serialization.Codec<A> decodeOnly(com.mojang.serialization.Decoder<A> decoder) Creates a codec from a decoder. The returned codec can only decode, and will throw on any attempt to encode. -
listWithOptionalElements
public static <A> com.mojang.serialization.Codec<List<A>> listWithOptionalElements(com.mojang.serialization.Codec<Optional<A>> elementCodec) Creates a codec for a list from a codec of optional elements. The empty optionals are removed from the list when decoding. -
listWithoutEmpty
public static <A> com.mojang.serialization.Codec<List<A>> listWithoutEmpty(com.mojang.serialization.Codec<List<Optional<A>>> codec) Creates a codec for a list of optional elements, that removes empty values when decoding. -
withAlternative
public static <T> com.mojang.serialization.Codec<T> withAlternative(com.mojang.serialization.Codec<T> codec, com.mojang.serialization.Codec<T> alternative) Codec with two alternatives.The vanilla
invalid reference
ExtraCodecs#withAlternative(Codec, Codec)
Unlike vanilla, this alternative codec also tries to encode with the second codec if the first encode fails.
- See Also:
-
withAlternative
public static <T> com.mojang.serialization.MapCodec<T> withAlternative(com.mojang.serialization.MapCodec<T> codec, com.mojang.serialization.MapCodec<T> alternative) MapCodec with two alternatives.mapWithAlternative(MapCodec, MapCodec)
will try the first codec and then the second codec for decoding, but only the first for encoding.Unlike
mapWithAlternative(MapCodec, MapCodec)
, this alternative codec also tries to encode with the second codec if the first encode fails. -
dispatchMapOrElse
public static <A,E, com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Either<E,B> B>> dispatchMapOrElse(com.mojang.serialization.Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends com.mojang.serialization.MapCodec<? extends E>> codec, com.mojang.serialization.MapCodec<B> fallbackCodec) Map dispatch codec with an alternative.The alternative will only be used if there is no
"type"
key in the serialized object.- Type Parameters:
A
- dispatch typeE
- dispatched typeB
- fallback type- Parameters:
typeCodec
- codec for the dispatch typetype
- function to retrieve the dispatch type from the dispatched typecodec
- function to retrieve the dispatched type map codec from the dispatch typefallbackCodec
- fallback to use when the deserialized object does not have a"type"
key
-
dispatchMapOrElse
public static <A,E, com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Either<E,B> B>> dispatchMapOrElse(String key, com.mojang.serialization.Codec<A> typeCodec, Function<? super E, ? extends A> type, Function<? super A, ? extends com.mojang.serialization.MapCodec<? extends E>> codec, com.mojang.serialization.MapCodec<B> fallbackCodec) Map dispatch codec with an alternative.The alternative will only be used if the provided key is not present in the serialized object.
- Type Parameters:
A
- dispatch typeE
- dispatched typeB
- fallback type- Parameters:
key
- key to dispatch ontypeCodec
- codec for the dispatch typetype
- function to retrieve the dispatch type from the dispatched typecodec
- function to retrieve the dispatched type map codec from the dispatch typefallbackCodec
- fallback to use when the deserialized object does not have a"type"
key
-
xor
public static <F,S> com.mojang.serialization.MapCodec<com.mojang.datafixers.util.Either<F,S>> xor(com.mojang.serialization.MapCodec<F> first, com.mojang.serialization.MapCodec<S> second) Codec that matches exactly one out of two map codecs. Same asCodec.xor(com.mojang.serialization.Codec<F>, com.mojang.serialization.Codec<S>)
but forMapCodec
s. -
unboundedMapAsList
public static <K,V> com.mojang.serialization.Codec<Map<K,V>> unboundedMapAsList(String keyName, com.mojang.serialization.Codec<K> keyCodec, String elementName, com.mojang.serialization.Codec<V> elementCodec) Creates a codec for an unbounded map whose underlying representation is a list of maps, with the given names for each key-element entry. Each key-element entry is encoded as a map with the given key and element names respectively.This is useful for maps where the key does not encode to a string, which causes errors when trying to serialize to a format that requires maps to have string keys (such as JSON and NBT).
- Type Parameters:
K
- the key typeV
- the element type- Parameters:
keyName
- the name of the key in the encoded map for each key-element entrykeyCodec
- codec for the keyelementName
- the name of the element in the encoded map for each key-element entryelementCodec
- codec for the element- Returns:
- a codec for an unbounded map
-