add automatic framework detection

This commit is contained in:
kac5a
2022-09-23 21:43:19 +02:00
parent 181f2111a3
commit e719faa74f
4 changed files with 42 additions and 25 deletions

View File

@@ -6,8 +6,18 @@ local Notification
local QBCore
ESX = nil
local CurrentFramework
if Config.Framework == "esx" then
if GetResourceState("es_extended") == "started" then
CurrentFramework = "esx"
elseif GetResourceState("qb-core") == "started" then
CurrentFramework = "qb"
else
print("^8ERROR: ^3This script only supports ESX and QBCore frameworks, but non of these are not present. Unfortunatelly, you cannot use this script.^7")
return
end
if CurrentFramework == "esx" then
Citizen.CreateThread(function()
while ESX == nil do
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
@@ -40,7 +50,7 @@ if Config.Framework == "esx" then
ESX.ShowNotification(msg)
end
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
TriggerCallback = function (name, cb, ...)
QBCore.Functions.TriggerCallback(name, cb, ...)
@@ -49,8 +59,6 @@ elseif Config.Framework == "qb" then
Notification = function (msg)
QBCore.Functions.Notify(msg)
end
else
print("^8ERROR: ^3Unsupported or misspelled framework^7")
end
function holdDocument(shouldHold)
@@ -114,9 +122,11 @@ RegisterNUICallback('hideDocument', function(_, cb)
cb({})
end)
RegisterCommand(Config.Command, function()
toggleNuiFrame(true, true)
end)
if CurrentFramework then
RegisterCommand(Config.Command, function()
toggleNuiFrame(true, true)
end)
end
RegisterNUICallback('hideFrame', function(_, cb)
toggleNuiFrame(false, false)
@@ -124,11 +134,11 @@ RegisterNUICallback('hideFrame', function(_, cb)
end)
RegisterNUICallback('getPlayerJob', function(data, cb)
if Config.Framework == "esx" then
if CurrentFramework == "esx" then
local retData <const> = ESX.PlayerData.job
retData.isBoss = retData.grade_name == "boss"
cb(retData)
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
local PlayerJob = QBCore.Functions.GetPlayerData().job
local retData = {
grade = PlayerJob.grade.level,
@@ -146,11 +156,11 @@ RegisterNUICallback('getPlayerJob', function(data, cb)
end)
RegisterNUICallback('getPlayerData', function(data, cb)
if Config.Framework == "esx" then
if CurrentFramework == "esx" then
ESX.TriggerServerCallback('k5_documents:getPlayerData', function(result)
cb(result)
end)
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
local PlayerData = QBCore.Functions.GetPlayerData().charinfo
cb({
firstname = PlayerData.firstname,
@@ -261,9 +271,9 @@ function playerSelector(confirmText)
while selectingPlayer do
local closestPlayer, closestPlayerDistance
if Config.Framework == "esx" then
if CurrentFramework == "esx" then
closestPlayer, closestPlayerDistance = ESX.Game.GetClosestPlayer()
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
closestPlayer, closestPlayerDistance = QBCore.Functions.GetClosestPlayer()
end
local closestPlayerCoords = GetEntityCoords(GetPlayerPed(closestPlayer))

View File

@@ -1,7 +1,5 @@
Config = {}
Config.Framework = "esx" -- "esx" or "qb"
Config.Command = "documents"
Config.PaperProp = {

View File

@@ -2,8 +2,8 @@ fx_version "cerulean"
description "A better document management script"
author "K5 Scripts"
version '1.1.5'
update "Custom document names, template creation set for job grades, bug fixes"
version '1.1.6'
update "Automatic framework detection"
repository 'https://github.com/kac5a/k5_documents'
lua54 'yes'

View File

@@ -1,19 +1,28 @@
local RegisterCallback
ESX = nil
local QBCore
if Config.Framework == "esx" then
local CurrentFramework
if GetResourceState("es_extended") == "started" then
CurrentFramework = "esx"
elseif GetResourceState("qb-core") == "started" then
CurrentFramework = "qb"
else
print("^8ERROR: ^3This script only supports ESX and QBCore frameworks, but non of these are present. Unfortunatelly, you cannot use this script.^7")
return
end
if CurrentFramework == "esx" then
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
RegisterCallback = function (name, fn)
ESX.RegisterServerCallback(name, fn)
end
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
QBCore = exports['qb-core']:GetCoreObject()
RegisterCallback = function (name, fn)
QBCore.Functions.CreateCallback(name, fn)
end
else
print("^8ERROR: ^3Unsupported or misspelled framework^7")
end
Citizen.CreateThread(function()
@@ -273,9 +282,9 @@ end)
function GetPlayerIdentifier(src)
local PlayerIdentifier
if Config.Framework == "esx" then
if CurrentFramework == "esx" then
PlayerIdentifier = ESX.GetPlayerFromId(src).identifier
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
PlayerIdentifier = QBCore.Functions.GetPlayer(src).PlayerData.citizenid
end
return PlayerIdentifier
@@ -283,9 +292,9 @@ end
function GetPlayerJobName(src)
local PlayerJobName
if Config.Framework == "esx" then
if CurrentFramework == "esx" then
PlayerJobName = ESX.GetPlayerFromId(src).job.name
elseif Config.Framework == "qb" then
elseif CurrentFramework == "qb" then
PlayerJobName = QBCore.Functions.GetPlayer(src).PlayerData.job.name
end
return PlayerJobName