Housing

Creating custom compatibility for housing.

What's needed?

To create custom compatibility for your housing/properties, you need to firstly get:

  • table where properties are saved;

If you already got this data you're good to go!

Gathering housing object

Headover to /server/functions/characters.lua and find Characters.ConvertProperties function:

Characters.ConvertProperties = function(identifier, charId)
    local properties = {}
    local startTime = GetGameTimer()
    debugPrint("[^2CHARACTERS.CONVERTPROPERTIES^7] Checking player properties [/]")
    if GetResourceState('qb-apartments') == 'started' then
        --only for QBCore
        local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
        local query = "SELECT * FROM `apartments` WHERE `citizenid` = ?"
        local response = MySQL.query.await(query, {citizenResponse[1].citizenid})
        for k,v in ipairs(response) do
            if Apartments.Locations[v.type] then
                properties[v.name] = {
                    name = v.name,
                    type = "property",
                    label = v.label,
                    coords = Apartments.Locations[v.type].coords.enter,
                }
            end
        end
    end
    if GetResourceState('qb-houses') == 'started' then
        --only for QBCore
        local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
        local query = "SELECT * FROM `player_houses` WHERE `citizenid` = ?"
        local response = MySQL.query.await(query, {citizenResponse[1].citizenid})
        
        for k,v in ipairs(response) do
            if Characters.QBHouses[v.house] then
                properties[v.house] = {
                    name = v.house,
                    type = "property",
                    label = v.label,
                    coords = vector3(Characters.QBHouses[v.house].coords.enter.x, Characters.QBHouses[v.house].coords.enter.y, Characters.QBHouses[v.house].coords.enter.z),
                }
            end
        end
    end
    if GetResourceState('qs-housing') == 'started' then
        if true then return {} end
        local response 
        if FrameworkSelected == 'QBCore' then
            local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
            response = MySQL.query.await("SELECT * FROM `player_houses` WHERE `citizenid` = ?", {citizenResponse[1].citizenid})
        elseif FrameworkSelected == 'ESX' then
            response = MySQL.query.await("SELECT * FROM `player_houses` WHERE `citizenid` = ?", {Config.Prefix..''..charId..':'..identifier})
        end
        for k,v in ipairs(response) do
            if Characters.QSHouses[v.house] then
                properties[v.house] = {
                    name = v.house,
                    type = "property",
                    label = Characters.QSHouses[v.house].label,
                    coords = vector3(Characters.QSHouses[v.house].coords.enter.x, Characters.QSHouses[v.house].coords.enter.y, Characters.QSHouses[v.house].coords.enter.z),
                }
            end
        end
    end
    if GetResourceState('0r-apartments') == 'started' then
        local response
        if FrameworkSelected == 'QBCore' then
            local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
            response = MySQL.query.await("SELECT * FROM `0resmon_apartment_rooms` WHERE `citizenid` = ?", {citizenResponse[1].citizenid})
        elseif FrameworkSelected == 'ESX' then
            response = MySQL.query.await("SELECT * FROM `0resmon_apartment_rooms` WHERE `citizenid` = ?", {Config.Prefix..''..charId..':'..identifier})
        end
        if not response then debugPrint("[CONVERT PROPERTIES] No appartments are owned.") return {} end
        for k,v in ipairs(response) do
            if Apartments.Locations[v.apartment_id] then
                local apartmentData = Apartments.Locations[v.apartment_id]
                properties[tostring(v.id)] = {
                    name = v.id,
                    type = "property",
                    label = apartmentData.label,
                    coords = vector3(apartmentData.coords.enter.x, apartmentData.coords.enter.y, apartmentData.coords.enter.z),
                }
            end
        end
    end
    if GetResourceState('ps-housing') == 'started' then
        --only for QBCore
        local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
        local query = "SELECT * FROM `properties` WHERE `owner_citizenid` = ?"
        response = MySQL.query.await(query, {citizenResponse[1].citizenid})
        for k,v in ipairs(response) do
            local coords = json.decode(v.door_data)
            if coords and coords.x and coords.y and coords.z then
                properties['house_'..v.property_id] = {
                    name = 'house_'..v.property_id,
                    type = "property",
                    label = v.apartment,
                    coords = vector3(coords.x, coords.y, coords.z),
                }
            end
        end
    end
    if GetResourceState('bcs_housing') == 'started' then
        local response
        local identifier_to_use

        if FrameworkSelected == 'ESX' then
            identifier_to_use = Config.Prefix..''..charId..':'..identifier
            response = exports['bcs_housing']:GetOwnedHomes(identifier_to_use)
        elseif FrameworkSelected == 'QBCore' then
            local citizenResponse = MySQL.query.await("SELECT `citizenid` FROM `players` WHERE `license` = '"..identifier.."' AND `cid` = ?", {charId})
            if citizenResponse and citizenResponse[1] then
                identifier_to_use = citizenResponse[1].citizenid
                response = exports['bcs_housing']:GetOwnedHomes(identifier_to_use)
            end
        end

        local ownedResponse = MySQL.query.await([[
            SELECT h.*, h.data as houseData, ho.owner 
            FROM house h 
            INNER JOIN house_owned ho ON h.identifier = ho.identifier 
            WHERE ho.owner = ?
        ]], {identifier_to_use})

        if ownedResponse then
            for k,v in ipairs(ownedResponse) do
                if v.complex == 'Flat' then
                    local houseData = json.decode(v.houseData)
                    if houseData and houseData.flat and houseData.flat.coords then
                        properties[v.identifier] = {
                            name = v.identifier,
                            type = "property",
                            label = v.name,
                            coords = vector3(houseData.flat.coords.x, houseData.flat.coords.y, houseData.flat.coords.z),
                        }
                    end
                elseif v.entry and v.entry ~= 'null' then
                    local entryCoords = type(v.entry) == 'string' and json.decode(v.entry) or v.entry
                    if entryCoords then
                        properties[v.identifier] = {
                            name = v.identifier,
                            type = "property",
                            label = v.name,
                            coords = vector3(entryCoords.x, entryCoords.y, entryCoords.z),
                        }
                    end
                end
            end
        end

        local apartmentResponse = MySQL.query.await([[
            SELECT h.*, ha.apartment 
            FROM house h 
            INNER JOIN house_apartment ha ON h.identifier = ha.identifier 
            WHERE ha.owner = ? AND h.complex = 'Apartment'
        ]], {identifier_to_use})

        if apartmentResponse then
            for k,v in ipairs(apartmentResponse) do
                if v.entry and v.entry ~= 'null' then
                    local entryCoords = type(v.entry) == 'string' and json.decode(v.entry) or v.entry
                    if entryCoords then
                        properties[v.identifier..'_'..v.apartment] = {
                            name = v.identifier..'_'..v.apartment,
                            type = "property",
                            label = v.name..' - Unit '..v.apartment
                        }
                    end
                end
            end
        end
    end
    
    if Config.DebugTimers then
        print('[CHARACTERS.ConvertProperties] Took: ^3'..(GetGameTimer() - startTime)..'ms^7')
    end
    
    debugPrint("[^2CHARACTERS.CONVERTPROPERTIES^7] Player properties checked.")
    return properties
end

Inside that function you need to add statement for your housing and parse proper object:

Last updated