| 1 | package net.minecraft.network.protocol.login;
|
| 2 |
|
| 3 | import net.minecraft.network.FriendlyByteBuf;
|
| 4 | import net.minecraft.network.codec.StreamCodec;
|
| 5 | import net.minecraft.network.protocol.Packet;
|
| 6 | import net.minecraft.network.protocol.PacketType;
|
| 7 | import net.minecraft.network.protocol.login.custom.CustomQueryAnswerPayload;
|
| 8 | import net.minecraft.network.protocol.login.custom.DiscardedQueryAnswerPayload;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | public record ServerboundCustomQueryAnswerPacket(int transactionId, @Nullable CustomQueryAnswerPayload payload) implements Packet<ServerLoginPacketListener> {
|
| 12 | public static final StreamCodec<FriendlyByteBuf, ServerboundCustomQueryAnswerPacket> STREAM_CODEC = Packet.codec(
|
| 13 | ServerboundCustomQueryAnswerPacket::write, ServerboundCustomQueryAnswerPacket::read
|
| 14 | );
|
| 15 | private static final int MAX_PAYLOAD_SIZE = 1048576;
|
| 16 |
|
| 17 | private static ServerboundCustomQueryAnswerPacket read(final FriendlyByteBuf input) {
|
| 18 | int transactionId = input.readVarInt();
|
| 19 | return new ServerboundCustomQueryAnswerPacket(transactionId, readPayload(transactionId, input));
|
| 20 | }
|
| 21 |
|
| 22 | private static CustomQueryAnswerPayload readPayload(final int transactionId, final FriendlyByteBuf input) {
|
| 23 | return readUnknownPayload(input);
|
| 24 | }
|
| 25 |
|
| 26 | private static CustomQueryAnswerPayload readUnknownPayload(final FriendlyByteBuf input) {
|
| 27 | int length = input.readableBytes();
|
| 28 | if (length >= 0 && length <= 1048576) {
|
| 29 | input.skipBytes(length);
|
| 30 | return DiscardedQueryAnswerPayload.INSTANCE;
|
| 31 | } else {
|
| 32 | throw new IllegalArgumentException("Payload may not be larger than 1048576 bytes");
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | private void write(final FriendlyByteBuf output) {
|
| 37 | output.writeVarInt(this.transactionId);
|
| 38 | output.writeNullable(this.payload, (buf, data) -> data.write(buf));
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public PacketType<ServerboundCustomQueryAnswerPacket> type() {
|
| 43 | return LoginPacketTypes.SERVERBOUND_CUSTOM_QUERY_ANSWER;
|
| 44 | }
|
| 45 |
|
| 46 | public void handle(final ServerLoginPacketListener listener) {
|
| 47 | listener.handleCustomQueryPacket(this);
|
| 48 | }
|
| 49 | }
|
| 50 |
|