package net.minecraft.network.protocol.login;

import net.minecraft.network.PacketListener;
import java.io.IOException;
import javax.annotation.Nullable;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;

public class ServerboundCustomQueryPacket implements Packet<ServerLoginPacketListener> {
    private int transactionId;
    private FriendlyByteBuf data;
    
    public ServerboundCustomQueryPacket() {
    }
    
    public ServerboundCustomQueryPacket(final int integer, @Nullable final FriendlyByteBuf kv) {
        this.transactionId = integer;
        this.data = kv;
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.transactionId = kv.readVarInt();
        if (kv.readBoolean()) {
            final int integer3 = kv.readableBytes();
            if (integer3 < 0 || integer3 > 1048576) {
                throw new IOException("Payload may not be larger than 1048576 bytes");
            }
            this.data = new FriendlyByteBuf(kv.readBytes(integer3));
        }
        else {
            this.data = null;
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.transactionId);
        if (this.data != null) {
            kv.writeBoolean(true);
            kv.writeBytes(this.data.copy());
        }
        else {
            kv.writeBoolean(false);
        }
    }
    
    @Override
    public void handle(final ServerLoginPacketListener rn) {
        rn.handleCustomQueryPacket(this);
    }
}
