Package org.moddingx.libx.codec
Class MoreCodecs
java.lang.Object
org.moddingx.libx.codec.MoreCodecs
Provides additional
codecs
.-
Field Summary
Modifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec<net.minecraft.world.item.ItemStack>
ACodec
foritem stacks
that will encode the stack as NBT when using NBT dynamic ops, as recipe JSON when using JSON dynamic ops and as a string containing the NBT tag if using some other dynamic ops.static final com.mojang.serialization.Codec<net.minecraft.util.Unit>
A codec for theUnit
constant that encodes to nothing. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Enum<T>>
com.mojang.serialization.Codec<T>Gets a codec that encodes anenum
as 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 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 codec that always encodes theunit value
to the given serialized value.static com.mojang.serialization.Codec<net.minecraft.util.Unit>
Creates a fixed codec that always encodes theunit value
to the given serialized value.static com.mojang.serialization.Codec<net.minecraft.util.Unit>
Creates a fixed codec 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 codec.static <T> com.mojang.serialization.Codec<Optional<T>>
option
(com.mojang.serialization.Codec<T> codec) Gets a codec that encodes anOptional
with 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 theUnit
constant that encodes to nothing. -
SAFE_ITEM_STACK
public static final com.mojang.serialization.Codec<net.minecraft.world.item.ItemStack> SAFE_ITEM_STACKACodec
foritem stacks
that will encode the stack as NBT when using NBT dynamic ops, as recipe JSON when using JSON dynamic ops and as a string containing the NBT tag if using some other dynamic ops.
-
-
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 anOptional
with a given child codec. -
fixed
Creates a fixed codec 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 codec 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 codec 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. -
enumCodec
Gets a codec 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 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 giveCodec
with 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 passedvalueCodecs
function. TheCodec
returned from that function must encode to aMapLike
. After that, the key is encoded and merged into theMapLike
from the value codec. 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 codec 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 codec 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 codec that will try to encode and decode values with the first matchingTypedEncoder
. If noTypedEncoder
matches, the fallback is used.
-