Skip to content

Commit

Permalink
Merge pull request #1601 from Kenshiin13/static-player
Browse files Browse the repository at this point in the history
feat(es_extended): add static player methods
  • Loading branch information
Kenshiin13 authored Jan 23, 2025
2 parents 153212e + 66bf71c commit 2dac443
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
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

0 comments on commit 2dac443

Please sign in to comment.