Interface TypedEncoder<T,R>


public interface TypedEncoder<T,R>
An encoder that can encode and decode elements of a specific type. This is used to create type mapped codecs that behave differently depending on the DynamicOps they are used on. A TypedEncoder is assumed to be able to encode and decode with every dynamic ops whose empty element is a subclass of the class returned by resultClass().
  • Method Summary

    Modifier and Type
    Method
    Description
    default <N> TypedEncoder<T,N>
    cast(com.mojang.serialization.DynamicOps<N> ops)
    Casts this TypedEncoder given some DynamicOps.
    com.mojang.serialization.DataResult<T>
    decode(R value)
    Decodes a value.
    com.mojang.serialization.DataResult<R>
    encode(T value)
    Encodes a value.
    static <T, R> TypedEncoder<T,R>
    of(Class<R> resultClass, Function<? super T,? extends R> encoder, Function<? super R,? extends T> decoder)
    Creates a new TypedEncoder given two functions to encode and decode an object.
    Gets the class of elements this TypedEncoder can encode to.
  • Method Details

    • resultClass

      Class<R> resultClass()
      Gets the class of elements this TypedEncoder can encode to.
    • encode

      com.mojang.serialization.DataResult<R> encode(T value)
      Encodes a value.
    • decode

      com.mojang.serialization.DataResult<T> decode(R value)
      Decodes a value.
    • cast

      @Nullable default <N> TypedEncoder<T,N> cast(com.mojang.serialization.DynamicOps<N> ops)
      Casts this TypedEncoder given some DynamicOps. Returns null if the TypedEncoder can't be used on the given DynamicOps. This can be overridden to better which DynamicOps an encoder accepts.
    • of

      static <T, R> TypedEncoder<T,R> of(Class<R> resultClass, Function<? super T,? extends R> encoder, Function<? super R,? extends T> decoder)
      Creates a new TypedEncoder given two functions to encode and decode an object. If a functions throws an exception, it is wrapped into an errored DataResult.