| 1 | package net.minecraft.network.protocol;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.network.ClientboundPacketListener;
|
| 9 | import net.minecraft.network.ConnectionProtocol;
|
| 10 | import net.minecraft.network.PacketListener;
|
| 11 | import net.minecraft.network.ProtocolInfo;
|
| 12 | import net.minecraft.network.ServerboundPacketListener;
|
| 13 | import net.minecraft.network.codec.StreamCodec;
|
| 14 | import net.minecraft.util.Unit;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | public class ProtocolInfoBuilder<T extends PacketListener, B extends ByteBuf, C> {
|
| 18 | private final ConnectionProtocol protocol;
|
| 19 | private final PacketFlow flow;
|
| 20 | private final List<ProtocolInfoBuilder.CodecEntry<T, ?, B, C>> codecs = new ArrayList<>();
|
| 21 | private @Nullable BundlerInfo bundlerInfo;
|
| 22 |
|
| 23 | public ProtocolInfoBuilder(final ConnectionProtocol protocol, final PacketFlow flow) {
|
| 24 | this.protocol = protocol;
|
| 25 | this.flow = flow;
|
| 26 | }
|
| 27 |
|
| 28 | public <P extends Packet<? super T>> ProtocolInfoBuilder<T, B, C> addPacket(final PacketType<P> type, final StreamCodec<? super B, P> serializer) {
|
| 29 | this.codecs.add(new ProtocolInfoBuilder.CodecEntry<>(type, serializer, null));
|
| 30 | return this;
|
| 31 | }
|
| 32 |
|
| 33 | public <P extends Packet<? super T>> ProtocolInfoBuilder<T, B, C> addPacket(
|
| 34 | final PacketType<P> type, final StreamCodec<? super B, P> serializer, final CodecModifier<B, P, C> modifier
|
| 35 | ) {
|
| 36 | this.codecs.add(new ProtocolInfoBuilder.CodecEntry<>(type, serializer, modifier));
|
| 37 | return this;
|
| 38 | }
|
| 39 |
|
| 40 | public <P extends BundlePacket<? super T>, D extends BundleDelimiterPacket<? super T>> ProtocolInfoBuilder<T, B, C> withBundlePacket(
|
| 41 | final PacketType<P> bundlerPacket, final Function<Iterable<Packet<? super T>>, P> constructor, final D delimiterPacket
|
| 42 | ) {
|
| 43 | StreamCodec<ByteBuf, D> delimitedCodec = StreamCodec.unit(delimiterPacket);
|
| 44 | PacketType<D> delimiterType = (PacketType<D>)delimiterPacket.type();
|
| 45 | this.codecs.add(new ProtocolInfoBuilder.CodecEntry<>(delimiterType, delimitedCodec, null));
|
| 46 | this.bundlerInfo = BundlerInfo.createForPacket(bundlerPacket, constructor, delimiterPacket);
|
| 47 | return this;
|
| 48 | }
|
| 49 |
|
| 50 | private StreamCodec<ByteBuf, Packet<? super T>> buildPacketCodec(
|
| 51 | final Function<ByteBuf, B> contextWrapper, final List<ProtocolInfoBuilder.CodecEntry<T, ?, B, C>> codecs, final C context
|
| 52 | ) {
|
| 53 | ProtocolCodecBuilder<ByteBuf, T> codecBuilder = new ProtocolCodecBuilder<>(this.flow);
|
| 54 |
|
| 55 | for (ProtocolInfoBuilder.CodecEntry<T, ?, B, C> codec : codecs) {
|
| 56 | codec.addToBuilder(codecBuilder, contextWrapper, context);
|
| 57 | }
|
| 58 |
|
| 59 | return codecBuilder.build();
|
| 60 | }
|
| 61 |
|
| 62 | private static ProtocolInfo.Details buildDetails(
|
| 63 | final ConnectionProtocol protocol, final PacketFlow flow, final List<? extends ProtocolInfoBuilder.CodecEntry<?, ?, ?, ?>> codecs
|
| 64 | ) {
|
| 65 | return new ProtocolInfo.Details() {
|
| 66 | @Override
|
| 67 | public ConnectionProtocol id() {
|
| 68 | return protocol;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public PacketFlow flow() {
|
| 73 | return flow;
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | public void listPackets(final ProtocolInfo.Details.PacketVisitor output) {
|
| 78 | for (int i = 0; i < codecs.size(); i++) {
|
| 79 | ProtocolInfoBuilder.CodecEntry<?, ?, ?, ?> entry = (ProtocolInfoBuilder.CodecEntry<?, ?, ?, ?>)codecs.get(i);
|
| 80 | output.accept(entry.type, i);
|
| 81 | }
|
| 82 | }
|
| 83 | };
|
| 84 | }
|
| 85 |
|
| 86 | public SimpleUnboundProtocol<T, B> buildUnbound(final C context) {
|
| 87 | final List<ProtocolInfoBuilder.CodecEntry<T, ?, B, C>> codecs = List.copyOf(this.codecs);
|
| 88 | final BundlerInfo bundlerInfo = this.bundlerInfo;
|
| 89 | final ProtocolInfo.Details details = buildDetails(this.protocol, this.flow, codecs);
|
| 90 | return new SimpleUnboundProtocol<T, B>() {
|
| 91 | @Override
|
| 92 | public ProtocolInfo<T> bind(final Function<ByteBuf, B> contextWrapper) {
|
| 93 | return new ProtocolInfoBuilder.Implementation<>(
|
| 94 | ProtocolInfoBuilder.this.protocol, ProtocolInfoBuilder.this.flow, ProtocolInfoBuilder.this.buildPacketCodec(contextWrapper, codecs, context), bundlerInfo
|
| 95 | );
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public ProtocolInfo.Details details() {
|
| 100 | return details;
|
| 101 | }
|
| 102 | };
|
| 103 | }
|
| 104 |
|
| 105 | public UnboundProtocol<T, B, C> buildUnbound() {
|
| 106 | final List<ProtocolInfoBuilder.CodecEntry<T, ?, B, C>> codecs = List.copyOf(this.codecs);
|
| 107 | final BundlerInfo bundlerInfo = this.bundlerInfo;
|
| 108 | final ProtocolInfo.Details details = buildDetails(this.protocol, this.flow, codecs);
|
| 109 | return new UnboundProtocol<T, B, C>() {
|
| 110 | @Override
|
| 111 | public ProtocolInfo<T> bind(final Function<ByteBuf, B> contextWrapper, final C context) {
|
| 112 | return new ProtocolInfoBuilder.Implementation<>(
|
| 113 | ProtocolInfoBuilder.this.protocol, ProtocolInfoBuilder.this.flow, ProtocolInfoBuilder.this.buildPacketCodec(contextWrapper, codecs, context), bundlerInfo
|
| 114 | );
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public ProtocolInfo.Details details() {
|
| 119 | return details;
|
| 120 | }
|
| 121 | };
|
| 122 | }
|
| 123 |
|
| 124 | private static <L extends PacketListener, B extends ByteBuf> SimpleUnboundProtocol<L, B> protocol(
|
| 125 | final ConnectionProtocol id, final PacketFlow flow, final Consumer<ProtocolInfoBuilder<L, B, Unit>> config
|
| 126 | ) {
|
| 127 | ProtocolInfoBuilder<L, B, Unit> builder = new ProtocolInfoBuilder<>(id, flow);
|
| 128 | config.accept(builder);
|
| 129 | return builder.buildUnbound(Unit.INSTANCE);
|
| 130 | }
|
| 131 |
|
| 132 | public static <T extends ServerboundPacketListener, B extends ByteBuf> SimpleUnboundProtocol<T, B> serverboundProtocol(
|
| 133 | final ConnectionProtocol id, final Consumer<ProtocolInfoBuilder<T, B, Unit>> config
|
| 134 | ) {
|
| 135 | return protocol(id, PacketFlow.SERVERBOUND, config);
|
| 136 | }
|
| 137 |
|
| 138 | public static <T extends ClientboundPacketListener, B extends ByteBuf> SimpleUnboundProtocol<T, B> clientboundProtocol(
|
| 139 | final ConnectionProtocol id, final Consumer<ProtocolInfoBuilder<T, B, Unit>> config
|
| 140 | ) {
|
| 141 | return protocol(id, PacketFlow.CLIENTBOUND, config);
|
| 142 | }
|
| 143 |
|
| 144 | private static <L extends PacketListener, B extends ByteBuf, C> UnboundProtocol<L, B, C> contextProtocol(
|
| 145 | final ConnectionProtocol id, final PacketFlow flow, final Consumer<ProtocolInfoBuilder<L, B, C>> config
|
| 146 | ) {
|
| 147 | ProtocolInfoBuilder<L, B, C> builder = new ProtocolInfoBuilder<>(id, flow);
|
| 148 | config.accept(builder);
|
| 149 | return builder.buildUnbound();
|
| 150 | }
|
| 151 |
|
| 152 | public static <T extends ServerboundPacketListener, B extends ByteBuf, C> UnboundProtocol<T, B, C> contextServerboundProtocol(
|
| 153 | final ConnectionProtocol id, final Consumer<ProtocolInfoBuilder<T, B, C>> config
|
| 154 | ) {
|
| 155 | return contextProtocol(id, PacketFlow.SERVERBOUND, config);
|
| 156 | }
|
| 157 |
|
| 158 | public static <T extends ClientboundPacketListener, B extends ByteBuf, C> UnboundProtocol<T, B, C> contextClientboundProtocol(
|
| 159 | final ConnectionProtocol id, final Consumer<ProtocolInfoBuilder<T, B, C>> config
|
| 160 | ) {
|
| 161 | return contextProtocol(id, PacketFlow.CLIENTBOUND, config);
|
| 162 | }
|
| 163 |
|
| 164 | private record CodecEntry<T extends PacketListener, P extends Packet<? super T>, B extends ByteBuf, C>(
|
| 165 | PacketType<P> type, StreamCodec<? super B, P> serializer, @Nullable CodecModifier<B, P, C> modifier
|
| 166 | ) {
|
| 167 | public void addToBuilder(final ProtocolCodecBuilder<ByteBuf, T> codecBuilder, final Function<ByteBuf, B> contextWrapper, final C context) {
|
| 168 | StreamCodec<? super B, P> finalSerializer;
|
| 169 | if (this.modifier != null) {
|
| 170 | finalSerializer = this.modifier.apply(this.serializer, context);
|
| 171 | } else {
|
| 172 | finalSerializer = this.serializer;
|
| 173 | }
|
| 174 |
|
| 175 | StreamCodec<ByteBuf, P> baseCodec = finalSerializer.mapStream(contextWrapper);
|
| 176 | codecBuilder.add(this.type, baseCodec);
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | private record Implementation<L extends PacketListener>(
|
| 181 | ConnectionProtocol id, PacketFlow flow, StreamCodec<ByteBuf, Packet<? super L>> codec, @Nullable BundlerInfo bundlerInfo
|
| 182 | ) implements ProtocolInfo<L> {
|
| 183 | }
|
| 184 | }
|
| 185 |
|