Skip to main content

ps-mdt

server/main.lua
QBCore.Functions.CreateCallback('mdt:server:GetProfileData', function(source, cb, sentId)
if not sentId then return cb({}) end

local src = source
local PlayerData = GetPlayerData(src)
if not PermCheck(src, PlayerData) then return cb({}) end
local JobType = GetJobType(PlayerData.job.name)
local target = GetPlayerDataById(sentId)
local JobName = PlayerData.job.name

local apartmentData

if not target or not next(target) then return cb({}) end

if type(target.job) == 'string' then target.job = json.decode(target.job) end
if type(target.charinfo) == 'string' then target.charinfo = json.decode(target.charinfo) end
if type(target.metadata) == 'string' then target.metadata = json.decode(target.metadata) end

local licenses = MySQL.query.await("SELECT license FROM licenses WHERE owner = ?", { sentId })
local licencesdata = {}
local p = promise.new()
exports.bcs_licensemanager:GetLicensesList(function(list)
for i = 1, #list do
if licenses then
for j = 1, #licenses do
if list[i].type == licenses[j].license then
licencesdata[list[i].type] = true
end
end
end
if not licencesdata[list[i].type] then
licencesdata[list[i].type] = false
end
end
p:resolve()
end)
Citizen.Await(p)

... -- and so on
server/dbm.lua
function GetPlayerLicenses(identifier)
local response = false
local Player = QBCore.Functions.GetPlayerByCitizenId(identifier)
if Player ~= nil then
local p = promise.new()
exports.bcs_licensemanager:GetLicenses(Player.PlayerData.source, function(list)
local licenses = {}
for k, v in pairs(list) do
licenses[v.type] = true
end
if #licenses == 0 then
licenses = {
['driver_car'] = false,
['weapon'] = false,
}
end
p:resolve(licenses)
end)
return Citizen.Await(p)
else
local result = MySQL.scalar.await('SELECT metadata FROM players WHERE citizenid = @identifier',
{ ['@identifier'] = identifier })
if result ~= nil then
local metadata = json.decode(result)
if metadata["licences"] ~= nil and metadata["licences"] then
return metadata["licences"]
else
return {
['driver_car'] = false,
['weapon'] = false,
}
end
end
end
end

function ManageLicense(identifier, type, status, source)
local Player = QBCore.Functions.GetPlayerByCitizenId(identifier)
local licenseStatus = nil
if status == "give" then
if Player ~= nil then
licenseStatus = true
TriggerClientEvent('LicenseManager:addLicense', Player.PlayerData.source, type)
else
licenseStatus = false
end
elseif status == "revoke" then
licenseStatus = false
exports.bcs_licensemanager:RevokeLicense(identifier, type, source)
end
if Player ~= nil then
local licences = Player.PlayerData.metadata["licences"]
local newLicenses = {}
for k, v in pairs(licences) do
local status = v
if k == type then
status = licenseStatus
end
newLicenses[k] = status
end
Player.Functions.SetMetaData("licences", newLicenses)
else
local licenseType = '$.licences.' .. type
local result = MySQL.query.await(
'UPDATE `players` SET `metadata` = JSON_REPLACE(`metadata`, ?, ?) WHERE `citizenid` = ?',
{ licenseType, licenseStatus, identifier }) --seems to not work on older MYSQL versions, think about alternative
end
end

function UpdateAllLicenses(identifier, incomingLicenses, source)
local Player = QBCore.Functions.GetPlayerByCitizenId(identifier)
if Player ~= nil then
for k, v in pairs(incomingLicenses) do
if v and not exports.bcs_licensemanager:HasLicense(Player.PlayerData.source, k) then
TriggerClientEvent('LicenseManager:addLicense', Player.PlayerData.source, k)
elseif not v and exports.bcs_licensemanager:HasLicense(Player.PlayerData.source, k) then
exports.bcs_licensemanager:RevokeLicense(identifier, k, source)
end
end
Player.Functions.SetMetaData("licences", incomingLicenses)
else
local result = MySQL.scalar.await('SELECT metadata FROM players WHERE citizenid = @identifier',
{ ['@identifier'] = identifier })
result = json.decode(result)

result.licences = result.licences or {
['driver_car'] = false,
['weapon'] = false,
}

for k, _ in pairs(incomingLicenses) do
result.licences[k] = incomingLicenses[k]
if not incomingLicenses[k] then
exports.bcs_licensemanager:RevokeLicense(identifier, k, source)
end
end
MySQL.query.await('UPDATE `players` SET `metadata` = @metadata WHERE citizenid = @citizenid',
{ ['@metadata'] = json.encode(result), ['@citizenid'] = identifier })
end
end

Add src as the argument in both function, find them in server/main.lua

server/main.lua
    ManageLicense(cid, type, status, src)

UpdateAllLicenses(cid, licenses, src)