Skip to content

Commit

Permalink
Merge pull request #1561 from Kenshiin13/cleanup-esx-object
Browse files Browse the repository at this point in the history
reafactor!(es_extended): Cleanup ESX object
  • Loading branch information
Kenshiin13 authored Dec 31, 2024
2 parents f53bbfe + abe6b2d commit da0655f
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 54 deletions.
4 changes: 2 additions & 2 deletions [core]/es_extended/server/bridge/inventory/oxinventory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ if Config.CustomInventory ~= "ox" then return end
MySQL.ready(function()
TriggerEvent("__cfx_export_ox_inventory_Items", function(ref)
if ref then
ESX.Items = ref()
Core.Items = ref()
end
end)

AddEventHandler("ox_inventory:itemList", function(items)
ESX.Items = items
Core.Items = items
end)
end)

Expand Down
6 changes: 3 additions & 3 deletions [core]/es_extended/server/classes/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
---@param count number
---@return boolean
function self.canCarryItem(itemName, count)
if ESX.Items[itemName] then
local currentWeight, itemWeight = self.weight, ESX.Items[itemName].weight
if Core.Items[itemName] then
local currentWeight, itemWeight = self.weight, Core.Items[itemName].weight
local newWeight = currentWeight + (itemWeight * count)

return newWeight <= self.maxWeight
Expand Down Expand Up @@ -527,7 +527,7 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
onDuty = Config.DefaultJobDuty
end

local jobObject, gradeObject = ESX.Jobs[newJob], ESX.Jobs[newJob].grades[grade]
local jobObject, gradeObject = Core.Jobs[newJob], Core.Jobs[newJob].grades[grade]

self.job = {
id = jobObject.id,
Expand Down
12 changes: 6 additions & 6 deletions [core]/es_extended/server/common.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ESX.Players = {}
ESX.Jobs = {}
ESX.JobsPlayerCount = {}
ESX.Items = {}
Core = {}
Core.Players = {}
Core.Items = {}
Core.Jobs = {}
Core.JobsPlayerCount = {}
Core.UsableItemsCallbacks = {}
Core.RegisteredCommands = {}
Core.Pickups = {}
Expand All @@ -14,7 +14,7 @@ Core.playersByIdentifier = {}
Core.vehicleTypesByModel = {}

RegisterNetEvent("esx:onPlayerSpawn", function()
ESX.Players[source].spawned = true
Core.Players[source].spawned = true
end)

if Config.CustomInventory then
Expand All @@ -37,7 +37,7 @@ MySQL.ready(function()
if not Config.CustomInventory then
local items = MySQL.query.await("SELECT * FROM items")
for _, v in ipairs(items) do
ESX.Items[v.name] = { label = v.label, weight = v.weight, rare = v.rare, canRemove = v.can_remove }
Core.Items[v.name] = { label = v.label, weight = v.weight, rare = v.rare, canRemove = v.can_remove }
end
end

Expand Down
38 changes: 19 additions & 19 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion)
if not command.allowConsole and playerId == 0 then
print(("[^3WARNING^7] ^5%s"):format(TranslateCap("commanderror_console")))
else
local xPlayer, error = ESX.Players[playerId], nil
local xPlayer, error = Core.Players[playerId], nil

if command.suggestion then
if command.suggestion.validate then
Expand Down Expand Up @@ -116,7 +116,7 @@ function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion)
error = TranslateCap("commanderror_argumentmismatch_string", k)
end
elseif v.type == "item" then
if ESX.Items[args[k]] then
if Core.Items[args[k]] then
newArgs[v.name] = args[k]
else
error = TranslateCap("commanderror_invaliditem")
Expand Down Expand Up @@ -234,15 +234,15 @@ end
---@param cb? function
---@return nil
function Core.SavePlayers(cb)
local xPlayers <const> = ESX.Players
local xPlayers <const> = Core.Players
if not next(xPlayers) then
return
end

local startTime <const> = os.time()
local parameters = {}

for _, xPlayer in pairs(ESX.Players) do
for _, xPlayer in pairs(Core.Players) do
updateHealthAndArmorInMetadata(xPlayer)
parameters[#parameters + 1] = {
json.encode(xPlayer.getAccounts(true)),
Expand Down Expand Up @@ -294,19 +294,19 @@ end
---@return table
function ESX.GetExtendedPlayers(key, val)
if not key then
return ESX.Table.ToArray(ESX.Players)
return ESX.Table.ToArray(Core.Players)
end

local xPlayers = {}
if type(val) == "table" then
for _, xPlayer in pairs(ESX.Players) do
for _, xPlayer in pairs(Core.Players) do
checkTable(key, val, xPlayer, xPlayers)
end

return xPlayers
end

for _, xPlayer in pairs(ESX.Players) do
for _, xPlayer in pairs(Core.Players) do
if (key == "job" and xPlayer.job.name == val) or xPlayer[key] == val then
xPlayers[#xPlayers + 1] = xPlayer
end
Expand All @@ -327,7 +327,7 @@ function ESX.GetNumPlayers(key, val)
local numPlayers = {}
if key == "job" then
for _, v in ipairs(val) do
numPlayers[v] = (ESX.JobsPlayerCount[v] or 0)
numPlayers[v] = (Core.JobsPlayerCount[v] or 0)
end
return numPlayers
end
Expand All @@ -340,7 +340,7 @@ function ESX.GetNumPlayers(key, val)
end

if key == "job" then
return (ESX.JobsPlayerCount[val] or 0)
return (Core.JobsPlayerCount[val] or 0)
end

return #ESX.GetExtendedPlayers(key, val)
Expand All @@ -349,7 +349,7 @@ end
---@param source number
---@return table
function ESX.GetPlayerFromId(source)
return ESX.Players[tonumber(source)]
return Core.Players[tonumber(source)]
end

---@param identifier string
Expand Down Expand Up @@ -495,9 +495,9 @@ function ESX.RefreshJobs()

if not Jobs then
-- Fallback data, if no jobs exist
ESX.Jobs["unemployed"] = { label = "Unemployed", grades = { ["0"] = { grade = 0, label = "Unemployed", salary = 200, skin_male = {}, skin_female = {} } } }
Core.Jobs["unemployed"] = { label = "Unemployed", grades = { ["0"] = { grade = 0, label = "Unemployed", salary = 200, skin_male = {}, skin_female = {} } } }
else
ESX.Jobs = Jobs
Core.Jobs = Jobs
end
end

Expand All @@ -513,7 +513,7 @@ end
---@param ... any
---@return nil
function ESX.UseItem(source, item, ...)
if ESX.Items[item] then
if Core.Items[item] then
local itemCallback = Core.UsableItemsCallbacks[item]

if itemCallback then
Expand Down Expand Up @@ -549,16 +549,16 @@ end
---@return string?
---@diagnostic disable-next-line: duplicate-set-field
function ESX.GetItemLabel(item)
if ESX.Items[item] then
return ESX.Items[item].label
if Core.Items[item] then
return Core.Items[item].label
else
print(("[^3WARNING^7] Attemting to get invalid Item -> ^5%s^7"):format(item))
end
end

---@return table
function ESX.GetJobs()
return ESX.Jobs
return Core.Jobs
end

---@return table
Expand All @@ -582,7 +582,7 @@ if not Config.CustomInventory then
---@return nil
function ESX.CreatePickup(itemType, name, count, label, playerId, components, tintIndex, coords)
local pickupId = (Core.PickupId == 65635 and 0 or Core.PickupId + 1)
local xPlayer = ESX.Players[playerId]
local xPlayer = Core.Players[playerId]
coords = ((type(coords) == "vector3" or type(coords) == "vector4") and coords.xyz or xPlayer.getCoords(true))

Core.Pickups[pickupId] = { type = itemType, name = name, count = count, label = label, coords = coords }
Expand All @@ -601,7 +601,7 @@ end
---@param grade string
---@return boolean
function ESX.DoesJobExist(job, grade)
return (ESX.Jobs[job] and ESX.Jobs[job].grades[tostring(grade)] ~= nil) or false
return (Core.Jobs[job] and Core.Jobs[job].grades[tostring(grade)] ~= nil) or false
end

---@param playerId string | number
Expand All @@ -612,6 +612,6 @@ function Core.IsPlayerAdmin(playerId)
return true
end

local xPlayer = ESX.Players[playerId]
local xPlayer = Core.Players[playerId]
return (xPlayer and Config.AdminGroups[xPlayer.group] and true) or false
end
38 changes: 19 additions & 19 deletions [core]/es_extended/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ end

if Config.Multichar then
AddEventHandler("esx:onPlayerJoined", function(src, char, data)
while not next(ESX.Jobs) do
while not next(Core.Jobs) do
Wait(50)
end

if not ESX.Players[src] then
if not Core.Players[src] then
local identifier = char .. ":" .. ESX.GetIdentifier(src)
if data then
createESXPlayer(identifier, src, data)
Expand All @@ -85,11 +85,11 @@ if Config.Multichar then
else
RegisterNetEvent("esx:onPlayerJoined", function()
local _source = source
while not next(ESX.Jobs) do
while not next(Core.Jobs) do
Wait(50)
end

if not ESX.Players[_source] then
if not Core.Players[_source] then
onPlayerJoined(_source)
end
end)
Expand Down Expand Up @@ -171,7 +171,7 @@ function loadESXPlayer(identifier, playerId, isNew)
job, grade = "unemployed", "0"
end

local jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
local jobObject, gradeObject = Core.Jobs[job], Core.Jobs[job].grades[grade]

userData.job = {
id = jobObject.id,
Expand All @@ -191,7 +191,7 @@ function loadESXPlayer(identifier, playerId, isNew)
if not Config.CustomInventory then
local inventory = (result.inventory and result.inventory ~= "") and json.decode(result.inventory) or {}

for name, item in pairs(ESX.Items) do
for name, item in pairs(Core.Items) do
local count = inventory[name] or 0
userData.weight += (count * item.weight)

Expand Down Expand Up @@ -258,7 +258,7 @@ function loadESXPlayer(identifier, playerId, isNew)
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.weight, userData.job, userData.loadout, GetPlayerName(playerId), userData.coords, userData.metadata)

GlobalState["playerCount"] = GlobalState["playerCount"] + 1
ESX.Players[playerId] = xPlayer
Core.Players[playerId] = xPlayer
Core.playersByIdentifier[identifier] = xPlayer

-- Identity
Expand Down Expand Up @@ -318,15 +318,15 @@ AddEventHandler("playerDropped", function(reason)
if xPlayer then
TriggerEvent("esx:playerDropped", playerId, reason)
local job = xPlayer.getJob().name
local currentJob = ESX.JobsPlayerCount[job]
ESX.JobsPlayerCount[job] = ((currentJob and currentJob > 0) and currentJob or 1) - 1
local currentJob = Core.JobsPlayerCount[job]
Core.JobsPlayerCount[job] = ((currentJob and currentJob > 0) and currentJob or 1) - 1

GlobalState[("%s:count"):format(job)] = ESX.JobsPlayerCount[job]
GlobalState[("%s:count"):format(job)] = Core.JobsPlayerCount[job]
Core.playersByIdentifier[xPlayer.identifier] = nil

Core.SavePlayer(xPlayer, function()
GlobalState["playerCount"] = GlobalState["playerCount"] - 1
ESX.Players[playerId] = nil
Core.Players[playerId] = nil
end)
end
end)
Expand All @@ -335,20 +335,20 @@ AddEventHandler("esx:playerLoaded", function(_, xPlayer)
local job = xPlayer.getJob().name
local jobKey = ("%s:count"):format(job)

ESX.JobsPlayerCount[job] = (ESX.JobsPlayerCount[job] or 0) + 1
GlobalState[jobKey] = ESX.JobsPlayerCount[job]
Core.JobsPlayerCount[job] = (Core.JobsPlayerCount[job] or 0) + 1
GlobalState[jobKey] = Core.JobsPlayerCount[job]
end)

AddEventHandler("esx:setJob", function(_, job, lastJob)
local lastJobKey = ("%s:count"):format(lastJob.name)
local jobKey = ("%s:count"):format(job.name)
local currentLastJob = ESX.JobsPlayerCount[lastJob.name]
local currentLastJob = Core.JobsPlayerCount[lastJob.name]

ESX.JobsPlayerCount[lastJob.name] = ((currentLastJob and currentLastJob > 0) and currentLastJob or 1) - 1
ESX.JobsPlayerCount[job.name] = (ESX.JobsPlayerCount[job.name] or 0) + 1
Core.JobsPlayerCount[lastJob.name] = ((currentLastJob and currentLastJob > 0) and currentLastJob or 1) - 1
Core.JobsPlayerCount[job.name] = (Core.JobsPlayerCount[job.name] or 0) + 1

GlobalState[lastJobKey] = ESX.JobsPlayerCount[lastJob.name]
GlobalState[jobKey] = ESX.JobsPlayerCount[job.name]
GlobalState[lastJobKey] = Core.JobsPlayerCount[lastJob.name]
GlobalState[jobKey] = Core.JobsPlayerCount[job.name]
end)

AddEventHandler("esx:playerLogout", function(playerId, cb)
Expand All @@ -359,7 +359,7 @@ AddEventHandler("esx:playerLogout", function(playerId, cb)
Core.playersByIdentifier[xPlayer.identifier] = nil
Core.SavePlayer(xPlayer, function()
GlobalState["playerCount"] = GlobalState["playerCount"] - 1
ESX.Players[playerId] = nil
Core.Players[playerId] = nil
if cb then
cb()
end
Expand Down
4 changes: 2 additions & 2 deletions [core]/es_extended/server/modules/createJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local NOTIFY_TYPES = {
}

local function doesJobAndGradesExist(name, grades)
if not ESX.Jobs[name] then
if not Core.Jobs[name] then
return false
end

Expand Down Expand Up @@ -86,7 +86,7 @@ function ESX.CreateJob(name, label, grades)
return success
end

ESX.Jobs[name] = generateNewJobTable(name, label, grades)
Core.Jobs[name] = generateNewJobTable(name, label, grades)

notify("SUCCESS", currentResourceName, 'Job created successfully: `%s`', name)

Expand Down
2 changes: 1 addition & 1 deletion [core]/es_extended/server/modules/npwd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AddEventHandler("onServerResourceStart", function(resource)
if not npwd then
return
end
for _, xPlayer in pairs(ESX.Players) do
for _, xPlayer in pairs(Core.Players) do
npwd:newPlayer({
source = xPlayer.source,
identifier = xPlayer.identifier,
Expand Down
2 changes: 1 addition & 1 deletion [core]/es_extended/server/modules/onesync.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local function getNearbyPlayers(source, closest, distance, ignore)
end
end

for _, xPlayer in pairs(ESX.Players) do
for _, xPlayer in pairs(Core.Players) do
if not ignore or not ignore[xPlayer.source] then
local entity = GetPlayerPed(xPlayer.source)
local coords = GetEntityCoords(entity)
Expand Down
2 changes: 1 addition & 1 deletion [core]/es_extended/server/modules/paycheck.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function StartPayCheck()
CreateThread(function()
while true do
Wait(Config.PaycheckInterval)
for player, xPlayer in pairs(ESX.Players) do
for player, xPlayer in pairs(Core.Players) do
local jobLabel = xPlayer.job.label
local job = xPlayer.job.grade_name
local salary = xPlayer.job.grade_salary
Expand Down
1 change: 1 addition & 0 deletions [core]/esx_multicharacter/server/modules/database.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ MySQL.ready(function()

Database.connected = true

ESX.Jobs = ESX.GetJobs()
while not next(ESX.Jobs) do
Wait(500)
ESX.Jobs = ESX.GetJobs()
Expand Down
2 changes: 2 additions & 0 deletions [core]/esx_multicharacter/server/modules/functions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
ESX.Players = {}

function Server:GetIdentifier(source)
local fxDk = GetConvarInt("sv_fxdkMode", 0)
if fxDk == 1 then
Expand Down

0 comments on commit da0655f

Please sign in to comment.