Class MoreCodecs

java.lang.Object
org.moddingx.libx.codec.MoreCodecs

public class MoreCodecs extends Object
Provides additional codecs.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.mojang.serialization.Codec<net.minecraft.world.item.ItemStack>
    A Codec for item 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 the Unit constant that encodes to nothing.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends Enum<T>>
    com.mojang.serialization.Codec<T>
    enumCodec(Class<T> clazz)
    Gets a codec that encodes an enum 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>
    error(String encodeMsg, String decodeMsg)
    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 give Codec with some new fields defined by the given MapCodec.
    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 give Codec with some new fields defined by the given MapCodec.
    static com.mojang.serialization.Codec<net.minecraft.util.Unit>
    fixed(com.mojang.serialization.Dynamic<?> value)
    Creates a fixed codec that always encodes the unit value to the given serialized value.
    static com.mojang.serialization.Codec<net.minecraft.util.Unit>
    fixed(Number value)
    Creates a fixed codec that always encodes the unit value to the given serialized value.
    static com.mojang.serialization.Codec<net.minecraft.util.Unit>
    fixed(String value)
    Creates a fixed codec that always encodes the unit value to the given serialized value.
    static <T> com.mojang.serialization.Codec<T>
    lazy(Supplier<com.mojang.serialization.Codec<T>> codec)
    Lazily wraps the given Codec.
    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 an Optional 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 as Codec.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 as Codec.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 matching TypedEncoder.
    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 matching TypedEncoder.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNIT

      public static final com.mojang.serialization.Codec<net.minecraft.util.Unit> UNIT
      A codec for the Unit constant that encodes to nothing.
    • SAFE_ITEM_STACK

      public static final com.mojang.serialization.Codec<net.minecraft.world.item.ItemStack> SAFE_ITEM_STACK
      A Codec for item 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

      public static <T> com.mojang.serialization.Codec<T> error(String msg)
      Gets a codec that always errors with the given message.
    • error

      public static <T> com.mojang.serialization.Codec<T> error(String encodeMsg, String decodeMsg)
      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 an Optional with a given child codec.
    • fixed

      public static com.mojang.serialization.Codec<net.minecraft.util.Unit> fixed(Number value)
      Creates a fixed codec that always encodes the unit value to the given serialized value. Decoding fille succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful in either-codecs.
    • fixed

      public static com.mojang.serialization.Codec<net.minecraft.util.Unit> fixed(String value)
      Creates a fixed codec that always encodes the unit value to the given serialized value. Decoding fille succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful in either-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 the unit value to the given serialized value. Decoding fille succeed if the serialized value equals the given value, otherwise it fails. Fixed codecs are useful in either-codecs.
    • enumCodec

      public static <T extends Enum<T>> com.mojang.serialization.Codec<T> enumCodec(Class<T> clazz)
      Gets a codec that encodes an enum 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 give Codec with some new fields defined by the given MapCodec. The given codec must encode to a MapLike.
    • extend

      public 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 give Codec with some new fields defined by the given MapCodec. The given codec must encode to a MapLike.
    • mapDispatch

      public 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. 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 passed valueCodecs function. The Codec returned from that function must encode to a MapLike. After that, the key is encoded and merged into the MapLike from the value codec. Decoding works the other way round in that the key is read first. Then the valueCodecs function is used to obtain a Codec to decode the value. In the end, the codec uses both key and value to construct the resulting element. Both the MapCodec and the codecs returned from valueCodecs 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 given Codec. 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 as Codec.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 empty Optional 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 as Codec.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 empty Optional 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 matching TypedEncoder. If no TypedEncoder 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 matching TypedEncoder. If no TypedEncoder matches, the fallback is used.