Merge pull request #782 from BerkieBb/player-method

feat(server/player): AddPlayerMethod
This commit is contained in:
Kakarot
2022-08-10 12:34:03 -05:00
committed by GitHub
+33
View File
@@ -512,6 +512,10 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
return self.PlayerData.items[slot]
end
function self.Functions.AddMethod(methodName, handler)
self.Functions[methodName] = handler
end
function self.Functions.Save()
if self.Offline then
QBCore.Player.SaveOffline(self.PlayerData)
@@ -537,6 +541,35 @@ function QBCore.Player.CreatePlayer(PlayerData, Offline)
end
end
-- Add a new function to the Functions table of the player class
-- Use-case:
--[[
AddEventHandler('QBCore:Server:PlayerLoaded', function(player)
QBCore.Functions.AddPlayerMethod(player.PlayerData.source, "functionName", function(oneArg, orMore)
-- do something here
end)
end)
]]
function QBCore.Functions.AddPlayerMethod(ids, methodName, handler)
local idType = type(ids)
if idType == "number" then
if ids == -1 then
for _, v in pairs(QBCore.Players) do
v.Functions.AddMethod(methodName, handler)
end
else
if not QBCore.Players[ids] then return end
QBCore.Players[ids].Functions.AddMethod(methodName, handler)
end
elseif idType == "table" and table.type(ids) == "array" then
for i = 1, #ids do
QBCore.Functions.AddPlayerMethod(ids[i], methodName, handler)
end
end
end
-- Save player info to database (make sure citizenid is the primary key in your database)
function QBCore.Player.Save(source)