1919import java .util .HashMap ;
2020import java .util .Map ;
2121import java .util .Objects ;
22+ import java .util .function .IntSupplier ;
2223
2324import io .netty .buffer .ByteBufUtil ;
2425import it .unimi .dsi .fastutil .objects .Object2IntMap ;
2526import it .unimi .dsi .fastutil .objects .Object2IntOpenHashMap ;
27+ import it .unimi .dsi .fastutil .objects .Object2ObjectMap ;
28+ import it .unimi .dsi .fastutil .objects .Object2ObjectOpenHashMap ;
2629import org .jspecify .annotations .Nullable ;
2730
2831import net .minecraft .network .ConnectionProtocol ;
@@ -44,7 +47,8 @@ public class PayloadTypeRegistryImpl<B extends FriendlyByteBuf> implements Paylo
4447 public static final PayloadTypeRegistryImpl <RegistryFriendlyByteBuf > SERVERBOUND_PLAY = new PayloadTypeRegistryImpl <>(ConnectionProtocol .PLAY , PacketFlow .SERVERBOUND );
4548 public static final PayloadTypeRegistryImpl <RegistryFriendlyByteBuf > CLIENTBOUND_PLAY = new PayloadTypeRegistryImpl <>(ConnectionProtocol .PLAY , PacketFlow .CLIENTBOUND );
4649 private final Map <Identifier , CustomPacketPayload .TypeAndCodec <B , ? extends CustomPacketPayload >> packetTypes = new HashMap <>();
47- private final Object2IntMap <Identifier > maxPacketSize = new Object2IntOpenHashMap <>();
50+ private final Object2IntMap <Identifier > maxPacketSizes = new Object2IntOpenHashMap <>();
51+ private final Object2ObjectMap <Identifier , IntSupplier > pendingMaxPacketSizes = new Object2ObjectOpenHashMap <>();
4852 private final ConnectionProtocol protocol ;
4953 private final PacketFlow flow ;
5054 private final int minimalSplittableSize ;
@@ -66,7 +70,7 @@ public static PayloadTypeRegistryImpl<?> get(ProtocolInfo<?> state) {
6670
6771 @ Override
6872 public <T extends CustomPacketPayload > CustomPacketPayload .TypeAndCodec <? super B , T > register (CustomPacketPayload .Type <T > type , StreamCodec <? super B , T > codec ) {
69- Objects .requireNonNull (type , "id " );
73+ Objects .requireNonNull (type , "type " );
7074 Objects .requireNonNull (codec , "codec" );
7175
7276 final CustomPacketPayload .TypeAndCodec <B , T > payloadType = new CustomPacketPayload .TypeAndCodec <>(type , codec .cast ());
@@ -80,15 +84,30 @@ public <T extends CustomPacketPayload> CustomPacketPayload.TypeAndCodec<? super
8084 }
8185
8286 @ Override
83- public <T extends CustomPacketPayload > CustomPacketPayload .TypeAndCodec <? super B , T > registerLarge (CustomPacketPayload .Type <T > type , StreamCodec <? super B , T > codec , int maxPayloadSize ) {
84- if (maxPayloadSize < 0 ) {
85- throw new IllegalArgumentException ("Provided maxPayloadSize needs to be positive!" );
87+ public <T extends CustomPacketPayload > CustomPacketPayload .TypeAndCodec <? super B , T > registerLarge (CustomPacketPayload .Type <T > type , StreamCodec <? super B , T > codec , int maxPacketSize ) {
88+ if (maxPacketSize < 0 ) {
89+ throw new IllegalArgumentException ("Provided maxPacketSize needs to be positive!" );
8690 }
8791
8892 CustomPacketPayload .TypeAndCodec <? super B , T > typeAndCodec = register (type , codec );
93+ padAndSetMaxPacketSize (type .id (), maxPacketSize );
94+ return typeAndCodec ;
95+ }
96+
97+ @ Override
98+ public <T extends CustomPacketPayload > CustomPacketPayload .TypeAndCodec <? super B , T > registerLarge (CustomPacketPayload .Type <T > type , StreamCodec <? super B , T > codec , IntSupplier maxPacketSizeSupplier ) {
99+ Objects .requireNonNull (maxPacketSizeSupplier , "maxPacketSizeSupplier" );
100+
101+ CustomPacketPayload .TypeAndCodec <? super B , T > typeAndCodec = register (type , codec );
102+ pendingMaxPacketSizes .put (type .id (), maxPacketSizeSupplier );
103+ return typeAndCodec ;
104+ }
105+
106+ private void padAndSetMaxPacketSize (Identifier id , int maxSize ) {
89107 // Defines max packet size, increased by length of packet's Identifier to cover full size of CustomPayloadX2YPackets.
90- int identifierSize = ByteBufUtil .utf8MaxBytes (type .id ().toString ());
91- int maxPacketSize = maxPayloadSize + VarInt .getByteSize (identifierSize ) + identifierSize + 5 * 2 ;
108+ int identifierSize = ByteBufUtil .utf8MaxBytes (id .toString ());
109+ int paddingSize = VarInt .getByteSize (identifierSize ) + identifierSize + 5 * 2 ;
110+ int maxPacketSize = maxSize + paddingSize ;
92111
93112 // Prevent overflow
94113 if (maxPacketSize < 0 ) {
@@ -97,23 +116,36 @@ public <T extends CustomPacketPayload> CustomPacketPayload.TypeAndCodec<? super
97116
98117 // No need to enable splitting, if packet's max size is smaller than chunk
99118 if (maxPacketSize > this .minimalSplittableSize ) {
100- this .maxPacketSize .put (type . id () , maxPacketSize );
119+ this .maxPacketSizes .put (id , maxPacketSize );
101120 }
102-
103- return typeAndCodec ;
104121 }
105122
106123 public CustomPacketPayload .@ Nullable TypeAndCodec <B , ? extends CustomPacketPayload > get (Identifier id ) {
107124 return packetTypes .get (id );
108125 }
109126
110- public <T extends CustomPacketPayload > CustomPacketPayload .@ Nullable TypeAndCodec <B , T > get (CustomPacketPayload .Type <T > id ) {
127+ public <T extends CustomPacketPayload > CustomPacketPayload .@ Nullable TypeAndCodec <B , T > get (CustomPacketPayload .Type <T > type ) {
111128 //noinspection unchecked
112- return (CustomPacketPayload .TypeAndCodec <B , T >) packetTypes .get (id .id ());
129+ return (CustomPacketPayload .TypeAndCodec <B , T >) packetTypes .get (type .id ());
113130 }
114131
115- public int getMaxPacketSize (Identifier id ) {
116- return this .maxPacketSize .getOrDefault (id , -1 );
132+ /**
133+ * @return the max packet size, or -1 if the payload type does not need splitting.
134+ */
135+ public int getMaxPacketSizeForSplitting (Identifier id ) {
136+ IntSupplier supplier = this .pendingMaxPacketSizes .remove (id );
137+
138+ if (supplier != null ) {
139+ int maxPacketSize = supplier .getAsInt ();
140+
141+ if (maxPacketSize < 0 ) {
142+ throw new IllegalArgumentException ("maxPacketSize supplier for packet type " + id + ": must be positive!" );
143+ }
144+
145+ padAndSetMaxPacketSize (id , maxPacketSize );
146+ }
147+
148+ return this .maxPacketSizes .getOrDefault (id , -1 );
117149 }
118150
119151 public ConnectionProtocol getProtocol () {
0 commit comments