Skip to content

Commit

Permalink
convert: esx_addoninvetory > bpt_addoninvetory
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator authored Jun 1, 2024
1 parent 55f06ee commit 7a7cc09
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
function CreateAddonInventory(name, owner, items)
local self = {}

self.name = name
self.owner = owner
self.items = items

function self.addItem(name, count)
local item = self.getItem(name)
item.count = item.count + count

self.saveItem(name, item.count)
end

function self.removeItem(name, count)
if count > 0 then
local item = self.getItem(name)
item.count = item.count - count

self.saveItem(name, item.count)
end
end

function self.setItem(name, count)
local item = self.getItem(name)
item.count = count

self.saveItem(name, item.count)
end

function self.getItem(name)
for i = 1, #self.items, 1 do
if self.items[i].name == name then
return self.items[i]
end
end

item = {
name = name,
count = 0,
label = Items[name]
}

table.insert(self.items, item)

if self.owner == nil then
MySQL.update(
'INSERT INTO addon_inventory_items (inventory_name, name, count) VALUES (@inventory_name, @item_name, @count)',
{
['@inventory_name'] = self.name,
['@item_name'] = name,
['@count'] = 0
})
else
MySQL.update(
'INSERT INTO addon_inventory_items (inventory_name, name, count, owner) VALUES (@inventory_name, @item_name, @count, @owner)',
{
['@inventory_name'] = self.name,
['@item_name'] = name,
['@count'] = 0,
['@owner'] = self.owner
})
end

return item
end

function self.saveItem(name, count)
if self.owner == nil then
MySQL.update(
'UPDATE addon_inventory_items SET count = @count WHERE inventory_name = @inventory_name AND name = @item_name',
{
['@inventory_name'] = self.name,
['@item_name'] = name,
['@count'] = count
})
else
MySQL.update(
'UPDATE addon_inventory_items SET count = @count WHERE inventory_name = @inventory_name AND name = @item_name AND owner = @owner',
{
['@inventory_name'] = self.name,
['@item_name'] = name,
['@count'] = count,
['@owner'] = self.owner
})
end
end

return self
end
133 changes: 133 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_addoninventory/server/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
if ESX.GetConfig().OxInventory then
AddEventHandler('onServerResourceStart', function(resourceName)
if resourceName == 'ox_inventory' or resourceName == GetCurrentResourceName() then
local stashes = MySQL.query.await('SELECT * FROM addon_inventory')

for i = 1, #stashes do
local stash = stashes[i]
local jobStash = stash.name:find('society') and string.sub(stash.name, 9)
exports.ox_inventory:RegisterStash(stash.name, stash.label, 100, 200000,
stash.shared == 0 and true or false, jobStash)
end
end
end)

return
end

Items = {}
local InventoriesIndex, Inventories, SharedInventories = {}, {}, {}

MySQL.ready(function()
local items = MySQL.query.await('SELECT * FROM items')

for i = 1, #items, 1 do
Items[items[i].name] = items[i].label
end

local result = MySQL.query.await('SELECT * FROM addon_inventory')

for i = 1, #result, 1 do
local name = result[i].name
local label = result[i].label
local shared = result[i].shared

local result2 = MySQL.query.await('SELECT * FROM addon_inventory_items WHERE inventory_name = @inventory_name', {
['@inventory_name'] = name
})

if shared == 0 then
table.insert(InventoriesIndex, name)

Inventories[name] = {}
local items = {}

for j = 1, #result2, 1 do
local itemName = result2[j].name
local itemCount = result2[j].count
local itemOwner = result2[j].owner

if items[itemOwner] == nil then
items[itemOwner] = {}
end

table.insert(items[itemOwner], {
name = itemName,
count = itemCount,
label = Items[itemName]
})
end

for k, v in pairs(items) do
local addonInventory = CreateAddonInventory(name, k, v)
table.insert(Inventories[name], addonInventory)
end
else
local items = {}

for j = 1, #result2, 1 do
table.insert(items, {
name = result2[j].name,
count = result2[j].count,
label = Items[result2[j].name]
})
end

local addonInventory = CreateAddonInventory(name, nil, items)
SharedInventories[name] = addonInventory
GlobalState.SharedInventories = SharedInventories
end
end
end)

function GetInventory(name, owner)
for i = 1, #Inventories[name], 1 do
if Inventories[name][i].owner == owner then
return Inventories[name][i]
end
end
end

function GetSharedInventory(name)
return SharedInventories[name]
end

function AddSharedInventory(society)
if type(society) ~= 'table' or not society?.name or not society?.label then return end
-- society (array) containing name (string) and label (string)

-- addon inventory:
MySQL.Async.execute('INSERT INTO addon_inventory (name, label, shared) VALUES (@name, @label, @shared)', {
['name'] = society.name,
['label'] = society.label,
['shared'] = 1
})

SharedInventories[society.name] = CreateAddonInventory(society.name, nil, {})
end

AddEventHandler('bpt_addoninventory:getInventory', function(name, owner, cb)
cb(GetInventory(name, owner))
end)

AddEventHandler('bpt_addoninventory:getSharedInventory', function(name, cb)
cb(GetSharedInventory(name))
end)

AddEventHandler('esx:playerLoaded', function(playerId, xPlayer)
local addonInventories = {}

for i = 1, #InventoriesIndex, 1 do
local name = InventoriesIndex[i]
local inventory = GetInventory(name, xPlayer.identifier)

if inventory == nil then
inventory = CreateAddonInventory(name, xPlayer.identifier, {})
table.insert(Inventories[name], inventory)
end

table.insert(addonInventories, inventory)
end

xPlayer.set('addonInventories', addonInventories)
end)

0 comments on commit 7a7cc09

Please sign in to comment.