package net.minecraft.network.protocol; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import net.minecraft.network.PacketListener; import org.jspecify.annotations.Nullable; public interface BundlerInfo { int BUNDLE_SIZE_LIMIT = 4096; static > BundlerInfo createForPacket( final PacketType

bundlePacketType, final Function>, P> constructor, final BundleDelimiterPacket delimiterPacket ) { return new BundlerInfo() { @Override public void unbundlePacket(final Packet packet, final Consumer> output) { if (packet.type() == bundlePacketType) { P bundlerPacket = (P)packet; output.accept(delimiterPacket); bundlerPacket.subPackets().forEach(output); output.accept(delimiterPacket); } else { output.accept(packet); } } @Override public BundlerInfo.@Nullable Bundler startPacketBundling(final Packet packet) { return packet == delimiterPacket ? new BundlerInfo.Bundler() { private final List> bundlePackets = new ArrayList<>(); @Override public @Nullable Packet addPacket(final Packet packet) { if (packet == delimiterPacket) { return constructor.apply(this.bundlePackets); } Packet castPacket = (Packet)packet; if (this.bundlePackets.size() >= 4096) { throw new IllegalStateException("Too many packets in a bundle"); } this.bundlePackets.add(castPacket); return null; } } : null; } }; } void unbundlePacket(Packet packet, Consumer> output); BundlerInfo.@Nullable Bundler startPacketBundling(Packet packet); interface Bundler { @Nullable Packet addPacket(Packet packet); } }