Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es_extended): add static player methods #1601

Merged
merged 7 commits into from
Jan 23, 2025
21 changes: 21 additions & 0 deletions [core]/es_extended/imports.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ if not IsDuplicityVersion() then -- Only register this event for the client
return error(('\n^1Error loading module (%s)'):format(external[i]))
end
end
else
ESX.Player = setmetatable({}, {
__call = function(_, src)
if type(src) ~= "number" then
src = ESX.GetPlayerIdFromIdentifier(src)
if not src then
return
end
elseif not ESX.IsPlayerLoaded(src) then
return
end

return setmetatable({src = src}, {
__index = function(self, method)
return function(...)
return exports.es_extended:RunStaticPlayerMethod(self.src, method, ...)
end
end
})
end
})
end

if GetResourceState("ox_lib") == "missing" then
Expand Down
25 changes: 25 additions & 0 deletions [core]/es_extended/server/classes/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
return self.paycheckEnabled
end

---@return boolean
function self.isAdmin()
return Core.IsPlayerAdmin(self.source)
end

---@param coordinates vector4 | vector3 | table
---@return nil
function self.setCoords(coordinates)
Expand Down Expand Up @@ -455,6 +460,12 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
return self.weight
end

---@return number
function self.getSource()
return self.source
end
self.getPlayerId = self.getSource

---@return number
function self.getMaxWeight()
return self.maxWeight
Expand Down Expand Up @@ -944,3 +955,17 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,

return self
end

local function runStaticPlayerMethod(src, method, ...)
local xPlayer = ESX.Players[src]
if not xPlayer then
return
end

if not ESX.IsFunctionReference(xPlayer[method]) then
error(("Attempted to call invalid method on playerId %s: %s"):format(src, method))
end

return xPlayer[method](...)
end
exports("RunStaticPlayerMethod", runStaticPlayerMethod)
12 changes: 12 additions & 0 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,18 @@ function ESX.GetPlayerFromIdentifier(identifier)
return Core.playersByIdentifier[identifier]
end

---@param identifier string
---@return number playerId
function ESX.GetPlayerIdFromIdentifier(identifier)
return Core.playersByIdentifier[identifier]?.source
end

---@param source number
---@return boolean
function ESX.IsPlayerLoaded(source)
return ESX.Players[source] ~= nil
end

---@param playerId number | string
---@return string
function ESX.GetIdentifier(playerId)
Expand Down
Loading