Class NeoForgeExtraCodecs

java.lang.Object
net.neoforged.neoforge.common.util.NeoForgeExtraCodecs

public class NeoForgeExtraCodecs extends Object
Codec-related helper functions that are not in ExtraCodecs, but useful to NeoForge and other mods.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final record 
     
    private static class 
     
    private static final class 
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <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 to Codec.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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 to Codec.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

      public static <T> com.mojang.serialization.MapCodec<Set<T>> singularOrPluralCodec(com.mojang.serialization.Codec<T> codec, String singularName)
    • singularOrPluralCodec

      public static <T> com.mojang.serialization.MapCodec<Set<T>> singularOrPluralCodec(com.mojang.serialization.Codec<T> codec, String singularName, String pluralName)
    • singularOrPluralCodecNotEmpty

      public static <T> com.mojang.serialization.MapCodec<Set<T>> singularOrPluralCodecNotEmpty(com.mojang.serialization.Codec<T> codec, String singularName)
    • singularOrPluralCodecNotEmpty

      public static <T> com.mojang.serialization.MapCodec<Set<T>> singularOrPluralCodecNotEmpty(com.mojang.serialization.Codec<T> codec, String singularName, String pluralName)
    • 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)
      will try the first codec and then the second codec for decoding, but only the first for encoding.

      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, 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.

      The alternative will only be used if there is no "type" key in the serialized object.

      Type Parameters:
      A - dispatch type
      E - dispatched type
      B - fallback type
      Parameters:
      typeCodec - codec for the dispatch type
      type - function to retrieve the dispatch type from the dispatched type
      codec - function to retrieve the dispatched type map codec from the dispatch type
      fallbackCodec - 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 as Codec.xor(com.mojang.serialization.Codec<F>, com.mojang.serialization.Codec<S>) but for MapCodecs.