Interface PacketCodec<B,V>

Type Parameters:
B - the type of the buffer; RegistryByteBuf for play-phase packets, PacketByteBuf for other phases (like configuration)
V - the type of the value to be encoded/decoded
All Superinterfaces:
PacketDecoder<B,V>, PacketEncoder<B,V>
All Known Implementing Classes:
PacketCodecDispatcher

public interface PacketCodec<B,V> extends PacketDecoder<B,V>, PacketEncoder<B,V>
A codec that is used for serializing a packet.

Packet codecs serialize to, and deserialize from, PacketByteBuf, which is a stream of data. To integrate the classic PacketByteBuf-based code, use of(ValueFirstEncoder, PacketDecoder) like this:


 public static final PacketCodec<PacketByteBuf, MyPacket> CODEC = PacketCodec.of(MyPacket::write, MyPacket::new);

 private MyPacket(PacketByteBuf buf) {
 	this.text = buf.readString();
 }

 private void write(PacketByteBuf buf) {
 	buf.writeString(this.text);
 }
 

While this serves similar functions as codecs in the DataFixerUpper library, the two are wholly separate and DataFixerUpper methods cannot be used with this. However, a packet codec may reference a regular codec by using PacketCodecs.codec(com.mojang.serialization.Codec<T>), which serializes the data to NBT.

See PacketCodecs for codecs to serialize various objects.

Mappings:
Namespace Name
named net/minecraft/network/codec/PacketCodec
intermediary net/minecraft/class_9139
official yw
  • Method Details

    • ofStatic

      static <B, V> PacketCodec<B,V> ofStatic(PacketEncoder<B,V> encoder, PacketDecoder<B,V> decoder)
      Returns a packet codec from the encoder and decoder.
      Returns:
      a packet codec from the encoder and decoder
      API Note:
      This is useful for integrating with code that uses static methods for packet writing, where the buffer is the first argument, like static void write(PacketByteBuf buf, Data data). For code that uses instance methods like void write(PacketByteBuf buf), use of(ValueFirstEncoder, PacketDecoder).
      Mappings:
      Namespace Name Mixin selector
      named ofStatic Lnet/minecraft/network/codec/PacketCodec;ofStatic(Lnet/minecraft/network/codec/PacketEncoder;Lnet/minecraft/network/codec/PacketDecoder;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56437 Lnet/minecraft/class_9139;method_56437(Lnet/minecraft/class_9142;Lnet/minecraft/class_9141;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyy;Lyx;)Lyw;
    • of

      static <B, V> PacketCodec<B,V> of(ValueFirstEncoder<B,V> encoder, PacketDecoder<B,V> decoder)
      Returns a packet codec from the encoder and decoder.
      Returns:
      a packet codec from the encoder and decoder
      API Note:
      This is useful for integrating with code that uses instance methods for packet writing, like void write(PacketByteBuf buf). For code that uses static methods like static void write(PacketByteBuf buf, Data data), where the buffer is the first argument, use ofStatic(PacketEncoder, PacketDecoder).
      Mappings:
      Namespace Name Mixin selector
      named of Lnet/minecraft/network/codec/PacketCodec;of(Lnet/minecraft/network/codec/ValueFirstEncoder;Lnet/minecraft/network/codec/PacketDecoder;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56438 Lnet/minecraft/class_9139;method_56438(Lnet/minecraft/class_9143;Lnet/minecraft/class_9141;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyz;Lyx;)Lyw;
    • unit

      static <B, V> PacketCodec<B,V> unit(V value)
      Returns a codec that always returns value.

      This does not encode anything. Instead, it throws IllegalStateException when the value does not equal value. This comparison is made with equals(), not reference equality (==).

      Returns:
      a codec that always returns value
      Mappings:
      Namespace Name Mixin selector
      named unit Lnet/minecraft/network/codec/PacketCodec;unit(Ljava/lang/Object;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56431 Lnet/minecraft/class_9139;method_56431(Ljava/lang/Object;)Lnet/minecraft/class_9139;
      official a Lyw;a(Ljava/lang/Object;)Lyw;
    • collect

      default <O> PacketCodec<B,O> collect(PacketCodec.ResultFunction<B,V,O> function)
      Returns the result mapped with function.

      For example, passing PacketCodecs::optional makes the value optional. Additionally, this method can be used like Stream Collectors - hence its name. For example, to make a codec for a list of something, write parentCodec.collect(PacketCodecs.toList()).

      Returns:
      the result mapped with function
      See Also:
      Mappings:
      Namespace Name Mixin selector
      named collect Lnet/minecraft/network/codec/PacketCodec;collect(Lnet/minecraft/network/codec/PacketCodec$ResultFunction;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56433 Lnet/minecraft/class_9139;method_56433(Lnet/minecraft/class_9139$class_9140;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw$a;)Lyw;
    • xmap

      default <O> PacketCodec<B,O> xmap(Function<? super V,? extends O> to, Function<? super O,? extends V> from)
      Returns a codec that maps its encode input and decode output with from and to, respectively.

      This can be used to transform a codec for a simple value (like a string) into a corresponding, more complex value (like an identifier). An example:

      
       public static final PacketCodec<ByteBuf, Identifier> PACKET_CODEC = PacketCodecs.STRING.xmap(Identifier::new, Identifier::toString);
       
      Returns:
      a codec that maps its encode input and decode output with from and to, respectively
      Mappings:
      Namespace Name Mixin selector
      named xmap Lnet/minecraft/network/codec/PacketCodec;xmap(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56432 Lnet/minecraft/class_9139;method_56432(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;
      official a Lyw;a(Ljava/util/function/Function;Ljava/util/function/Function;)Lyw;
    • mapBuf

      default <O extends ByteBuf> PacketCodec<O,V> mapBuf(Function<O,? extends B> function)
      Mappings:
      Namespace Name Mixin selector
      named mapBuf Lnet/minecraft/network/codec/PacketCodec;mapBuf(Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56439 Lnet/minecraft/class_9139;method_56439(Ljava/util/function/Function;)Lnet/minecraft/class_9139;
      official b Lyw;b(Ljava/util/function/Function;)Lyw;
    • dispatch

      default <U> PacketCodec<B,U> dispatch(Function<? super U,? extends V> type, Function<? super V,? extends PacketCodec<? super B,? extends U>> codec)
      Returns a codec that dispatches one of the sub-codecs based on the type.

      For example, subtypes of Stat requires different values to be serialized, yet it makes sense to use the same codec for all stats. This method should be called on the codec for the "type" - like StatType. An example:

      
       public static final PacketCodec<RegistryByteBuf, Thing<?>> PACKET_CODEC = PacketCodecs.registryValue(RegistryKeys.THING_TYPE).dispatch(Thing::getType, ThingType::getPacketCodec);
       
      Parameters:
      type - a function that, given a value, returns its "type"
      codec - a function that, given a "type", returns the codec for encoding/decoding the value
      Returns:
      a codec that dispatches one of the sub-codecs based on the type
      Mappings:
      Namespace Name Mixin selector
      named dispatch Lnet/minecraft/network/codec/PacketCodec;dispatch(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56440 Lnet/minecraft/class_9139;method_56440(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;
      official b Lyw;b(Ljava/util/function/Function;Ljava/util/function/Function;)Lyw;
    • tuple

      static <B, C, T1> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec, Function<C,T1> from, Function<T1,C> to)
      Returns a codec for encoding one value.
      Returns:
      a codec for encoding one value
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56434 Lnet/minecraft/class_9139;method_56434(Lnet/minecraft/class_9139;Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Ljava/util/function/Function;)Lyw;
    • tuple

      static <B, C, T1, T2> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec1, Function<C,T1> from1, PacketCodec<? super B,T2> codec2, Function<C,T2> from2, BiFunction<T1,T2,C> to)
      Returns a codec for encoding two values.
      Returns:
      a codec for encoding two values
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Ljava/util/function/BiFunction;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56435 Lnet/minecraft/class_9139;method_56435(Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Ljava/util/function/BiFunction;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Ljava/util/function/BiFunction;)Lyw;
    • tuple

      static <B, C, T1, T2, T3> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec1, Function<C,T1> from1, PacketCodec<? super B,T2> codec2, Function<C,T2> from2, PacketCodec<? super B,T3> codec3, Function<C,T3> from3, com.mojang.datafixers.util.Function3<T1,T2,T3,C> to)
      Returns a codec for encoding three values.
      Returns:
      a codec for encoding three values
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function3;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56436 Lnet/minecraft/class_9139;method_56436(Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function3;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function3;)Lyw;
    • tuple

      static <B, C, T1, T2, T3, T4> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec1, Function<C,T1> from1, PacketCodec<? super B,T2> codec2, Function<C,T2> from2, PacketCodec<? super B,T3> codec3, Function<C,T3> from3, PacketCodec<? super B,T4> codec4, Function<C,T4> from4, com.mojang.datafixers.util.Function4<T1,T2,T3,T4,C> to)
      Returns a codec for encoding four values.
      Returns:
      a codec for encoding four values
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56905 Lnet/minecraft/class_9139;method_56905(Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;)Lyw;
    • tuple

      static <B, C, T1, T2, T3, T4, T5> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec1, Function<C,T1> from1, PacketCodec<? super B,T2> codec2, Function<C,T2> from2, PacketCodec<? super B,T3> codec3, Function<C,T3> from3, PacketCodec<? super B,T4> codec4, Function<C,T4> from4, PacketCodec<? super B,T5> codec5, Function<C,T5> from5, com.mojang.datafixers.util.Function5<T1,T2,T3,T4,T5,C> to)
      Returns a codec for encoding five values.
      Returns:
      a codec for encoding five values
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function5;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56906 Lnet/minecraft/class_9139;method_56906(Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function5;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function5;)Lyw;
    • tuple

      static <B, C, T1, T2, T3, T4, T5, T6> PacketCodec<B,C> tuple(PacketCodec<? super B,T1> codec1, Function<C,T1> from1, PacketCodec<? super B,T2> codec2, Function<C,T2> from2, PacketCodec<? super B,T3> codec3, Function<C,T3> from3, PacketCodec<? super B,T4> codec4, Function<C,T4> from4, PacketCodec<? super B,T5> codec5, Function<C,T5> from5, PacketCodec<? super B,T6> codec6, Function<C,T6> from6, com.mojang.datafixers.util.Function6<T1,T2,T3,T4,T5,T6,C> to)
      Returns a codec for encoding six values.
      Returns:
      a codec for encoding six values
      Mappings:
      Namespace Name Mixin selector
      named tuple Lnet/minecraft/network/codec/PacketCodec;tuple(Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lnet/minecraft/network/codec/PacketCodec;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function6;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_58025 Lnet/minecraft/class_9139;method_58025(Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lnet/minecraft/class_9139;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function6;)Lnet/minecraft/class_9139;
      official a Lyw;a(Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lyw;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function6;)Lyw;
    • recursive

      static <B, T> PacketCodec<B,T> recursive(UnaryOperator<PacketCodec<B,T>> codecGetter)
      Mappings:
      Namespace Name Mixin selector
      named recursive Lnet/minecraft/network/codec/PacketCodec;recursive(Ljava/util/function/UnaryOperator;)Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_58024 Lnet/minecraft/class_9139;method_58024(Ljava/util/function/UnaryOperator;)Lnet/minecraft/class_9139;
      official a Lyw;a(Ljava/util/function/UnaryOperator;)Lyw;
    • cast

      default <S extends B> PacketCodec<S,V> cast()
      Returns the same codec, casted to work with buffers of type S.
      Returns:
      the same codec, casted to work with buffers of type S
      API Note:
      For example, BlockPos.PACKET_CODEC is defined as PacketCodec<ByteBuf, BlockPos>. To use this codec where PacketByteBuf is expected, you can call this method for easy casting, like: PACKET_CODEC.cast(). Doing this is generally safe and will not result in exceptions.
      Mappings:
      Namespace Name Mixin selector
      named cast Lnet/minecraft/network/codec/PacketCodec;cast()Lnet/minecraft/network/codec/PacketCodec;
      intermediary method_56430 Lnet/minecraft/class_9139;method_56430()Lnet/minecraft/class_9139;
      official a Lyw;a()Lyw;