Files
Elite-Gaming-FiveM/resources/ImperialCAD-main/utils/framework.lua
T
KingMcDonalds b283de77ee fixing k9script
2026-03-15 14:32:40 -07:00

613 lines
20 KiB
Lua

--[[
Imperial Export for FiveM
DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOUR DOING!
]]--
local function getDiscordId(src)
if src == nil then return false end
local identifiers = GetPlayerIdentifiers(src)
for _, v in pairs(identifiers) do
if string.sub(v, 1, 8) == "discord:" then
return string.gsub(v, "discord:", "")
end
end
return false
end
if Config.isQB then
local QBCore = exports['qb-core']:GetCoreObject({'Functions'})
-- Hook into the player character load event
RegisterNetEvent('qb-multicharacter:server:loadUserData')
AddEventHandler('qb-multicharacter:server:loadUserData', function(cData)
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("✅ QBCore Load User Detected, Loading Character Data: " .. json.encode(cData) .. " | Source: " .. src)
end
local charinfo = cData.charinfo
local fname = charinfo.firstname or "N?A"
local lname = charinfo.lastname or "N?A"
local gender = charinfo.gender or "N?A"
local dob = charinfo.birthdate or "N?A"
local phone = charinfo.phone or "N?A"
-- Extract required values
local citizenid = cData.citizenid
local commId = GetConvar("imperial_community_id", "")
if not citizenid or citizenid == "" then
print("❌ ERROR: Citizen ID is missing!")
return
end
if not commId or commId == "" then
print("❌ ERROR: Community ID is missing! Check your server config.")
return
end
-- Check if character exists in CAD
exports["ImperialCAD"]:GetCharacter(citizenid, commId, function(success, resultData)
if success then
print("✅ Character Data Found in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(resultData))
end
local data = {
ssn = resultData.ssn,
name = resultData.Name,
address = resultData.address,
age = resultData.age
}
TriggerClientEvent('ImperialCAD:setActiveCiv', src, data)
else
if Config.QBRegCurrent then -- If QBRegCurrent is true then
if Config.debug then
print("⚠️ Character NOT found in CAD. Creating new entry...")
end
else -- If QBRegCurrent is not true then (false)
if Config.debug then
print("⚠️ Character NOT found in CAD. Will not Create new entry config is set to false")
end
return end
if gender == 0 then
gender = "MALE"
elseif gender == 1 then
gender = "FEMALE"
end
-- If character isnt found, create a new one
exports["ImperialCAD"]:NewCharacter({
users_discordID = discordId,
Fname = fname,
Mname = "",
Lname = lname,
Birthdate = dob,
race = "nil",
hairC = "nil",
eyeC = "nil",
height = "6'",
weight = "150",
postal = nil,
address = "nil",
gender = gender,
city = "nil",
county = "nil",
phonenum = phone,
dlstatus = "VALID",
citizenid = citizenid
}, function(createSuccess, createResult)
if createSuccess then
print("✅ New Character Created in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(createResult))
end
local eventdata = json.decode(createResult)
local data = {
ssn = eventdata.response.ssn,
name = eventdata.response.name,
address = "Unkown Address",
age = eventdata.response.age
}
TriggerClientEvent('ImperialCAD:setActiveCiv', src, data)
else
print("⚠️ ERROR: Failed to create character in CAD for CitizenID: " .. citizenid)
end
end)
end
end)
end)
-- Hook into the character creation event
RegisterNetEvent('qb-multicharacter:server:createCharacter')
AddEventHandler('qb-multicharacter:server:createCharacter', function(cData)
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("Received Character Data (Pre-Delay): " .. json.encode(cData))
end
-- Wait a few seconds for the data to be properly created
Wait(3000) -- 3 seconds
-- Fetch the updated player data from QBCore
local Player = QBCore.Functions.GetPlayer(src)
if Player then
local citizenid = Player.PlayerData.citizenid
local charinfo = Player.PlayerData.charinfo
local firstname = cData.firstname
local lastname = cData.lastname
local birthdate = cData.birthdate
local gender = cData.gender
local nationality = cData.nationality
local phone = cData.phone
local account = cData.account
if gender == 0 then
gender = "MALE"
elseif gender == 1 then
gender = "FEMALE"
end
if Config.debug then
print("✅ Character Created After Delay:")
print("Citizen ID: " .. citizenid)
print("Discord ID: " .. discordId)
print("First Name: " .. firstname)
print("Last Name: " .. lastname)
print("Birthdate: " .. birthdate)
print("Gender: " .. gender)
end
-- Call the NewCharacter function to send data to the API
exports["ImperialCAD"]:NewCharacter({
users_discordID = discordId,
Fname = firstname,
Mname = "",
Lname = lastname,
Birthdate = birthdate,
race = "nil",
hairC = "nil",
eyeC = "nil",
height = "6'",
weight = "150",
postal = nil,
address = "nil",
gender = gender,
city = "nil",
county = "nil",
phonenum = phone,
dlstatus = "VALID",
citizenid = citizenid
}, function(success, resultData)
if success then
print("✅ Character Created in CAD")
if Config.debug then
print("Result Data: " .. json.encode(resultData))
end
local eventdate = json.decode(resultData)
local data = {
ssn = eventdate.response.ssn,
name = eventdate.response.name,
address = "Unkown Address",
age = eventdate.response.age
}
TriggerClientEvent('ImperialCAD:setActiveCiv', src, data)
else
print("⚠️ ERROR: Could not create character in CAD.")
end
end)
else
print("⚠️ ERROR: Could not retrieve player data after delay!")
end
end)
-- Hook into the character deletion event
RegisterNetEvent('qb-multicharacter:server:deleteCharacter')
AddEventHandler('qb-multicharacter:server:deleteCharacter', function(citizenid)
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("Received Character Deletion Request: " .. citizenid)
end
exports["ImperialCAD"]:DeleteCharacter({
users_discordID = discordId,
citizenid = citizenid
}, function(success, resultData)
if success then
print("✅ Character was deleted in CAD")
else
print("⚠️ ERROR: Could not delete character in CAD.")
end
end)
end)
RegisterNetEvent('ImperialCAD:QBFramework:CreateVehicle')
AddEventHandler('ImperialCAD:QBFramework:CreateVehicle', function(data, source)
local src = source
local discordId = getDiscordId(src)
local vehicle = data.vehicle
local plate = data.plate
local color = data.color
local make = data.make
if Config.debug then
print("✅ Vehicle purchase recevied: " .. vehicle .. " Purchased by " .. data.ssn .. " | Plate: " .. plate .. " Color is " .. color .. " | make is " .. make)
end
exports["ImperialCAD"]:CreateVehicle({
ssn = data.ssn,
model = data.vehicle,
plate = data.plate,
year = "2015",
make = make,
color = color
}, function(success, resultData)
if success then
print("✅ Vehicle registered successfully to CAD.")
end
end)
end)
end
-- NAT2K15 Integration
if Config.isNAT2K15 then
FWN = Config.resourceName
NAT = exports[FWN]:getServerFunctions()
-- Hook into the player character load event
RegisterNetEvent("NAT2K15:CHECKSQL")
AddEventHandler("NAT2K15:CHECKSQL", function(steam, discord, first_name, last_name, twt, dept, dob, gender, data, clientFramework)
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("✅ NatFW Load User Detected, Loading Character Data: " .. json.encode(data) .. " | Source: " .. src)
end
local charinfo = data
local fname = first_name
local lname = last_name
local gender = gender
local dob = dob
local phone = ""
-- Extract required values
local citizenid = data.char_id
local commId = GetConvar("imperial_community_id", "")
if not citizenid or citizenid == "" then
print("❌ ERROR: Citizen ID is missing!")
return
end
if not commId or commId == "" then
print("❌ ERROR: Community ID is missing! Check your server config.")
return
end
-- Check if character exists in CAD
if Config.debug then
print("Checking for character in CAD with CitizenID: " .. citizenid)
end
exports["ImperialCAD"]:GetCharacter(citizenid, commId, function(success, resultData)
if success then
print("✅ Character Data Found in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(resultData))
end
else
print("⚠️ Character NOT found in CAD. Creating new entry...")
if gender == "Male" then
gender = "MALE"
elseif gender == "Female" then
gender = "FEMALE"
end
-- If character isnt found, create a new one
exports["ImperialCAD"]:NewCharacter({
users_discordID = discordId,
Fname = fname,
Mname = "",
Lname = lname,
Birthdate = dob,
race = "nil",
hairC = "nil",
eyeC = "nil",
height = "6'",
weight = "150",
postal = nil,
address = "nil",
gender = gender,
city = "nil",
county = "nil",
phonenum = phone,
dlstatus = "VALID",
citizenid = citizenid
}, function(createSuccess, createResult)
if createSuccess then
print("✅ New Character Created in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(createResult))
end
else
print("⚠️ ERROR: Failed to create character in CAD for CitizenID: " .. citizenid)
end
end)
end
end)
end)
-- Hook into the character deletion event
RegisterNetEvent("NAT2K15:DELETEUSER")
AddEventHandler("NAT2K15:DELETEUSER", function(data)
local src = source
local discordId = getDiscordId(src)
local citizenid = data.char_id
if Config.debug then
print("Received Character Deletion Request: " .. citizenid)
end
exports["ImperialCAD"]:DeleteCharacter({
users_discordID = discordId,
citizenid = citizenid
}, function(success, resultData)
if success then
print("✅ Character was deleted in CAD")
else
print("⚠️ ERROR: Could not delete character in CAD.")
end
end)
end)
end -- end of nat
if Config.isQBX then -- Start of QBX, nothing in this works if not QBX
-- Hook into the player character load event
RegisterNetEvent('QBCore:Server:OnPlayerLoaded')
AddEventHandler('QBCore:Server:OnPlayerLoaded', function()
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("✅ QBXCore Load User Detected, Requesting Character Data for Source: "..src)
end
local data = exports.qbx_core:GetPlayer(source)
local cData = data.PlayerData
if Config.debug then
print('Recieved character data for '..src.." character data is: "..json.encode(cData))
end
if not cData or cData == nil then
print("Uhmmm, no information was found or the script failed. Returning instead")
return end
local charinfo = cData.charinfo
local fname = charinfo.firstname or "N?A"
local lname = charinfo.lastname or "N?A"
local gender = charinfo.gender or "N?A"
local dob = charinfo.birthdate or "N?A"
local phone = charinfo.phone or "N?A"
-- Extract required values
local citizenid = cData.citizenid
local commId = GetConvar("imperial_community_id", "")
if not citizenid or citizenid == "" then
print("❌ ERROR: Citizen ID is missing!")
return
end
if not commId or commId == "" then
print("❌ ERROR: Community ID is missing! Check your server config.")
return
end
-- Check if character exists in CAD
exports["ImperialCAD"]:GetCharacter(citizenid, commId, function(success, resultData)
if success then
print("✅ Character Data Found in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(resultData))
end
local res = resultData.response
local data = {
ssn = res.ssn,
name = res.Name,
address = res.address,
age = res.age
}
TriggerClientEvent('ImperialCAD:setActiveCiv', src, data)
else
if Config.debug then
print("⚠️ Character NOT found in CAD. Creating new entry...")
end
if gender == 0 then
gender = "MALE"
elseif gender == 1 then
gender = "FEMALE"
end
-- If character isnt found, create a new one
exports["ImperialCAD"]:NewCharacter({
users_discordID = discordId,
Fname = fname,
Mname = "",
Lname = lname,
Birthdate = dob,
race = "nil",
hairC = "nil",
eyeC = "nil",
height = "6'",
weight = "150",
postal = nil,
address = "nil",
gender = gender,
city = "nil",
county = "nil",
phonenum = phone,
dlstatus = "VALID",
citizenid = citizenid
}, function(createSuccess, createResult)
if createSuccess then
print("New Character Created in CAD for CitizenID: " .. citizenid)
if Config.debug then
print("Result Data: " .. json.encode(createResult))
end
local eventdata = json.decode(createResult)
local data = {
ssn = eventdata.response.ssn,
name = eventdata.response.name,
address = "Unknown Address",
age = eventdata.response.age
}
TriggerClientEvent('ImperialCAD:setActiveCiv', src, data)
else
print("⚠️ ERROR: Failed to create character in CAD for CitizenID: " .. citizenid)
end
end)
end
end)
end)
-- Hook into the character deletion event
RegisterNetEvent('qbx_core:server:deleteCharacter')
AddEventHandler('qbx_core:server:deleteCharacter', function(citizenid)
local src = source
local discordId = getDiscordId(src)
if Config.debug then
print("Received Character Deletion Request: " .. citizenid)
end
exports["ImperialCAD"]:DeleteCharacter({
users_discordID = discordId,
citizenid = citizenid
}, function(success, resultData)
if success then
print("✅ Character was deleted in CAD")
else
print("⚠️ ERROR: Could not delete character in CAD.")
end
end)
end)
-- Very hacky lazy workaround - I need to improve this | Purchasing
RegisterNetEvent('qbx_vehicleshop:server:buyShowroomVehicle')
AddEventHandler('qbx_vehicleshop:server:buyShowroomVehicle', function()
if Config.debug then print("Received QBX create new vehicle, registering for "..source) end
TriggerClientEvent('qbx_vehicleshop:imperial:client:boughtshowroomvehicle', source)
end)
-- Very hacky lazy workaround - I need to improve this | Financing
RegisterNetEvent('qbx_vehicleshop:server:financeVehicle')
AddEventHandler('qbx_vehicleshop:server:financeVehicle', function(downPayment, paymentAmount, buyVehicle)
if Config.debug then print("Received QBX create new vehicle, finance") end
TriggerClientEvent('qbx_vehicleshop:imperial:client:boughtshowroomvehicle', source)
end)
--Another lazy hacky method, but vehicle financed by sales guy?
RegisterNetEvent('qbx_vehicleshop:server:sellfinanceVehicle')
AddEventHandler('qbx_vehicleshop:server:sellfinanceVehicle', function(downPayment, paymentAmount, vehicle, playerId)
if Config.debug then print("Received QBX create new vehicle, finance sell by player") end
TriggerClientEvent('qbx_vehicleshop:imperial:client:boughtshowroomvehicle', playerId)
end)
--Another lazy hacky method, but vehicle purchase by sales guy?
RegisterNetEvent('qbx_vehicleshop:server:sellShowroomVehicle')
AddEventHandler('qbx_vehicleshop:server:sellShowroomVehicle', function(vehicle, playerId)
if Config.debug then print("Received QBX create new vehicle, purchase sell by player") end
TriggerClientEvent('qbx_vehicleshop:imperial:client:boughtshowroomvehicle', playerId)
end)
RegisterNetEvent('ImperialCAD:QBXFramework:CreateVehicle')
AddEventHandler('ImperialCAD:QBXFramework:CreateVehicle', function(data, source)
local src = source
local discordId = getDiscordId(src)
local vehicle = data.vehicle
local plate = data.plate
local color = data.color
local make = data.make
if Config.debug then
print("✅ Vehicle purchase recevied: " .. vehicle .. " Purchased by " .. data.ssn .. " | Plate: " .. plate .. " Color is " .. color .. " | make is " .. make)
end
exports["ImperialCAD"]:CreateVehicle({
ssn = data.ssn,
model = data.vehicle,
plate = data.plate,
year = "2015",
make = make,
color = color
}, function(success, resultData)
if success then
print("✅ Vehicle registered successfully to CAD.")
else
print("Unable to register vehicle to cad for: "..source)
end
end)
end)
end -- end QBX