Interface PacketCodec<B,V>
- Type Parameters:
B- the type of the buffer;RegistryByteBuffor play-phase packets,PacketByteBuffor 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
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/PacketCodecintermediary net/minecraft/class_9139official yx
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptiondefault <S extends B>
PacketCodec<S, V> cast()Returns the same codec, casted to work with buffers of typeS.default <O> PacketCodec<B, O> collect(PacketCodec.ResultFunction<B, V, O> function) Returns the result mapped withfunction.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.default <O extends ByteBuf>
PacketCodec<O, V> static <B,V> PacketCodec <B, V> of(ValueFirstEncoder<B, V> encoder, PacketDecoder<B, V> decoder) Returns a packet codec from theencoderanddecoder.static <B,V> PacketCodec <B, V> ofStatic(PacketEncoder<B, V> encoder, PacketDecoder<B, V> decoder) Returns a packet codec from theencoderanddecoder.static <B,T> PacketCodec <B, T> recursive(UnaryOperator<PacketCodec<B, T>> codecGetter) 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.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.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.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.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.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.static <B,V> PacketCodec <B, V> unit(V value) Returns a codec that always returnsvalue.default <O> PacketCodec<B, O> Returns a codec that maps its encode input and decode output withfromandto, respectively.Methods inherited from interface net.minecraft.network.codec.PacketDecoder
decodeMethods inherited from interface net.minecraft.network.codec.PacketEncoder
encode
-
Method Details
-
ofStatic
Returns a packet codec from theencoderanddecoder.- Returns:
- a packet codec from the
encoderanddecoder - 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 likevoid write(PacketByteBuf buf), useof(ValueFirstEncoder, PacketDecoder). - Mappings:
Namespace Name Mixin selector named ofStaticLnet/minecraft/network/codec/PacketCodec;ofStatic(Lnet/minecraft/network/codec/PacketEncoder;Lnet/minecraft/network/codec/PacketDecoder;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56437Lnet/minecraft/class_9139;method_56437(Lnet/minecraft/class_9142;Lnet/minecraft/class_9141;)Lnet/minecraft/class_9139;official aLyx;a(Lyz;Lyy;)Lyx;
-
of
Returns a packet codec from theencoderanddecoder.- Returns:
- a packet codec from the
encoderanddecoder - 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 likestatic void write(PacketByteBuf buf, Data data), where the buffer is the first argument, useofStatic(PacketEncoder, PacketDecoder). - Mappings:
Namespace Name Mixin selector named ofLnet/minecraft/network/codec/PacketCodec;of(Lnet/minecraft/network/codec/ValueFirstEncoder;Lnet/minecraft/network/codec/PacketDecoder;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56438Lnet/minecraft/class_9139;method_56438(Lnet/minecraft/class_9143;Lnet/minecraft/class_9141;)Lnet/minecraft/class_9139;official aLyx;a(Lza;Lyy;)Lyx;
-
unit
Returns a codec that always returnsvalue.This does not encode anything. Instead, it throws
IllegalStateExceptionwhen the value does not equalvalue. This comparison is made withequals(), not reference equality (==).- Returns:
- a codec that always returns
value - Mappings:
Namespace Name Mixin selector named unitLnet/minecraft/network/codec/PacketCodec;unit(Ljava/lang/Object;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56431Lnet/minecraft/class_9139;method_56431(Ljava/lang/Object;)Lnet/minecraft/class_9139;official aLyx;a(Ljava/lang/Object;)Lyx;
-
collect
Returns the result mapped withfunction.For example, passing
PacketCodecs::optionalmakes the value optional. Additionally, this method can be used like StreamCollectors- hence its name. For example, to make a codec for a list of something, writeparentCodec.collect(PacketCodecs.toList()).- Returns:
- the result mapped with
function - See Also:
- Mappings:
Namespace Name Mixin selector named collectLnet/minecraft/network/codec/PacketCodec;collect(Lnet/minecraft/network/codec/PacketCodec$ResultFunction;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56433Lnet/minecraft/class_9139;method_56433(Lnet/minecraft/class_9139$class_9140;)Lnet/minecraft/class_9139;official aLyx;a(Lyx$a;)Lyx;
-
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 withfromandto, 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
fromandto, respectively - Mappings:
Namespace Name Mixin selector named xmapLnet/minecraft/network/codec/PacketCodec;xmap(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56432Lnet/minecraft/class_9139;method_56432(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;official aLyx;a(Ljava/util/function/Function;Ljava/util/function/Function;)Lyx;
-
mapBuf
- Mappings:
Namespace Name Mixin selector named mapBufLnet/minecraft/network/codec/PacketCodec;mapBuf(Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56439Lnet/minecraft/class_9139;method_56439(Ljava/util/function/Function;)Lnet/minecraft/class_9139;official bLyx;b(Ljava/util/function/Function;)Lyx;
-
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
Statrequires 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" - likeStatType. 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 dispatchLnet/minecraft/network/codec/PacketCodec;dispatch(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_56440Lnet/minecraft/class_9139;method_56440(Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;official bLyx;b(Ljava/util/function/Function;Ljava/util/function/Function;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1> 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 tupleLnet/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_56434Lnet/minecraft/class_9139;method_56434(Lnet/minecraft/class_9139;Ljava/util/function/Function;Ljava/util/function/Function;)Lnet/minecraft/class_9139;official aLyx;a(Lyx;Ljava/util/function/Function;Ljava/util/function/Function;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1, T2> 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 tupleLnet/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_56435Lnet/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 aLyx;a(Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Ljava/util/function/BiFunction;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1, T2, T3> 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 tupleLnet/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_56436Lnet/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 aLyx;a(Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function3;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1, T2, T3, T4> 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 tupleLnet/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_56905Lnet/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 aLyx;a(Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1, T2, T3, T4, T5> 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 tupleLnet/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_56906Lnet/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 aLyx;a(Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function5;)Lyx;
-
tuple
static <B,C, PacketCodec<B,T1, T2, T3, T4, T5, T6> 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 tupleLnet/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_58025Lnet/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 aLyx;a(Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lyx;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function6;)Lyx;
-
recursive
- Mappings:
Namespace Name Mixin selector named recursiveLnet/minecraft/network/codec/PacketCodec;recursive(Ljava/util/function/UnaryOperator;)Lnet/minecraft/network/codec/PacketCodec;intermediary method_58024Lnet/minecraft/class_9139;method_58024(Ljava/util/function/UnaryOperator;)Lnet/minecraft/class_9139;official aLyx;a(Ljava/util/function/UnaryOperator;)Lyx;
-
cast
Returns the same codec, casted to work with buffers of typeS.- Returns:
- the same codec, casted to work with buffers of type
S - API Note:
- For example,
BlockPos.PACKET_CODECis defined asPacketCodec<ByteBuf, BlockPos>. To use this codec wherePacketByteBufis 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 castLnet/minecraft/network/codec/PacketCodec;cast()Lnet/minecraft/network/codec/PacketCodec;intermediary method_56430Lnet/minecraft/class_9139;method_56430()Lnet/minecraft/class_9139;official aLyx;a()Lyx;
-