Merge pull request #87 from JericoFX/dev-1
Add GetPlayer functions to framework and New FUnctions to Utility
This commit is contained in:
4 files changed
+118
No files matched your search
@@ -404,6 +404,97 @@ function Utility.RemovePoint(pointID)
|
|||||||
return Point.Remove(pointID)
|
return Point.Remove(pointID)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Simple switch-case function
|
||||||
|
---@generic T
|
||||||
|
---@param value T The value to match against the cases
|
||||||
|
---@param cases table<T|false, fun(): any> Table with case functions and an optional default (false key)
|
||||||
|
---@return any|false result The return value of the matched case function, or false if none matched
|
||||||
|
function Utility.Switch(value, cases)
|
||||||
|
local caseFunc = cases[value] or cases[false]
|
||||||
|
|
||||||
|
if caseFunc and type(caseFunc) == "function" then
|
||||||
|
local ok, result = pcall(caseFunc)
|
||||||
|
return ok and result or false
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
local vehicleType = "boat"
|
||||||
|
|
||||||
|
local result = Utility.Switch(vehicleType, {
|
||||||
|
car = function()
|
||||||
|
print("This is a Car")
|
||||||
|
-- Car-related logic here
|
||||||
|
return "Car logic executed"
|
||||||
|
end,
|
||||||
|
bike = function()
|
||||||
|
print("This is a Bike")
|
||||||
|
-- Bike-related logic here
|
||||||
|
return "Bike logic executed"
|
||||||
|
end,
|
||||||
|
[false] = function()
|
||||||
|
print("Unknown vehicle type. Running default logic.")
|
||||||
|
-- Default logic here
|
||||||
|
return "Default logic executed"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
print("Result:", result)
|
||||||
|
|
||||||
|
]]
|
||||||
|
|
||||||
|
|
||||||
|
--- Pattern match-like function
|
||||||
|
---@generic T
|
||||||
|
---@param value T The value to match
|
||||||
|
---@param patterns table<T|fun(T):boolean|false, fun(): any> A list of matchers and their handlers
|
||||||
|
---@return any|false result The result of the first matched case, or false if none
|
||||||
|
function Utility.Match(value, patterns)
|
||||||
|
for pattern, handler in pairs(patterns) do
|
||||||
|
if type(pattern) == "function" then
|
||||||
|
local ok, matched = pcall(pattern, value)
|
||||||
|
if ok and matched then
|
||||||
|
local success, result = pcall(handler)
|
||||||
|
return success and result or false
|
||||||
|
end
|
||||||
|
elseif pattern == value then
|
||||||
|
local success, result = pcall(handler)
|
||||||
|
return success and result or false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if patterns[false] then
|
||||||
|
local ok, result = pcall(patterns[false])
|
||||||
|
return ok and result or false
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
local input = 42
|
||||||
|
|
||||||
|
local result = Utility.Match(input, {
|
||||||
|
[function(v) return v < 0 end] = function()
|
||||||
|
print("Negative number")
|
||||||
|
return "Negative"
|
||||||
|
end,
|
||||||
|
[function(v) return v % 2 == 0 end] = function()
|
||||||
|
print("Even number")
|
||||||
|
return "Even"
|
||||||
|
end,
|
||||||
|
[false] = function()
|
||||||
|
print("No match")
|
||||||
|
return "Default"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
print("Result:", result)
|
||||||
|
|
||||||
|
]]
|
||||||
|
|
||||||
AddEventHandler('onResourceStop', function(resource)
|
AddEventHandler('onResourceStop', function(resource)
|
||||||
if resource ~= GetCurrentResourceName() then return end
|
if resource ~= GetCurrentResourceName() then return end
|
||||||
for _, blip in pairs(blipIDs) do
|
for _, blip in pairs(blipIDs) do
|
||||||
|
|||||||
@@ -16,6 +16,15 @@ Framework.GetPlayerDob = function(src)
|
|||||||
return dob
|
return dob
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- This will return a table of the players data
|
||||||
|
---@param src any
|
||||||
|
---@return table | nil
|
||||||
|
Framework.GetPlayer = function(src)
|
||||||
|
local xPlayer = ESX.GetPlayerFromId(src)
|
||||||
|
if not xPlayer then return end
|
||||||
|
return xPlayer
|
||||||
|
end
|
||||||
|
|
||||||
-- Framework.GetPlayerIdentifier(src)
|
-- Framework.GetPlayerIdentifier(src)
|
||||||
-- Returns the citizen ID of the player.
|
-- Returns the citizen ID of the player.
|
||||||
Framework.GetPlayerIdentifier = function(src)
|
Framework.GetPlayerIdentifier = function(src)
|
||||||
|
|||||||
@@ -19,6 +19,15 @@ Framework.GetPlayerIdentifier = function(src)
|
|||||||
return playerData.citizenid
|
return playerData.citizenid
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Returns the player data of the specified source.
|
||||||
|
---@param src any
|
||||||
|
---@return table | nil
|
||||||
|
Framework.GetPlayer = function(src)
|
||||||
|
local player = QBCore.Functions.GetPlayer(src)
|
||||||
|
if not player then return end
|
||||||
|
return player
|
||||||
|
end
|
||||||
|
|
||||||
Framework.GetFrameworkJobs = function()
|
Framework.GetFrameworkJobs = function()
|
||||||
local jobs = {}
|
local jobs = {}
|
||||||
for k, v in pairs(QBCore.Shared.Jobs) do
|
for k, v in pairs(QBCore.Shared.Jobs) do
|
||||||
|
|||||||
@@ -21,6 +21,15 @@ Framework.GetPlayerDob = function(src)
|
|||||||
return playerData.charinfo.birthdate
|
return playerData.charinfo.birthdate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Returns the player data of the specified source.
|
||||||
|
---@param src any
|
||||||
|
---@return table | nil
|
||||||
|
Framework.GetPlayer = function(src)
|
||||||
|
local player = QBox:GetPlayer(src)
|
||||||
|
if not player then return end
|
||||||
|
return player
|
||||||
|
end
|
||||||
|
|
||||||
---Returns a table of the jobs in the framework.
|
---Returns a table of the jobs in the framework.
|
||||||
---@return table
|
---@return table
|
||||||
Framework.GetFrameworkJobs = function()
|
Framework.GetFrameworkJobs = function()
|
||||||
|
|||||||
Reference in new issue
Block a user