Interface Encoder<T extends @UnknownNullability Object>

Type Parameters:
T - the value type
All Known Subinterfaces:
Codec<T>, DataComponent<T>, StructCodec<R>
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Encoder<T extends @UnknownNullability Object>
Encoders are interfaces used in Codec which purpose is to encode any value of Encoder with a transcoder.
For example:

 record Name(String imTheBoss) { }
 Encoder<Name> encoder = new Encoder<>() {
     @Override
     public <D> Result<D> encode(Transcoder<D> coder, @Nullable Name value) {
         if (value == null) return new Result.Error<>("null");
         return new Result.Ok<>(coder.createString(value.imTheBoss()));
     }
 };
 Result<BinaryTag> result = encoder.encode(Transcoder.NBT, new Name("me")); // Result.OK(StringBinaryTag("me"))
 Result<BinaryTag> errorResult = encoder.encode(Transcoder.NBT, null); // Result.Error("null")
 
  • Method Details

    • empty

      static <T> Encoder<T> empty()
      Creates an empty encoder that only encodes null
      Type Parameters:
      T - the encoder type
      Returns:
      the empty encoder
    • encode

      <D> Result<D> encode(Transcoder<D> coder, @Nullable T value)
      Encodes a value of Encoder using the specific Transcoder
      The Result will be of Result.Ok or Result.Error and its typed Encoder
      Type Parameters:
      D - The resultant type
      Parameters:
      coder - the transcoder to use
      value - the value to encode
      Returns:
      the Result of the encoding with its type determined by the transcoder