class Handshake extends Packet {

    public static Handshake loadFromPacket(byte[] packet) {
        Handshake obj = new Handshake();
        Proto proto = new Proto(packet, 3);

        obj.sequenceId = proto.get_fixed_int(1);
        obj.protocolVersion = proto.get_fixed_int(1);
        obj.serverVersion = proto.get_null_str();
        obj.connectionId = proto.get_fixed_int(4);
        obj.challenge1 = proto.get_fixed_str(8);
        proto.get_filler(1);
        obj.capabilityFlags = proto.get_fixed_int(2) << 16;

        if (proto.has_remaining_data()) {
            obj.characterSet = proto.get_fixed_int(1);
            obj.statusFlags = proto.get_fixed_int(2);
            obj.setCapabilityFlag(proto.get_fixed_int(2));

            if (obj.hasCapabilityFlag(Flags.CLIENT_PLUGIN_AUTH)) {
                obj.authPluginDataLength = proto.get_fixed_int(1);
            }
            else {
                proto.get_filler(1);
            }

            proto.get_filler(10);

            if (obj.hasCapabilityFlag(Flags.CLIENT_SECURE_CONNECTION)) {
                obj.challenge2 = proto.get_fixed_str(Math.max(13, obj.authPluginDataLength - 8));
            }

            if (obj.hasCapabilityFlag(Flags.CLIENT_PLUGIN_AUTH)) {
                obj.authPluginName = proto.get_null_str();
            }
        }

        return obj;
    }

}