mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 09:18:53 +00:00
Improved saving loadouts (see desc)
- Only save weapon components if needed, wont save default clip anymore, and if no other component is available then its not saved at all - Tint index not stored if not above 0 - Weapon label not saved in database since its useless - Validate weapon on player load
This commit is contained in:
@@ -96,15 +96,15 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
|
||||
|
||||
self.getInventory = function(minimal)
|
||||
if minimal then
|
||||
local items = {}
|
||||
local minimalInventory = {}
|
||||
|
||||
for k,v in ipairs(self.inventory) do
|
||||
if v.count > 0 then
|
||||
items[v.name] = v.count
|
||||
minimalInventory[v.name] = v.count
|
||||
end
|
||||
end
|
||||
|
||||
return items
|
||||
return minimalInventory
|
||||
else
|
||||
return self.inventory
|
||||
end
|
||||
@@ -114,8 +114,33 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
|
||||
return self.job
|
||||
end
|
||||
|
||||
self.getLoadout = function()
|
||||
return self.loadout
|
||||
self.getLoadout = function(minimal)
|
||||
if minimal then
|
||||
local minimalLoadout = {}
|
||||
|
||||
for k,v in ipairs(self.loadout) do
|
||||
minimalLoadout[v.name] = {ammo = v.ammo}
|
||||
if v.tintIndex > 0 then minimalLoadout[v.name].tintIndex = v.tintIndex end
|
||||
|
||||
if #v.components > 0 then
|
||||
local components = {}
|
||||
|
||||
for k2,component in ipairs(v.components) do
|
||||
if component ~= 'clip_default' then
|
||||
table.insert(components, component)
|
||||
end
|
||||
end
|
||||
|
||||
if #components > 0 then
|
||||
minimalLoadout[v.name].components = components
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return minimalLoadout
|
||||
else
|
||||
return self.loadout
|
||||
end
|
||||
end
|
||||
|
||||
self.getName = function()
|
||||
@@ -185,11 +210,11 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
|
||||
if account then
|
||||
local newMoney = account.money + ESX.Math.Round(money)
|
||||
account.money = newMoney
|
||||
|
||||
|
||||
if accountName == 'bank' then
|
||||
self.set('bank', newMoney)
|
||||
end
|
||||
|
||||
|
||||
self.triggerEvent('esx:setAccountMoney', account)
|
||||
end
|
||||
end
|
||||
@@ -202,11 +227,11 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
|
||||
if account then
|
||||
local newMoney = account.money - ESX.Math.Round(money)
|
||||
account.money = newMoney
|
||||
|
||||
|
||||
if accountName == 'bank' then
|
||||
self.set('bank', newMoney)
|
||||
end
|
||||
|
||||
|
||||
self.triggerEvent('esx:setAccountMoney', account)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -182,7 +182,7 @@ ESX.SavePlayer = function(xPlayer, cb)
|
||||
MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade, loadout = @loadout, position = @position, inventory = @inventory WHERE identifier = @identifier', {
|
||||
['@job'] = xPlayer.job.name,
|
||||
['@job_grade'] = xPlayer.job.grade,
|
||||
['@loadout'] = json.encode(xPlayer.getLoadout()),
|
||||
['@loadout'] = json.encode(xPlayer.getLoadout(true)),
|
||||
['@position'] = json.encode(xPlayer.getCoords()),
|
||||
['@identifier'] = xPlayer.identifier,
|
||||
['@inventory'] = json.encode(xPlayer.getInventory(true))
|
||||
|
||||
+17
-6
@@ -133,13 +133,24 @@ function loadESXPlayer(identifier, playerId)
|
||||
return a.label < b.label
|
||||
end)
|
||||
|
||||
if result[1].loadout then
|
||||
userData.loadout = json.decode(result[1].loadout)
|
||||
if result[1].loadout and result[1].loadout ~= '' then
|
||||
local loadout = json.decode(result[1].loadout)
|
||||
|
||||
-- compatibility with old loadouts
|
||||
for k,v in ipairs(userData.loadout) do
|
||||
if not v.components then v.components = {} end
|
||||
if not v.tintIndex then v.tintIndex = 0 end
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user