Package org.moddingx.libx.codec
Class MoreCodecs
java.lang.Object
org.moddingx.libx.codec.MoreCodecs
Provides additional
codecs
.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec
<net.minecraft.util.Unit> A streamCodec for theUnit
constant that encodes to nothing. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Enum<T>>
com.mojang.serialization.Codec<T> Gets a streamCodec that encodes anenum
as a string.static <T> com.mojang.serialization.Codec
<T> Gets a streamCodec that always errors with the given message.static <T> com.mojang.serialization.Codec
<T> Gets a streamCodec that always errors with the given messages.static <M,
E> com.mojang.serialization.Codec <com.mojang.datafixers.util.Pair<M, E>> extend
(com.mojang.serialization.Codec<M> codec, com.mojang.serialization.MapCodec<E> extension) Extends the giveCodec
with some new fields defined by the givenMapCodec
.static <A,
M, E> com.mojang.serialization.Codec <A> extend
(com.mojang.serialization.Codec<M> codec, com.mojang.serialization.MapCodec<E> extension, Function<A, com.mojang.datafixers.util.Pair<M, E>> decompose, BiFunction<M, E, A> construct) Extends the giveCodec
with some new fields defined by the givenMapCodec
.static com.mojang.serialization.Codec
<net.minecraft.util.Unit> fixed
(com.mojang.serialization.Dynamic<?> value) Creates a fixed streamCodec that always encodes theunit value
to the given serialized value.static com.mojang.serialization.Codec
<net.minecraft.util.Unit> Creates a fixed streamCodec that always encodes theunit value
to the given serialized value.static com.mojang.serialization.Codec
<net.minecraft.util.Unit> Creates a fixed streamCodec that always encodes theunit value
to the given serialized value.static <T> com.mojang.serialization.Codec
<T> Lazily wraps the givenCodec
.static <A,
K, V> com.mojang.serialization.Codec <A> mapDispatch
(com.mojang.serialization.MapCodec<K> keyCodec, Function<K, com.mojang.serialization.DataResult<com.mojang.serialization.Codec<? extends V>>> valueCodecs, Function<A, com.mojang.datafixers.util.Pair<K, V>> decompose, BiFunction<K, V, com.mojang.serialization.DataResult<A>> construct) Creates a map dispatched streamCodec.static <T> com.mojang.serialization.Codec
<Optional<T>> option
(com.mojang.serialization.Codec<T> codec) Gets a streamCodec that encodes anOptional
with a given child streamCodec.static <T> com.mojang.serialization.MapCodec
<Optional<T>> optionalFieldOf
(com.mojang.serialization.Codec<T> codec, String name) Behaves the same asCodec.optionalFieldOf(String)
but instead of silently using the default value if decoding the element produces an error, propagates the error through.static <T> com.mojang.serialization.MapCodec
<T> optionalFieldOf
(com.mojang.serialization.Codec<T> codec, String name, T defaultValue) Behaves the same asCodec.optionalFieldOf(String, Object)
but instead of silently using the default value if decoding the element produces an error, propagates the error through.static <T> com.mojang.serialization.Codec
<T> typeMapped
(com.mojang.serialization.Codec<T> fallback, TypedEncoder<T, ?>... encoders) Gets a type mapped streamCodec that will try to encode and decode values with the first matchingTypedEncoder
.static <T> com.mojang.serialization.Codec
<T> typeMapped
(TypedEncoder<T, ?>... encoders) Gets a type mapped streamCodec that will try to encode and decode values with the first matchingTypedEncoder
.
-
Field Details
-
UNIT
public static final com.mojang.serialization.Codec<net.minecraft.util.Unit> UNITA streamCodec for theUnit
constant that encodes to nothing.
-
-
Constructor Details
-
MoreCodecs
public MoreCodecs()
-
-
Method Details
-
error
Gets a streamCodec that always errors with the given message. -
error
Gets a streamCodec that always errors with the given messages. -
option
public static <T> com.mojang.serialization.Codec<Optional<T>> option(com.mojang.serialization.Codec<T> codec) Gets a streamCodec that encodes anOptional
with a given child streamCodec. -
fixed
Creates a fixed streamCodec that always encodes theunit value
to the given serialized value. Decoding fille succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful ineither
-codecs. -
fixed
Creates a fixed streamCodec that always encodes theunit value
to the given serialized value. Decoding fille succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful ineither
-codecs. -
fixed
public static com.mojang.serialization.Codec<net.minecraft.util.Unit> fixed(com.mojang.serialization.Dynamic<?> value) Creates a fixed streamCodec that always encodes theunit value
to the given serialized value. Decoding will succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful ineither
-codecs. -
enumCodec
Gets a streamCodec that encodes anenum
as a string. -
extend
public static <M,E> com.mojang.serialization.Codec<com.mojang.datafixers.util.Pair<M,E>> extend(com.mojang.serialization.Codec<M> codec, com.mojang.serialization.MapCodec<E> extension) Extends the giveCodec
with some new fields defined by the givenMapCodec
. The given streamCodec must encode to aMapLike
. -
extend
public static <A,M, com.mojang.serialization.Codec<A> extendE> (com.mojang.serialization.Codec<M> codec, com.mojang.serialization.MapCodec<E> extension, Function<A, com.mojang.datafixers.util.Pair<M, E>> decompose, BiFunction<M, E, A> construct) Extends the giveCodec
with some new fields defined by the givenMapCodec
. The given streamCodec must encode to aMapLike
. -
mapDispatch
public static <A,K, com.mojang.serialization.Codec<A> mapDispatchV> (com.mojang.serialization.MapCodec<K> keyCodec, Function<K, com.mojang.serialization.DataResult<com.mojang.serialization.Codec<? extends V>>> valueCodecs, Function<A, com.mojang.datafixers.util.Pair<K, V>> decompose, BiFunction<K, V, com.mojang.serialization.DataResult<A>> construct) Creates a map dispatched streamCodec. When encoding an element, it ist first decomposed into key and value. The key is used to obtain a streamCodec to encode the value using the passedvalueCodecs
function. TheCodec
returned from that function must encode to aMapLike
. After that, the key is encoded and merged into theMapLike
from the value streamCodec. Decoding works the other way round in that the key is read first. Then thevalueCodecs
function is used to obtain aCodec
to decode the value. In the end, the streamCodec uses both key and value to construct the resulting element. Both theMapCodec
and the codecs returned fromvalueCodecs
must be able to work with additional values, they don't know about. -
lazy
public static <T> com.mojang.serialization.Codec<T> lazy(Supplier<com.mojang.serialization.Codec<T>> codec) Lazily wraps the givenCodec
. Useful when codecs need to reference each other to recurse. -
optionalFieldOf
public static <T> com.mojang.serialization.MapCodec<Optional<T>> optionalFieldOf(com.mojang.serialization.Codec<T> codec, String name) Behaves the same asCodec.optionalFieldOf(String)
but instead of silently using the default value if decoding the element produces an error, propagates the error through. The only way this yields an emptyOptional
is if the key is completely missing. -
optionalFieldOf
public static <T> com.mojang.serialization.MapCodec<T> optionalFieldOf(com.mojang.serialization.Codec<T> codec, String name, T defaultValue) Behaves the same asCodec.optionalFieldOf(String, Object)
but instead of silently using the default value if decoding the element produces an error, propagates the error through. The only way this yields an emptyOptional
is if the key is completely missing. -
typeMapped
@SafeVarargs public static <T> com.mojang.serialization.Codec<T> typeMapped(TypedEncoder<T, ?>... encoders) Gets a type mapped streamCodec that will try to encode and decode values with the first matchingTypedEncoder
. If noTypedEncoder
matches, an error will be returned. -
typeMapped
@SafeVarargs public static <T> com.mojang.serialization.Codec<T> typeMapped(com.mojang.serialization.Codec<T> fallback, TypedEncoder<T, ?>... encoders) Gets a type mapped streamCodec that will try to encode and decode values with the first matchingTypedEncoder
. If noTypedEncoder
matches, the fallback is used.
-