| 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.CustomQueryPayload;
|
| 8 | import net.minecraft.network.protocol.login.custom.DiscardedQueryPayload;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 |
|
| 11 | public record ClientboundCustomQueryPacket(int transactionId, CustomQueryPayload payload) implements Packet<ClientLoginPacketListener> {
|
| 12 | public static final StreamCodec<FriendlyByteBuf, ClientboundCustomQueryPacket> STREAM_CODEC = Packet.codec(
|
| 13 | ClientboundCustomQueryPacket::write, ClientboundCustomQueryPacket::new
|
| 14 | );
|
| 15 | private static final int MAX_PAYLOAD_SIZE = 1048576;
|
| 16 |
|
| 17 | private ClientboundCustomQueryPacket(final FriendlyByteBuf input) {
|
| 18 | this(input.readVarInt(), readPayload(input.readIdentifier(), input));
|
| 19 | }
|
| 20 |
|
| 21 | private static CustomQueryPayload readPayload(final Identifier identifier, final FriendlyByteBuf input) {
|
| 22 | return readUnknownPayload(identifier, input);
|
| 23 | }
|
| 24 |
|
| 25 | private static DiscardedQueryPayload readUnknownPayload(final Identifier identifier, final FriendlyByteBuf input) {
|
| 26 | int length = input.readableBytes();
|
| 27 | if (length >= 0 && length <= 1048576) {
|
| 28 | input.skipBytes(length);
|
| 29 | return new DiscardedQueryPayload(identifier);
|
| 30 | } else {
|
| 31 | throw new IllegalArgumentException("Payload may not be larger than 1048576 bytes");
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | private void write(final FriendlyByteBuf output) {
|
| 36 | output.writeVarInt(this.transactionId);
|
| 37 | output.writeIdentifier(this.payload.id());
|
| 38 | this.payload.write(output);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public PacketType<ClientboundCustomQueryPacket> type() {
|
| 43 | return LoginPacketTypes.CLIENTBOUND_CUSTOM_QUERY;
|
| 44 | }
|
| 45 |
|
| 46 | public void handle(final ClientLoginPacketListener listener) {
|
| 47 | listener.handleCustomQuery(this);
|
| 48 | }
|
| 49 | }
|
| 50 |
|