mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-29 09:18:53 +00:00
Implemented json encoded inventory, closes #177
This commit is contained in:
@@ -129,8 +129,20 @@ function CreateExtendedPlayer(player, accounts, inventory, job, loadout, name, c
|
||||
end
|
||||
end
|
||||
|
||||
self.getInventory = function()
|
||||
return self.inventory
|
||||
self.getInventory = function(minimal)
|
||||
if minimal then
|
||||
local items = {}
|
||||
|
||||
for k,v in ipairs(self.inventory) do
|
||||
if v.count > 0 then
|
||||
items[v.name] = v.count
|
||||
end
|
||||
end
|
||||
|
||||
return items
|
||||
else
|
||||
return self.inventory
|
||||
end
|
||||
end
|
||||
|
||||
self.getJob = function()
|
||||
|
||||
+8
-24
@@ -56,31 +56,15 @@ ESX.SavePlayer = function(xPlayer, cb)
|
||||
end
|
||||
end
|
||||
|
||||
-- Inventory items
|
||||
for k,v in ipairs(xPlayer.inventory) do
|
||||
if ESX.LastPlayerData[xPlayer.source].items[v.name] ~= v.count then
|
||||
table.insert(asyncTasks, function(cb)
|
||||
MySQL.Async.execute('UPDATE user_inventory SET count = @count WHERE identifier = @identifier AND item = @item', {
|
||||
['@count'] = v.count,
|
||||
['@identifier'] = xPlayer.identifier,
|
||||
['@item'] = v.name
|
||||
}, function(rowsChanged)
|
||||
cb()
|
||||
end)
|
||||
end)
|
||||
|
||||
ESX.LastPlayerData[xPlayer.source].items[v.name] = v.count
|
||||
end
|
||||
end
|
||||
|
||||
-- Job, loadout and position
|
||||
-- Job, loadout, inventory and position
|
||||
table.insert(asyncTasks, function(cb)
|
||||
MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade, loadout = @loadout, position = @position WHERE identifier = @identifier', {
|
||||
['@job'] = xPlayer.job.name,
|
||||
['@job_grade'] = xPlayer.job.grade,
|
||||
['@loadout'] = json.encode(xPlayer.getLoadout()),
|
||||
['@position'] = json.encode(xPlayer.getCoords()),
|
||||
['@identifier'] = xPlayer.identifier
|
||||
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()),
|
||||
['@position'] = json.encode(xPlayer.getCoords()),
|
||||
['@identifier'] = xPlayer.identifier,
|
||||
['@inventory'] = json.encode(xPlayer.getInventory(true))
|
||||
}, function(rowsChanged)
|
||||
cb()
|
||||
end)
|
||||
|
||||
+36
-68
@@ -30,72 +30,6 @@ AddEventHandler('es:playerLoaded', function(playerId, player)
|
||||
end)
|
||||
end)
|
||||
|
||||
-- Get inventory
|
||||
table.insert(tasks, function(cb)
|
||||
MySQL.Async.fetchAll('SELECT item, count FROM user_inventory WHERE identifier = @identifier', {
|
||||
['@identifier'] = player.getIdentifier()
|
||||
}, function(inventory)
|
||||
local tasks2, foundItems = {}, {}
|
||||
|
||||
for k,v in ipairs(inventory) do
|
||||
local item = ESX.Items[v.item]
|
||||
|
||||
if item then
|
||||
foundItems[v.item] = true
|
||||
|
||||
table.insert(userData.inventory, {
|
||||
name = v.item,
|
||||
count = v.count,
|
||||
label = item.label,
|
||||
weight = item.weight,
|
||||
usable = ESX.UsableItemsCallbacks[v.item] ~= nil,
|
||||
rare = item.rare,
|
||||
canRemove = item.canRemove
|
||||
})
|
||||
else
|
||||
print(('[es_extended] [^3WARNING^7] Ignoring invalid item "%s" for "%s"'):format(v.item, player.getIdentifier()))
|
||||
end
|
||||
end
|
||||
|
||||
for name,item in pairs(ESX.Items) do
|
||||
if not foundItems[name] then
|
||||
table.insert(userData.inventory, {
|
||||
name = name,
|
||||
count = 0,
|
||||
label = item.label,
|
||||
weight = item.weight,
|
||||
usable = ESX.UsableItemsCallbacks[name] ~= nil,
|
||||
rare = item.rare,
|
||||
canRemove = item.canRemove
|
||||
})
|
||||
|
||||
local scope = function(item, identifier)
|
||||
table.insert(tasks2, function(cb2)
|
||||
MySQL.Async.execute('INSERT INTO user_inventory (identifier, item, count) VALUES (@identifier, @item, @count)', {
|
||||
['@identifier'] = identifier,
|
||||
['@item'] = item,
|
||||
['@count'] = 0
|
||||
}, function(rowsChanged)
|
||||
cb2()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
scope(name, player.getIdentifier())
|
||||
end
|
||||
end
|
||||
|
||||
Async.parallelLimit(tasks2, 5, function(results) end)
|
||||
|
||||
table.sort(userData.inventory, function(a,b)
|
||||
return a.label < b.label
|
||||
end)
|
||||
|
||||
cb()
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
-- Get job and loadout
|
||||
table.insert(tasks, function(cb)
|
||||
|
||||
@@ -104,7 +38,7 @@ AddEventHandler('es:playerLoaded', function(playerId, player)
|
||||
-- Get job name, grade and coords
|
||||
table.insert(tasks2, function(cb2)
|
||||
|
||||
MySQL.Async.fetchAll('SELECT job, job_grade, loadout, position FROM users WHERE identifier = @identifier', {
|
||||
MySQL.Async.fetchAll('SELECT job, job_grade, loadout, position, inventory FROM users WHERE identifier = @identifier', {
|
||||
['@identifier'] = player.getIdentifier()
|
||||
}, function(result)
|
||||
local job, grade = result[1].job, tostring(result[1].job_grade)
|
||||
@@ -154,10 +88,44 @@ AddEventHandler('es:playerLoaded', function(playerId, player)
|
||||
userData.job.skin_female = {}
|
||||
end
|
||||
|
||||
local foundItems = {}
|
||||
|
||||
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, player.getIdentifier()))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for name,item in pairs(ESX.Items) do
|
||||
local count = foundItems[name] or 0
|
||||
|
||||
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)
|
||||
|
||||
if result[1].loadout then
|
||||
userData.loadout = json.decode(result[1].loadout)
|
||||
|
||||
-- Compatibility with old loadouts prior to components update
|
||||
-- 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
|
||||
|
||||
Reference in New Issue
Block a user