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 mg) {
        this.transactionId = integer;
        this.data = mg;
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.transactionId = mg.readVarInt();
        if (mg.readBoolean()) {
            final int integer3 = mg.readableBytes();
            if (integer3 < 0 || integer3 > 1048576) {
                throw new IOException("Payload may not be larger than 1048576 bytes");
            }
            this.data = new FriendlyByteBuf(mg.readBytes(integer3));
        }
        else {
            this.data = null;
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.transactionId);
        if (this.data != null) {
            mg.writeBoolean(true);
            mg.writeBytes(this.data.copy());
        }
        else {
            mg.writeBoolean(false);
        }
    }
    
    @Override
    public void handle(final ServerLoginPacketListener tc) {
        tc.handleCustomQueryPacket(this);
    }
}
