Código:

#include <YSI-Includes\YSI_Coding\y_hooks>

#define C_MAX_VEHICLES 400

enum C_Info
{
    Float:C_X,
    Float:C_Y,
    Float:C_Z,
    Float:C_A,

    C_Modelo,
    C_DonoID, //Id do dono
    C_trv,    // Se o carro esta trancado
    C_Gasosa, //Gasolina
    Float:C_Lataria,//lataria
    C_Tunado, //tuning
    C_Cor1,
    C_Cor2
}

new C_InfoVehicle[C_MAX_VEHICLES][C_Info], C_MaxId;
new DB:C_Database;
new CarrosComprados[C_MAX_VEHICLES];

C_ConectarDBVehicle()
{
    if((C_Database = db_open("Veiculos.db")) == DB:0)
    {
        SendRconCommand("exit");
        print("Falha ao se conectar a DataBase SQlite"); // DEBUG

    } else {
        print("Conectado com sucesso a DataBase SQlite."); // DEBUG
    }

    new Query[900];
    format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS C_Vehicles (\
    ID INTEGER PRIMARY KEY AUTOINCREMENT,\
    X FLOAT DEFAULT 0.0,\
    Y FLOAT DEFAULT 0.0,\
    Z FLOAT DEFAULT 0.0,\
    A FLOAT DEFAULT 0.0,\
    TRANCADA INTEGER DEFAULT 0,\
    DonoID INTEGER DEFAULT 0,\
    Modelo INTEGER DEFAULT 0,\
    GasolinaID INTEGER DEFAULT 0,\
    Tunado INTEGER DEFAULT 0,\
    Cor1 INTEGER DEFAULT 0,\
    Cor2 INTEGER DEFAULT 0,\
    Lataria FLOAT DEFAULT 0.0)");
    db_query(C_Database, Query);

    format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS DONOS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NOME TEXT DEFAULT '', CASAID INTEGER DEFAULT 0)");
    db_query(C_Database, Query);

    return 1;
}

C_CarregarCarros()
{
    new DBResult:Result, Query[100];
    for(new i = 0; i < C_MAX_VEHICLES; i++)
    {
        format(Query, sizeof(Query), "SELECT * FROM C_Vehicles WHERE ID = %d", i);
        Result = db_query(C_Database, Query);

        if(db_num_rows(Result))
        {
            C_InfoVehicle[i][C_X] = db_get_field_assoc_float(Result, "X");
            C_InfoVehicle[i][C_Y] = db_get_field_assoc_float(Result, "Y");
            C_InfoVehicle[i][C_Z] = db_get_field_assoc_float(Result, "Z");
            C_InfoVehicle[i][C_A] = db_get_field_assoc_float(Result, "A");

            C_InfoVehicle[i][C_Lataria] = db_get_field_assoc_float(Result, "Lataria");

            C_InfoVehicle[i][C_DonoID] = db_get_field_assoc_int(Result, "DonoID");
            C_InfoVehicle[i][C_trv] = db_get_field_assoc_int(Result, "TRANCADA");
            C_InfoVehicle[i][C_Gasosa] = db_get_field_assoc_int(Result, "GasolinaID");
            C_InfoVehicle[i][C_Tunado] = db_get_field_assoc_int(Result, "Tunado");
            C_InfoVehicle[i][C_Modelo] = db_get_field_assoc_int(Result, "Modelo");

            C_InfoVehicle[i][C_Cor1] = db_get_field_assoc_int(Result, "Cor1");
            C_InfoVehicle[i][C_Cor2] = db_get_field_assoc_int(Result, "Cor2");
            C_MaxId ++;

            CriarCarro(i);
        }
        db_free_result(Result);
    }

    printf("[load] Foram Carregados %d carros.", C_MaxId);
    return 1;
}

CriarCarro(i)
{
    CarrosComprados[i] = AddStaticVehicle(C_InfoVehicle[i][C_Modelo], C_InfoVehicle[i][C_X], C_InfoVehicle[i][C_Y], C_InfoVehicle[i][C_Z], C_InfoVehicle[i][C_A], C_InfoVehicle[i][C_Cor1], C_InfoVehicle[i][C_Cor2]);
    SetVehicleHealth(CarrosComprados[i], C_InfoVehicle[i][C_Lataria]);
    GasolinaV[CarrosComprados[i]] = C_InfoVehicle[i][C_Gasosa];
    // Adicionando o ID do veículo para a verificação de propriedade
    printf("[Debug - Loading]: Criando carro. ID do carro: %d. DonoID: %d. Trancado: %d. Modelo: %d", i, C_InfoVehicle[i][C_DonoID], C_InfoVehicle[i][C_trv], C_InfoVehicle[i][C_Modelo]);
    C_MaxId ++;
    return 1;
}


Criar_Carro(playerid, id, modelo)
{
    new DBResult:Result, Query[320];
    format(Query, sizeof(Query), "SELECT * FROM C_Vehicles WHERE ID = %d", id);
    Result = db_query(C_Database, Query);

    if(db_num_rows(Result))
    {
        SendClientMessage(playerid, -1, "Ocorreu um erro. Esse ID está em uso.");

    } else {
        new Float:Pos[3];
        GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);

        C_InfoVehicle[id][C_X] = Pos[0];
        C_InfoVehicle[id][C_Y] = Pos[1];
        C_InfoVehicle[id][C_Z] = Pos[2];
        C_InfoVehicle[id][C_A] = 0.0; // Adicione este valor para evitar valores indefinidos
        C_InfoVehicle[id][C_DonoID] = Player[playerid][pIDFIXO];
        C_InfoVehicle[id][C_trv] = 0;
        C_InfoVehicle[id][C_Tunado] = 0;
        C_InfoVehicle[id][C_Modelo] = modelo;
        C_InfoVehicle[id][C_Cor1] = 1;
        C_InfoVehicle[id][C_Cor2] = 1;
        C_InfoVehicle[id][C_Gasosa] = 100;
        C_InfoVehicle[id][C_Lataria] = 1000.0;
        printf("[Debug]: Criando carro. Player ID: %d, pIDFIXO: %d", playerid, C_InfoVehicle[id][C_DonoID]);
        CriarCarro(id);

        format(Query, sizeof(Query), "INSERT INTO C_Vehicles (X, Y, Z, A, TRANCADA, DonoID, GasolinaID, Lataria, Tunado, Modelo, Cor1, Cor2) VALUES (%f, %f, %f, %f, %d, %d, %d, %f, %d, %d, %d, %d)", C_InfoVehicle[id][C_X], C_InfoVehicle[id][C_Y], C_InfoVehicle[id][C_Z], C_InfoVehicle[id][C_A], C_InfoVehicle[id][C_trv], C_InfoVehicle[id][C_DonoID], C_InfoVehicle[id][C_Gasosa], C_InfoVehicle[id][C_Lataria], C_InfoVehicle[id][C_Tunado], C_InfoVehicle[id][C_Modelo], C_InfoVehicle[id][C_Cor1], C_InfoVehicle[id][C_Cor2]);
        db_query(C_Database, Query);

        C_MaxId ++;
    }
    db_free_result(Result);
    return 1;
}

hook OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    // Loop através dos veículos para verificar o status de trancamento
    for (new i = 0; i < C_MaxId; i++)
    {
        if (CarrosComprados[i] == vehicleid)
        {
            if (C_InfoVehicle[i][C_trv] == 1)
            {
                SendClientMessage(playerid, -1, "Este veículo está trancado.");
                RemovePlayerFromVehicle(playerid);
                return 0; // Impede o jogador de entrar no veículo
            }
        }
    }

    return 1; // Permite o jogador entrar no veículo se não estiver trancado
}

CMD:qdonoid(playerid)
{
    new vehicleid = GetPlayerVehicleID(playerid);
    if(vehicleid != INVALID_VEHICLE_ID)
    {
        for(new i = 0; i < C_MaxId; i++)
        {
            if(CarrosComprados[i] == vehicleid)
            {
                new string[180];
                format(string, sizeof string, "O id dono desse veiculo é %d", C_InfoVehicle[i][C_DonoID]);
                SendClientMessage(playerid, -1, string);

                // Debug message
                printf("Debug: Consultando dono do veículo. Player ID: %d, Veículo ID: %d, Índice: %d, Dono ID: %d", playerid, vehicleid, i, C_InfoVehicle[i][C_DonoID]);

                return 1;
            }
        }
    }
    SendClientMessage(playerid, -1, "Você não está em um veículo válido.");
    return 1;
}


CMD:criarcarro(playerid, params[])
{
    if(Player[playerid][Admin] <= 70) return SendClientMessage(playerid, -1, "Comando restrito a administradores.");
    new modelo;
    if(sscanf(params, "i", modelo)) return SendClientMessage(playerid, -1, "Use /criarcarro [modelo]");
    Criar_Carro(playerid, C_MaxId+1, modelo);
    return 1;
}

CMD:trancarcarro(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new mot, lu, alar, por, cap, porma, ob;
    // Debug message
    printf("Debug: Veículo ID obtido: %d", vehicleid);
    
    if (vehicleid == INVALID_VEHICLE_ID)
    {
        SendClientMessage(playerid, -1, "Você não está em um veículo.");
        return 1;
    }

    new i, found = 0;
    for (i = 0; i < C_MaxId; i++)
    {
        // Debug message
        printf("Debug: Verificando veículo no índice: %d", i);
        
        if (CarrosComprados[i] == vehicleid)
        {
            // Debug message
            printf("Debug: Tentando trancar o veículo. Índice: %d, Dono ID: %d, Player ID: %d", i, C_InfoVehicle[i][C_DonoID], Player[playerid][pIDFIXO]);

            found = 1; // Marcar que o veículo foi encontrado
            if (C_InfoVehicle[i][C_DonoID] == Player[playerid][pIDFIXO])
            {
                if (C_InfoVehicle[i][C_trv] == 1)
                {
                    SendClientMessage(playerid, -1, "O veículo já está trancado.");
                    return 1;
                }
                GetVehicleParamsEx(CarrosComprados[i], mot, lu, alar, por, cap, porma, ob);

                C_InfoVehicle[i][C_trv] = 1;
                new Query[256];
                format(Query, sizeof(Query), "UPDATE C_Vehicles SET TRANCADA = 1 WHERE ID = %d", i);
                db_query(C_Database, Query);

                SendClientMessage(playerid, -1, "Você trancou o veículo.");
                SetVehicleParamsEx(DonoCar[playerid], mot,lu,alar,VEHICLE_PARAMS_ON, cap, porma, ob);
                return 1;
            }
            else
            {
                SendClientMessage(playerid, -1, "Este veículo não pertence a você.");
                return 1;
            }
        }
    }
    
    // Se não encontrar o veículo
    if (!found)
    {
        SendClientMessage(playerid, -1, "O veículo não foi encontrado.");
    }
    
    return 1;
}



CMD:destrancarcarro(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    new mot, lu, alar, por, cap, porma, ob;
    if (vehicleid == INVALID_VEHICLE_ID)
    {
        SendClientMessage(playerid, -1, "Você não está em um veículo.");
        return 1;
    }

    new i;
    for (i = 0; i < C_MaxId; i++)
    {
        if (CarrosComprados[i] == vehicleid)
        {
            if (C_InfoVehicle[i][C_DonoID] == Player[playerid][pIDFIXO])
            {
                if (C_InfoVehicle[i][C_trv] == 0)
                {
                    SendClientMessage(playerid, -1, "O veículo já está destrancado.");
                    return 1;
                }

                C_InfoVehicle[i][C_trv] = 0;
                GetVehicleParamsEx(CarrosComprados[i], mot, lu, alar, por, cap, porma, ob);
                new Query[256];
                format(Query, sizeof(Query), "UPDATE C_Vehicles SET TRANCADA = 0 WHERE ID = %d", i);
                db_query(C_Database, Query);

                SendClientMessage(playerid, -1, "Você destrancou o veículo.");
                SetVehicleParamsEx(DonoCar[playerid], mot,lu,alar,VEHICLE_PARAMS_OFF, cap, porma, ob);
                return 1;
            }
            else
            {
                SendClientMessage(playerid, -1, "Este veículo não pertence a você.");
                return 1;
            }
        }
    }

    SendClientMessage(playerid, -1, "Ocorreu um erro ao tentar destrancar o veículo.");
    return 1;
}

CMD:estacionarcarro(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    if (vehicleid == INVALID_VEHICLE_ID)
    {
        SendClientMessage(playerid, -1, "Você não está em um veículo.");
        return 1;
    }

    new i;
    for (i = 0; i < C_MaxId; i++)
    {
        if (CarrosComprados[i] == vehicleid)
        {
            if (C_InfoVehicle[i][C_DonoID] == Player[playerid][pIDFIXO])
            {
                new Float:x, Float:y, Float:z, Float:a;
                GetVehiclePos(vehicleid, x, y, z);
                GetVehicleZAngle(vehicleid, a);

                C_InfoVehicle[i][C_X] = x;
                C_InfoVehicle[i][C_Y] = y;
                C_InfoVehicle[i][C_Z] = z;
                C_InfoVehicle[i][C_A] = a;

                new Query[256];
                format(Query, sizeof(Query), "UPDATE C_Vehicles SET X = %f, Y = %f, Z = %f, A = %f WHERE ID = %d", x, y, z, a, i);
                db_query(C_Database, Query);

                SendClientMessage(playerid, -1, "Você estacionou o veículo.");
                return 1;
            }
            else
            {
                SendClientMessage(playerid, -1, "Este veículo não pertence a você.");
                return 1;
            }
        }
    }

    SendClientMessage(playerid, -1, "Ocorreu um erro ao tentar estacionar o veículo.");
    return 1;
}

CMD:destruircarro(playerid, params[])
{
    new vehicleid = GetPlayerVehicleID(playerid);
    if (vehicleid == INVALID_VEHICLE_ID)
    {
        SendClientMessage(playerid, -1, "Você não está em um veículo.");
        return 1;
    }

    new i;
    for (i = 0; i < C_MaxId; i++)
    {
        if (CarrosComprados[i] == vehicleid)
        {
            if(Player[playerid][Admin] > 70)
            {
                DestroyVehicle(vehicleid);
                CarrosComprados[i] = -1;
                C_InfoVehicle[i][C_DonoID] = -1; // Marca como não possui dono

                new Query[256];
                format(Query, sizeof(Query), "DELETE FROM C_Vehicles WHERE ID = %d", i);
                db_query(C_Database, Query);

                SendClientMessage(playerid, -1, "Você destruiu o veículo.");
                return 1;
            }
            else
            {
                SendClientMessage(playerid, -1, "Você não é um administrador!.");
                return 1;
            }
        }
    }

    SendClientMessage(playerid, -1, "Ocorreu um erro ao tentar destruir o veículo.");
    return 1;
}