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 codec for theUnitconstant that encodes to nothing. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Enum<T>>
com.mojang.serialization.Codec<T> Gets a codec that encodes anenumas a string.static <T> com.mojang.serialization.Codec<T> Gets a codec that always errors with the given message.static <T> com.mojang.serialization.Codec<T> Gets a codec 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 giveCodecwith 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 giveCodecwith 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 codec that always encodes theunit valueto the given serialized value.static com.mojang.serialization.Codec<net.minecraft.util.Unit> Creates a fixed codec that always encodes theunit valueto the given serialized value.static com.mojang.serialization.Codec<net.minecraft.util.Unit> Creates a fixed codec that always encodes theunit valueto 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 codec.static <T> com.mojang.serialization.Codec<Optional<T>> option(com.mojang.serialization.Codec<T> codec) Gets a codec that encodes anOptionalwith a given child codec.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 codec 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 codec 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 codec for theUnitconstant that encodes to nothing.
-
-
Constructor Details
-
MoreCodecs
public MoreCodecs()
-
-
Method Details
-
error
Gets a codec that always errors with the given message. -
error
Gets a codec 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 codec that encodes anOptionalwith a given child codec. -
fixed
Creates a fixed codec that always encodes theunit valueto 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 codec that always encodes theunit valueto 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 codec that always encodes theunit valueto 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 codec that encodes anenumas 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 giveCodecwith some new fields defined by the givenMapCodec. The given codec 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 giveCodecwith some new fields defined by the givenMapCodec. The given codec 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 codec. When encoding an element, it ist first decomposed into key and value. The key is used to obtain a codec to encode the value using the passedvalueCodecsfunction. TheCodecreturned from that function must encode to aMapLike. After that, the key is encoded and merged into theMapLikefrom the value codec. Decoding works the other way round in that the key is read first. Then thevalueCodecsfunction is used to obtain aCodecto decode the value. In the end, the codec uses both key and value to construct the resulting element. Both theMapCodecand the codecs returned fromvalueCodecsmust 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 emptyOptionalis 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 emptyOptionalis if the key is completely missing. -
typeMapped
@SafeVarargs public static <T> com.mojang.serialization.Codec<T> typeMapped(TypedEncoder<T, ?>... encoders) Gets a type mapped codec that will try to encode and decode values with the first matchingTypedEncoder. If noTypedEncodermatches, 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 codec that will try to encode and decode values with the first matchingTypedEncoder. If noTypedEncodermatches, the fallback is used.
-