Now we can extend xPlayer without touching core files

This commit is contained in:
Jérémie N'gadi
2020-05-13 21:04:06 +02:00
parent c6ba934d06
commit ff8625cb32
5 changed files with 373 additions and 224 deletions
+10 -162
View File
@@ -50,9 +50,13 @@ function onPlayerJoined(playerId)
MySQL.Async.fetchScalar('SELECT 1 FROM users WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(result)
if result then
loadESXPlayer(identifier, playerId)
else
if result then
LoadExtendedPlayer(identifier, playerId)
else
local accounts = {}
for account,money in pairs(Config.StartingAccountMoney) do
@@ -63,8 +67,9 @@ function onPlayerJoined(playerId)
['@accounts'] = json.encode(accounts),
['@identifier'] = identifier
}, function(rowsChanged)
loadESXPlayer(identifier, playerId)
end)
LoadExtendedPlayer(identifier, playerId)
end)
end
end)
end
@@ -96,163 +101,6 @@ AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
end
end)
function loadESXPlayer(identifier, playerId)
local tasks = {}
local userData = {
accounts = {},
inventory = {},
job = {},
loadout = {},
playerName = GetPlayerName(playerId),
weight = 0
}
table.insert(tasks, function(cb)
MySQL.Async.fetchAll('SELECT accounts, job, job_grade, `group`, loadout, position, inventory FROM users WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(result)
local job, grade, jobObject, gradeObject = result[1].job, tostring(result[1].job_grade)
local foundAccounts, foundItems = {}, {}
-- Accounts
if result[1].accounts and result[1].accounts ~= '' then
local accounts = json.decode(result[1].accounts)
for account,money in pairs(accounts) do
foundAccounts[account] = money
end
end
for account,label in pairs(Config.Accounts) do
table.insert(userData.accounts, {
name = account,
money = foundAccounts[account] or Config.StartingAccountMoney[account] or 0,
label = label
})
end
-- Job
if ESX.DoesJobExist(job, grade) then
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
else
print(('[es_extended] [^3WARNING^7] Ignoring invalid job for %s [job: %s, grade: %s]'):format(identifier, job, grade))
job, grade = 'unemployed', '0'
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
end
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 result[1].inventory and result[1].inventory ~= '' then
local inventory = json.decode(result[1].inventory)
for name,count in pairs(inventory) do
local item = ESX.Items[name]
if item then
foundItems[name] = count
else
print(('[es_extended] [^3WARNING^7] Ignoring invalid item "%s" for "%s"'):format(name, identifier))
end
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) end
table.insert(userData.inventory, {
name = name,
count = count,
label = item.label,
weight = item.weight,
usable = ESX.UsableItemsCallbacks[name] ~= nil,
rare = item.rare,
canRemove = item.canRemove
})
end
table.sort(userData.inventory, function(a, b)
return a.label < b.label
end)
-- Group
if result[1].group then
userData.group = result[1].group
else
userData.group = 'user'
end
-- Loadout
if result[1].loadout and result[1].loadout ~= '' then
local loadout = json.decode(result[1].loadout)
for name,weapon in pairs(loadout) do
local label = ESX.GetWeaponLabel(name)
if label then
if not weapon.components then weapon.components = {} 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
-- Position
if result[1].position and result[1].position ~= '' then
userData.coords = json.decode(result[1].position)
else
print('[es_extended] [^3WARNING^7] Column "position" in "users" table is missing required default value. Using backup coords, fix your database.')
userData.coords = {x = -269.4, y = -955.3, z = 31.2, heading = 205.8}
end
cb()
end)
end)
Async.parallel(tasks, function(results)
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.weight, userData.job, userData.loadout, userData.playerName, userData.coords)
ESX.Players[playerId] = xPlayer
TriggerEvent('esx:playerLoaded', playerId, xPlayer)
xPlayer.triggerEvent('esx:playerLoaded', {
accounts = xPlayer.getAccounts(),
coords = xPlayer.getCoords(),
identifier = xPlayer.getIdentifier(),
inventory = xPlayer.getInventory(),
job = xPlayer.getJob(),
loadout = xPlayer.getLoadout(),
maxWeight = xPlayer.getMaxWeight(),
money = xPlayer.getMoney()
})
xPlayer.triggerEvent('esx:createMissingPickups', ESX.Pickups)
xPlayer.triggerEvent('esx:registerSuggestions', ESX.RegisteredCommands)
end)
end
AddEventHandler('chatMessage', function(playerId, author, message)
if message:sub(1, 1) == '/' and playerId > 0 then
CancelEvent()