Class NeoForgeExtraCodecs
java.lang.Object
net.neoforged.neoforge.common.util.NeoForgeExtraCodecs
Codec
-related helper functions that are not in ExtraCodecs
, but useful to NeoForge and other mods.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static final record
private static class
private static final class
-
Constructor Summary
-
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> 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 <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
-
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.
-