mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 17:28:53 +00:00
formatting
This commit is contained in:
+211
-198
@@ -121,239 +121,252 @@ if not Config.Multichar then
|
||||
end
|
||||
|
||||
function loadESXPlayer(identifier, playerId, isNew)
|
||||
local userData = { accounts = {}, inventory = {}, job = {}, loadout = {}, playerName = GetPlayerName(playerId),
|
||||
weight = 0, metadata = {} }
|
||||
local result = MySQL.prepare.await(loadPlayer, { identifier })
|
||||
local job, grade, jobObject, gradeObject = result.job, tostring(result.job_grade)
|
||||
local foundAccounts, foundItems = {}, {}
|
||||
|
||||
-- Accounts
|
||||
if result.accounts and result.accounts ~= '' then
|
||||
local accounts = json.decode(result.accounts)
|
||||
|
||||
for account, money in pairs(accounts) do
|
||||
foundAccounts[account] = money
|
||||
end
|
||||
end
|
||||
|
||||
for account, data in pairs(Config.Accounts) do
|
||||
if data.round == nil then
|
||||
data.round = true
|
||||
end
|
||||
local index = #userData.accounts + 1
|
||||
userData.accounts[index] = {
|
||||
name = account,
|
||||
money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0,
|
||||
label = data.label,
|
||||
round = data.round,
|
||||
index = index
|
||||
local userData = {
|
||||
accounts = {},
|
||||
inventory = {},
|
||||
job = {},
|
||||
loadout = {},
|
||||
playerName = GetPlayerName(playerId),
|
||||
weight = 0,
|
||||
metadata = {}
|
||||
}
|
||||
end
|
||||
local result = MySQL.prepare.await(loadPlayer, { identifier })
|
||||
local job, grade, jobObject, gradeObject = result.job, tostring(result.job_grade)
|
||||
local foundAccounts, foundItems = {}, {}
|
||||
|
||||
-- Job
|
||||
if ESX.DoesJobExist(job, grade) then
|
||||
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
|
||||
else
|
||||
print(('[^3WARNING^7] Ignoring invalid job for ^5%s^7 [job: ^5%s^7, grade: ^5%s^7]'):format(identifier, job, grade))
|
||||
job, grade = 'unemployed', '0'
|
||||
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
|
||||
end
|
||||
-- Accounts
|
||||
if result.accounts and result.accounts ~= '' then
|
||||
local accounts = json.decode(result.accounts)
|
||||
|
||||
userData.job.id = jobObject.id
|
||||
userData.job.name = jobObject.name
|
||||
userData.job.label = jobObject.label
|
||||
|
||||
userData.job.grade = tonumber(grade)
|
||||
userData.job.grade_name = gradeObject.name
|
||||
userData.job.grade_label = gradeObject.label
|
||||
userData.job.grade_salary = gradeObject.salary
|
||||
|
||||
userData.job.skin_male = {}
|
||||
userData.job.skin_female = {}
|
||||
|
||||
if gradeObject.skin_male then
|
||||
userData.job.skin_male = json.decode(gradeObject.skin_male)
|
||||
end
|
||||
if gradeObject.skin_female then
|
||||
userData.job.skin_female = json.decode(gradeObject.skin_female)
|
||||
end
|
||||
|
||||
-- Inventory
|
||||
if not Config.OxInventory then
|
||||
if result.inventory and result.inventory ~= '' then
|
||||
local inventory = json.decode(result.inventory)
|
||||
|
||||
for name, count in pairs(inventory) do
|
||||
local item = ESX.Items[name]
|
||||
|
||||
if item then
|
||||
foundItems[name] = count
|
||||
else
|
||||
print(('[^3WARNING^7] Ignoring invalid item ^5"%s"^7 for ^5"%s^7"'):format(name, identifier))
|
||||
end
|
||||
for account, money in pairs(accounts) do
|
||||
foundAccounts[account] = money
|
||||
end
|
||||
end
|
||||
|
||||
for name, item in pairs(ESX.Items) do
|
||||
local count = foundItems[name] or 0
|
||||
if count > 0 then
|
||||
userData.weight = userData.weight + (item.weight * count)
|
||||
for account, data in pairs(Config.Accounts) do
|
||||
if data.round == nil then
|
||||
data.round = true
|
||||
end
|
||||
|
||||
table.insert(userData.inventory,
|
||||
{
|
||||
name = name,
|
||||
count = count,
|
||||
label = item.label,
|
||||
weight = item.weight,
|
||||
usable = Core.UsableItemsCallbacks[name] ~= nil,
|
||||
rare = item.rare,
|
||||
canRemove = item.canRemove
|
||||
})
|
||||
local index = #userData.accounts + 1
|
||||
userData.accounts[index] = {
|
||||
name = account,
|
||||
money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0,
|
||||
label = data.label,
|
||||
round = data.round,
|
||||
index = index
|
||||
}
|
||||
end
|
||||
|
||||
table.sort(userData.inventory, function(a, b)
|
||||
return a.label < b.label
|
||||
end)
|
||||
else
|
||||
if result.inventory and result.inventory ~= '' then
|
||||
userData.inventory = json.decode(result.inventory)
|
||||
-- Job
|
||||
if ESX.DoesJobExist(job, grade) then
|
||||
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
|
||||
else
|
||||
userData.inventory = {}
|
||||
print(('[^3WARNING^7] Ignoring invalid job for ^5%s^7 [job: ^5%s^7, grade: ^5%s^7]'):format(identifier, job,
|
||||
grade))
|
||||
job, grade = 'unemployed', '0'
|
||||
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
|
||||
end
|
||||
end
|
||||
|
||||
-- Group
|
||||
if result.group then
|
||||
if result.group == "superadmin" then
|
||||
userData.group = "admin"
|
||||
print("[^3WARNING^7] ^5Superadmin^7 detected, setting group to ^5admin^7")
|
||||
else
|
||||
userData.group = result.group
|
||||
userData.job.id = jobObject.id
|
||||
userData.job.name = jobObject.name
|
||||
userData.job.label = jobObject.label
|
||||
|
||||
userData.job.grade = tonumber(grade)
|
||||
userData.job.grade_name = gradeObject.name
|
||||
userData.job.grade_label = gradeObject.label
|
||||
userData.job.grade_salary = gradeObject.salary
|
||||
|
||||
userData.job.skin_male = {}
|
||||
userData.job.skin_female = {}
|
||||
|
||||
if gradeObject.skin_male then
|
||||
userData.job.skin_male = json.decode(gradeObject.skin_male)
|
||||
end
|
||||
if gradeObject.skin_female then
|
||||
userData.job.skin_female = json.decode(gradeObject.skin_female)
|
||||
end
|
||||
else
|
||||
userData.group = 'user'
|
||||
end
|
||||
|
||||
-- Loadout
|
||||
if not Config.OxInventory then
|
||||
if result.loadout and result.loadout ~= '' then
|
||||
local loadout = json.decode(result.loadout)
|
||||
-- Inventory
|
||||
if not Config.OxInventory then
|
||||
if result.inventory and result.inventory ~= '' then
|
||||
local inventory = json.decode(result.inventory)
|
||||
|
||||
for name, weapon in pairs(loadout) do
|
||||
local label = ESX.GetWeaponLabel(name)
|
||||
for name, count in pairs(inventory) do
|
||||
local item = ESX.Items[name]
|
||||
|
||||
if label then
|
||||
if not weapon.components then
|
||||
weapon.components = {}
|
||||
if item then
|
||||
foundItems[name] = count
|
||||
else
|
||||
print(('[^3WARNING^7] Ignoring invalid item ^5"%s"^7 for ^5"%s^7"'):format(name, identifier))
|
||||
end
|
||||
if not weapon.tintIndex then
|
||||
weapon.tintIndex = 0
|
||||
end
|
||||
|
||||
table.insert(userData.loadout,
|
||||
{ name = name, ammo = weapon.ammo, label = label, components = weapon.components,
|
||||
tintIndex = weapon.tintIndex })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Position
|
||||
userData.coords = json.decode(result.position) or Config.DefaultSpawns[math.random(#Config.DefaultSpawns)]
|
||||
for name, item in pairs(ESX.Items) do
|
||||
local count = foundItems[name] or 0
|
||||
if count > 0 then
|
||||
userData.weight = userData.weight + (item.weight * count)
|
||||
end
|
||||
|
||||
-- Skin
|
||||
if result.skin and result.skin ~= '' then
|
||||
userData.skin = json.decode(result.skin)
|
||||
else
|
||||
if userData.sex == 'f' then
|
||||
userData.skin = { sex = 1 }
|
||||
table.insert(userData.inventory,
|
||||
{
|
||||
name = name,
|
||||
count = count,
|
||||
label = item.label,
|
||||
weight = item.weight,
|
||||
usable = Core.UsableItemsCallbacks[name] ~= nil,
|
||||
rare = item.rare,
|
||||
canRemove = item.canRemove
|
||||
})
|
||||
end
|
||||
|
||||
table.sort(userData.inventory, function(a, b)
|
||||
return a.label < b.label
|
||||
end)
|
||||
else
|
||||
userData.skin = { sex = 0 }
|
||||
if result.inventory and result.inventory ~= '' then
|
||||
userData.inventory = json.decode(result.inventory)
|
||||
else
|
||||
userData.inventory = {}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Identity
|
||||
if result.firstname and result.firstname ~= '' then
|
||||
userData.firstname = result.firstname
|
||||
userData.lastname = result.lastname
|
||||
userData.playerName = userData.firstname .. ' ' .. userData.lastname
|
||||
if result.dateofbirth then
|
||||
userData.dateofbirth = result.dateofbirth
|
||||
-- Group
|
||||
if result.group then
|
||||
if result.group == "superadmin" then
|
||||
userData.group = "admin"
|
||||
print("[^3WARNING^7] ^5Superadmin^7 detected, setting group to ^5admin^7")
|
||||
else
|
||||
userData.group = result.group
|
||||
end
|
||||
else
|
||||
userData.group = 'user'
|
||||
end
|
||||
if result.sex then
|
||||
userData.sex = result.sex
|
||||
end
|
||||
if result.height then
|
||||
userData.height = result.height
|
||||
end
|
||||
end
|
||||
|
||||
if result.metadata and result.metadata ~= '' then
|
||||
local metadata = json.decode(result.metadata)
|
||||
userData.metadata = metadata
|
||||
end
|
||||
-- Loadout
|
||||
if not Config.OxInventory then
|
||||
if result.loadout and result.loadout ~= '' then
|
||||
local loadout = json.decode(result.loadout)
|
||||
|
||||
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory,
|
||||
userData.weight, userData.job,
|
||||
userData.loadout, userData.playerName, userData.coords, userData.metadata)
|
||||
ESX.Players[playerId] = xPlayer
|
||||
Core.playersByIdentifier[identifier] = xPlayer
|
||||
for name, weapon in pairs(loadout) do
|
||||
local label = ESX.GetWeaponLabel(name)
|
||||
|
||||
if userData.firstname then
|
||||
xPlayer.set('firstName', userData.firstname)
|
||||
xPlayer.set('lastName', userData.lastname)
|
||||
if userData.dateofbirth then
|
||||
xPlayer.set('dateofbirth', userData.dateofbirth)
|
||||
end
|
||||
if userData.sex then
|
||||
xPlayer.set('sex', userData.sex)
|
||||
end
|
||||
if userData.height then
|
||||
xPlayer.set('height', userData.height)
|
||||
end
|
||||
end
|
||||
if label then
|
||||
if not weapon.components then
|
||||
weapon.components = {}
|
||||
end
|
||||
if not weapon.tintIndex then
|
||||
weapon.tintIndex = 0
|
||||
end
|
||||
|
||||
TriggerEvent('esx:playerLoaded', playerId, xPlayer, isNew)
|
||||
|
||||
xPlayer.triggerEvent('esx:playerLoaded',
|
||||
{
|
||||
accounts = xPlayer.getAccounts(),
|
||||
coords = userData.coords,
|
||||
identifier = xPlayer.getIdentifier(),
|
||||
inventory = xPlayer.getInventory(),
|
||||
job = xPlayer.getJob(),
|
||||
loadout = xPlayer.getLoadout(),
|
||||
maxWeight = xPlayer.getMaxWeight(),
|
||||
money = xPlayer.getMoney(),
|
||||
sex = xPlayer.get("sex") or "m",
|
||||
firstName = xPlayer.get("firstName") or "John",
|
||||
lastName = xPlayer.get("lastName") or "Doe",
|
||||
dateofbirth = xPlayer.get("dateofbirth") or "01/01/2000",
|
||||
height = xPlayer.get("height") or 120,
|
||||
dead = false,
|
||||
metadata = xPlayer.getMeta()
|
||||
}, isNew,
|
||||
userData.skin)
|
||||
|
||||
if not Config.OxInventory then
|
||||
xPlayer.triggerEvent('esx:createMissingPickups', Core.Pickups)
|
||||
else
|
||||
exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory)
|
||||
|
||||
if isNew then
|
||||
for account, money in pairs(Config.StartingAccountMoney) do
|
||||
if account == 'money' or account == 'black_money' then
|
||||
exports.ox_inventory:AddItem(playerId, account, money)
|
||||
table.insert(userData.loadout,
|
||||
{
|
||||
name = name,
|
||||
ammo = weapon.ammo,
|
||||
label = label,
|
||||
components = weapon.components,
|
||||
tintIndex = weapon.tintIndex
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands)
|
||||
print(('[^2INFO^0] Player ^5"%s"^0 has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId))
|
||||
|
||||
-- Position
|
||||
userData.coords = json.decode(result.position) or Config.DefaultSpawns[math.random(#Config.DefaultSpawns)]
|
||||
|
||||
-- Skin
|
||||
if result.skin and result.skin ~= '' then
|
||||
userData.skin = json.decode(result.skin)
|
||||
else
|
||||
if userData.sex == 'f' then
|
||||
userData.skin = { sex = 1 }
|
||||
else
|
||||
userData.skin = { sex = 0 }
|
||||
end
|
||||
end
|
||||
|
||||
-- Identity
|
||||
if result.firstname and result.firstname ~= '' then
|
||||
userData.firstname = result.firstname
|
||||
userData.lastname = result.lastname
|
||||
userData.playerName = userData.firstname .. ' ' .. userData.lastname
|
||||
if result.dateofbirth then
|
||||
userData.dateofbirth = result.dateofbirth
|
||||
end
|
||||
if result.sex then
|
||||
userData.sex = result.sex
|
||||
end
|
||||
if result.height then
|
||||
userData.height = result.height
|
||||
end
|
||||
end
|
||||
|
||||
if result.metadata and result.metadata ~= '' then
|
||||
local metadata = json.decode(result.metadata)
|
||||
userData.metadata = metadata
|
||||
end
|
||||
|
||||
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory,
|
||||
userData.weight, userData.job,
|
||||
userData.loadout, userData.playerName, userData.coords, userData.metadata)
|
||||
ESX.Players[playerId] = xPlayer
|
||||
Core.playersByIdentifier[identifier] = xPlayer
|
||||
|
||||
if userData.firstname then
|
||||
xPlayer.set('firstName', userData.firstname)
|
||||
xPlayer.set('lastName', userData.lastname)
|
||||
if userData.dateofbirth then
|
||||
xPlayer.set('dateofbirth', userData.dateofbirth)
|
||||
end
|
||||
if userData.sex then
|
||||
xPlayer.set('sex', userData.sex)
|
||||
end
|
||||
if userData.height then
|
||||
xPlayer.set('height', userData.height)
|
||||
end
|
||||
end
|
||||
|
||||
TriggerEvent('esx:playerLoaded', playerId, xPlayer, isNew)
|
||||
|
||||
xPlayer.triggerEvent('esx:playerLoaded',
|
||||
{
|
||||
accounts = xPlayer.getAccounts(),
|
||||
coords = userData.coords,
|
||||
identifier = xPlayer.getIdentifier(),
|
||||
inventory = xPlayer.getInventory(),
|
||||
job = xPlayer.getJob(),
|
||||
loadout = xPlayer.getLoadout(),
|
||||
maxWeight = xPlayer.getMaxWeight(),
|
||||
money = xPlayer.getMoney(),
|
||||
sex = xPlayer.get("sex") or "m",
|
||||
firstName = xPlayer.get("firstName") or "John",
|
||||
lastName = xPlayer.get("lastName") or "Doe",
|
||||
dateofbirth = xPlayer.get("dateofbirth") or "01/01/2000",
|
||||
height = xPlayer.get("height") or 120,
|
||||
dead = false,
|
||||
metadata = xPlayer.getMeta()
|
||||
}, isNew,
|
||||
userData.skin)
|
||||
|
||||
if not Config.OxInventory then
|
||||
xPlayer.triggerEvent('esx:createMissingPickups', Core.Pickups)
|
||||
else
|
||||
exports.ox_inventory:setPlayerInventory(xPlayer, userData.inventory)
|
||||
|
||||
if isNew then
|
||||
for account, money in pairs(Config.StartingAccountMoney) do
|
||||
if account == 'money' or account == 'black_money' then
|
||||
exports.ox_inventory:AddItem(playerId, account, money)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
xPlayer.triggerEvent('esx:registerSuggestions', Core.RegisteredCommands)
|
||||
print(('[^2INFO^0] Player ^5"%s"^0 has connected to the server. ID: ^5%s^7'):format(xPlayer.getName(), playerId))
|
||||
end
|
||||
|
||||
|
||||
AddEventHandler('chatMessage', function(playerId, author, message)
|
||||
local xPlayer = ESX.GetPlayerFromId(playerId)
|
||||
if message:sub(1, 1) == '/' and playerId > 0 then
|
||||
|
||||
Reference in New Issue
Block a user