| 1 | package net.minecraft.network.protocol.common.custom;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.stream.Collectors;
|
| 7 | import net.minecraft.network.FriendlyByteBuf;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.network.codec.StreamDecoder;
|
| 10 | import net.minecraft.network.codec.StreamMemberEncoder;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 |
|
| 13 | public interface CustomPacketPayload {
|
| 14 | CustomPacketPayload.Type<? extends CustomPacketPayload> type();
|
| 15 |
|
| 16 | static <B extends ByteBuf, T extends CustomPacketPayload> StreamCodec<B, T> codec(final StreamMemberEncoder<B, T> writer, final StreamDecoder<B, T> reader) {
|
| 17 | return StreamCodec.ofMember(writer, reader);
|
| 18 | }
|
| 19 |
|
| 20 | static <T extends CustomPacketPayload> CustomPacketPayload.Type<T> createType(final String id) {
|
| 21 | return new CustomPacketPayload.Type<>(Identifier.withDefaultNamespace(id));
|
| 22 | }
|
| 23 |
|
| 24 | static <B extends FriendlyByteBuf> StreamCodec<B, CustomPacketPayload> codec(
|
| 25 | final CustomPacketPayload.FallbackProvider<B> fallback, final List<CustomPacketPayload.TypeAndCodec<? super B, ?>> types
|
| 26 | ) {
|
| 27 | final Map<Identifier, StreamCodec<? super B, ? extends CustomPacketPayload>> idToType = types.stream()
|
| 28 | .collect(Collectors.toUnmodifiableMap(t -> t.type().id(), CustomPacketPayload.TypeAndCodec::codec));
|
| 29 | return new StreamCodec<B, CustomPacketPayload>() {
|
| 30 | private StreamCodec<? super B, ? extends CustomPacketPayload> findCodec(final Identifier typeId) {
|
| 31 | StreamCodec<? super B, ? extends CustomPacketPayload> codec = idToType.get(typeId);
|
| 32 | return codec != null ? codec : fallback.create(typeId);
|
| 33 | }
|
| 34 |
|
| 35 | private <T extends CustomPacketPayload> void writeCap(final B output, final CustomPacketPayload.Type<T> type, final CustomPacketPayload payload) {
|
| 36 | output.writeIdentifier(type.id());
|
| 37 | StreamCodec<B, T> codec = this.findCodec(type.id);
|
| 38 | codec.encode(output, (T)payload);
|
| 39 | }
|
| 40 |
|
| 41 | public void encode(final B output, final CustomPacketPayload value) {
|
| 42 | this.writeCap(output, value.type(), value);
|
| 43 | }
|
| 44 |
|
| 45 | public CustomPacketPayload decode(final B input) {
|
| 46 | Identifier identifier = input.readIdentifier();
|
| 47 | return (CustomPacketPayload)this.findCodec(identifier).decode(input);
|
| 48 | }
|
| 49 | };
|
| 50 | }
|
| 51 |
|
| 52 | interface FallbackProvider<B extends FriendlyByteBuf> {
|
| 53 | StreamCodec<B, ? extends CustomPacketPayload> create(Identifier typeId);
|
| 54 | }
|
| 55 |
|
| 56 | record Type<T extends CustomPacketPayload>(Identifier id) {
|
| 57 | }
|
| 58 |
|
| 59 | record TypeAndCodec<B extends FriendlyByteBuf, T extends CustomPacketPayload>(CustomPacketPayload.Type<T> type, StreamCodec<B, T> codec) {
|
| 60 | }
|
| 61 | }
|
| 62 |
|