mirror of
https://github.com/jimathy/jim_bridge.git
synced 2026-08-17 05:56:02 +01:00
Testing a rework _loaders.lua
Uses more specific functions per framework now for player loading in and out and optimizes use of them
This commit is contained in:
@@ -27,6 +27,132 @@ local function _debouncedRun(func)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local frameworkLoadFunc = {
|
||||||
|
{ framework = Exports.QBXExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', func)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('QBCore:Client:OnPlayerUnload', func)
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
local startTime = GetGameTimer()
|
||||||
|
while not LocalPlayer.state.isLoggedIn and (GetGameTimer() - startTime) < timeout do
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
return LocalPlayer.state.isLoggedIn
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
{ framework = Exports.QBExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', func)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('QBCore:Client:OnPlayerUnload', func)
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
local startTime = GetGameTimer()
|
||||||
|
while not LocalPlayer.state.isLoggedIn and (GetGameTimer() - startTime) < timeout do
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
return LocalPlayer.state.isLoggedIn
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
{ framework = Exports.ESXExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent("esx:playerLoaded", function()
|
||||||
|
-- make sure shared has loaded (because sql)
|
||||||
|
if waitForSharedLoad() then func() end
|
||||||
|
end)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent("esx:onPlayerLogout", func)
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
local startTime = GetGameTimer()
|
||||||
|
while (GetGameTimer() - startTime) < timeout do
|
||||||
|
local playerData = ESX.GetPlayerData()
|
||||||
|
if playerData and playerData.job then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
{ framework = Exports.OXCoreExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('ox:playerLoaded', func)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('ox:playerLogout', func)
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
if OxPlayer["stateId"] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
while not OxPlayer["stateId"] do
|
||||||
|
Wait(1000)
|
||||||
|
if OxPlayer.get["stateId"] then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
{ framework = Exports.RSGExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('RSGCore:Client:OnPlayerLoaded', func)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('RSGCore:Client:OnPlayerUnload', func)
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
local startTime = GetGameTimer()
|
||||||
|
while not LocalPlayer.state.isLoggedIn and (GetGameTimer() - startTime) < timeout do
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
return LocalPlayer.state.isLoggedIn
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
|
||||||
|
{ framework = Exports.VorpExport,
|
||||||
|
onPlayerLoaded =
|
||||||
|
function(func)
|
||||||
|
RegisterNetEvent('vorp_core:Client:OnPlayerSpawned', func)
|
||||||
|
end,
|
||||||
|
onPlayerUnload =
|
||||||
|
function(func)
|
||||||
|
--??
|
||||||
|
end,
|
||||||
|
waitforLogin =
|
||||||
|
function(timeout)
|
||||||
|
local startTime = GetGameTimer()
|
||||||
|
while not LocalPlayer.state.IsInSession and (GetGameTimer() - startTime) < timeout do
|
||||||
|
Wait(100)
|
||||||
|
end
|
||||||
|
return LocalPlayer.state.IsInSession
|
||||||
|
end,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
-------------------------------------------------------------
|
-------------------------------------------------------------
|
||||||
-- Player Loaded and Unloaded Events
|
-- Player Loaded and Unloaded Events
|
||||||
-------------------------------------------------------------
|
-------------------------------------------------------------
|
||||||
@@ -44,9 +170,6 @@ end
|
|||||||
--- end, true)
|
--- end, true)
|
||||||
--- ```
|
--- ```
|
||||||
function onPlayerLoaded(func, onStart)
|
function onPlayerLoaded(func, onStart)
|
||||||
local onPlayerFramework = ""
|
|
||||||
local loaded = false
|
|
||||||
|
|
||||||
if onStart then
|
if onStart then
|
||||||
onResourceStart(function()
|
onResourceStart(function()
|
||||||
if not waitForLogin() then return end
|
if not waitForLogin() then return end
|
||||||
@@ -59,34 +182,16 @@ function onPlayerLoaded(func, onStart)
|
|||||||
_debouncedRun(func)
|
_debouncedRun(func)
|
||||||
end
|
end
|
||||||
|
|
||||||
if isStarted(QBExport) or isStarted(QBXExport) then
|
for i = 1, #frameworkLoadFunc do
|
||||||
onPlayerFramework = QBExport
|
local data = frameworkLoadFunc[i]
|
||||||
RegisterNetEvent('QBCore:Client:OnPlayerLoaded', handler)
|
jsonPrint(data)
|
||||||
|
if isStarted(data.framework) then
|
||||||
elseif isStarted(ESXExport) then
|
debugPrint("^6Bridge^7: ^2Registering ^3"..data.framework.." ^5onPlayerLoaded^7()")
|
||||||
onPlayerFramework = ESXExport
|
data.onPlayerLoaded(handler)
|
||||||
RegisterNetEvent('esx:playerLoaded', function()
|
return
|
||||||
if waitForSharedLoad() then handler() end
|
end
|
||||||
end)
|
|
||||||
|
|
||||||
elseif isStarted(OXCoreExport) then
|
|
||||||
onPlayerFramework = OXCoreExport
|
|
||||||
RegisterNetEvent('ox:playerLoaded', handler)
|
|
||||||
|
|
||||||
elseif isStarted(RSGExport) then
|
|
||||||
onPlayerFramework = RSGExport
|
|
||||||
RegisterNetEvent('RSGCore:Client:OnPlayerLoaded', handler)
|
|
||||||
|
|
||||||
elseif isStarted(VorpExport) then
|
|
||||||
onPlayerFramework = VorpExport
|
|
||||||
RegisterNetEvent('vorp_core:Client:OnPlayerSpawned', handler)
|
|
||||||
end
|
|
||||||
|
|
||||||
if onPlayerFramework ~= "" then
|
|
||||||
debugPrint("^6Bridge^7: ^2Registering ^3onPlayerLoaded^7()^2 with ^3" .. onPlayerFramework .. "^7")
|
|
||||||
else
|
|
||||||
print("^4ERROR^7: No supported core detected for onPlayerLoaded - Check starter.lua")
|
|
||||||
end
|
end
|
||||||
|
print("^1ERROR^7: ^1No supported core detected for onPlayerLoaded - Check starter.lua^7")
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Executes a function when the player character is unloaded.
|
--- Executes a function when the player character is unloaded.
|
||||||
@@ -99,15 +204,15 @@ end
|
|||||||
--- end)
|
--- end)
|
||||||
--- ```
|
--- ```
|
||||||
function onPlayerUnload(func)
|
function onPlayerUnload(func)
|
||||||
debugPrint("^6Bridge^7: ^2Registering ^3onPlayerUnload^7()")
|
for i = 1, #frameworkLoadFunc do
|
||||||
RegisterNetEvent('QBCore:Client:OnPlayerUnload', function() func() end)
|
local data = frameworkLoadFunc[i]
|
||||||
|
if isStarted(data.framework) then
|
||||||
RegisterNetEvent('ox:playerLogout', function() func() end)
|
debugPrint("^6Bridge^7: ^2Registering ^3"..data.framework.." ^5onPlayerUnload^7()")
|
||||||
|
data.onPlayerUnload(func)
|
||||||
RegisterNetEvent('RSGCore:Client:OnPlayerUnload', function() func() end)
|
return
|
||||||
|
end
|
||||||
RegisterNetEvent('esx:onPlayerLogout', function() func() end)
|
end
|
||||||
|
print("^1ERROR^7: ^1No supported core detected for onPlayerUnload - Check starter.lua^7")
|
||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------------------
|
-------------------------------------------------------------
|
||||||
@@ -166,51 +271,18 @@ end
|
|||||||
--- @usage
|
--- @usage
|
||||||
--- waitForLogin()
|
--- waitForLogin()
|
||||||
function waitForLogin()
|
function waitForLogin()
|
||||||
local timeout = 10000 -- 10 seconds in milliseconds
|
for i = 1, #frameworkLoadFunc do
|
||||||
local startTime = GetGameTimer()
|
local data = frameworkLoadFunc[i]
|
||||||
local loggedIn = false
|
if isStarted(data.framework) then
|
||||||
|
debugPrint("^6Bridge^7: ^2Waiting for ^3"..data.framework.."^2 player login^7.")
|
||||||
if isStarted(ESXExport) then
|
local result = data.waitforLogin(10000)
|
||||||
while (GetGameTimer() - startTime) < timeout do
|
if result == true then
|
||||||
local playerData = ESX.GetPlayerData()
|
debugPrint("^6Bridge^7: ^3"..data.framework.."^2 Player Login Detected^7.")
|
||||||
if playerData and playerData.job then
|
else
|
||||||
loggedIn = true
|
print("^4Error^7: ^2Timeout reached while waiting for player login^7.")
|
||||||
break
|
|
||||||
end
|
end
|
||||||
Wait(100)
|
return result
|
||||||
end
|
end
|
||||||
elseif isStarted(OXCoreExport) then
|
|
||||||
if OxPlayer["stateId"] then
|
|
||||||
loggedIn = true
|
|
||||||
end
|
|
||||||
while not OxPlayer["stateId"] do
|
|
||||||
Wait(1000)
|
|
||||||
debugPrint("Waiting for stateId to class as logged in")
|
|
||||||
if OxPlayer.get["stateId"] then
|
|
||||||
loggedIn = true
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif isStarted(VorpExport) then
|
|
||||||
-- For other frameworks, use LocalPlayer.state.isLoggedIn.
|
|
||||||
while not LocalPlayer.state.IsInSession and (GetGameTimer() - startTime) < timeout do
|
|
||||||
Wait(100)
|
|
||||||
end
|
|
||||||
loggedIn = LocalPlayer.state.IsInSession
|
|
||||||
else
|
|
||||||
-- For other frameworks, use LocalPlayer.state.isLoggedIn.
|
|
||||||
while not LocalPlayer.state.isLoggedIn and (GetGameTimer() - startTime) < timeout do
|
|
||||||
Wait(100)
|
|
||||||
end
|
|
||||||
loggedIn = LocalPlayer.state.isLoggedIn
|
|
||||||
end
|
|
||||||
|
|
||||||
if not loggedIn then
|
|
||||||
print("^4Error^7: ^2Timeout reached while waiting for player login^7.")
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
debugPrint("^6Bridge^7: ^2Player Login Detected^7.")
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user