Class KeyDispatchCodec<K,V>

java.lang.Object
com.mojang.serialization.CompressorHolder
com.mojang.serialization.MapCodec<V>
com.mojang.serialization.codecs.KeyDispatchCodec<K,V>
Type Parameters:
K - the type of the key
V - the type of the value
All Implemented Interfaces:
Compressable, Keyable, MapDecoder<V>, MapEncoder<V>

public class KeyDispatchCodec<K,V> extends MapCodec<V>
Delegate codecs allow using the value of an initially decoded key to determine how to decode the rest of the input. On encoding, the behaviour is similar, with the dispatch codec finding a codec from the supplied type, using that to encode the value and using the receiver to encode the key.

Note: codecs constructed from a MapCodec.MapCodecCodec have special behaviour compared to other codecs; while other codecs, act on a "value field within the structure that the dispatch codec is decoding, a MapCodec.MapCodecCodec that has been delegated to (such as the output of MapCodec.codec() or RecordCodecBuilder.create(Function) will instead decode and encode to the root level of the data. This means that if you want this behaviour, you should make sure that your codec is a MapCodec.MapCodecCodec - which means using the MapCodec specific xmap/flatXmap methods instead of the ones on Codec in any transformations you might do! This behaviour can be disabled by setting the assumeMap argument of the constructor to true, in which case every delegated codec will attempt to decode the full input object. Also of note is that this behaviour is lost when using a DynamicOps with DynamicOps.compressMaps(), in which case the delegated codec is always encoded in a value field.

Note: On encoding the function fed into the type argument of both constructors is used to find a key from an object, and that key is then used to find the encoder using the function fed into the constructor; however, there is no guarantee that the capture of ? super E fed into the first function is the same capture that comes out of the second! In other words, this method is not inherently typesafe. Take care that whatever functions you provide only involve a single capture of that generic for a given key; a common approach is using the delegated codec itself as the key.

  • Constructor Details

  • Method Details

    • unsafe

      public static <K, V> KeyDispatchCodec<K,V> unsafe(String typeKey, Codec<K> keyCodec, Function<? super V,? extends DataResult<? extends K>> type, Function<? super K,? extends DataResult<? extends Decoder<? extends V>>> decoder, Function<? super V,? extends DataResult<? extends Encoder<V>>> encoder)
      Mojang-Added Docs:

      Will assume that the result of all elements is a map

    • decode

      public <T> DataResult<V> decode(DynamicOps<T> ops, MapLike<T> input)
    • encode

      public <T> RecordBuilder<T> encode(V input, DynamicOps<T> ops, RecordBuilder<T> prefix)
    • keys

      public <T> Stream<T> keys(DynamicOps<T> ops)
      Description copied from class: MapCodec
      Returns a stream of keys this map codec may write or read. This stream should contain any key that may be written or read, even ones that are optional. The codec need not read or write all of these keys on any given occasion, but all keys that might be read or written should be included, as this stream is used to form the number-based indexing used with DynamicOps.compressMaps().

      Note: if a map codec writes a key that is not returned by this method, it will not work correctly with a DynamicOps that has DynamicOps.compressMaps().

      Specified by:
      keys in interface Keyable
      Specified by:
      keys in class MapCodec<V>
      Type Parameters:
      T - the data type to encode keys to or decode keys from
      Parameters:
      ops - the dynamic ops instance
      Returns:
      a stream of keys this map codec may write or read
    • toString

      public String toString()
      Overrides:
      toString in class Object