Skip to content

Commit

Permalink
Merge pull request #711 from bitpredator/dev
Browse files Browse the repository at this point in the history
convert: esx_addoninventory > bpt_addoninventory
  • Loading branch information
bitpredator authored Jun 3, 2024
2 parents 1d0736d + 0b6cc1d commit e7918b1
Show file tree
Hide file tree
Showing 22 changed files with 299 additions and 339 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down Expand Up @@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

esx_addoninventory
Copyright (C) 2015-2022 Jérémie N'gadi
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 2024 bitpredator

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -645,14 +645,14 @@ the "copyright" line and a pointer to where the full notice is found.
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
along with this program. If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

esx_addoninventory Copyright (C) 2015-2022 Jérémie N'gadi
<program> Copyright (C) 2024 bitpredator
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
Expand All @@ -664,11 +664,11 @@ might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
<https://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
<https://www.gnu.org/licenses/why-not-lgpl.html>.
19 changes: 19 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_addoninventory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1 align='center'>bpt_addoninventory</a></h1>
<p align='center'><a href='https://discord.gg/ksGfNvDEfq'>Discord</a>

Copyright (C) 2024 bitpredator

This program Is free software: you can redistribute it And/Or modify it under the terms Of the GNU General Public License As published by the Free Software Foundation, either version 3 Of the License, Or (at your option) any later version.

This program Is distributed In the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty Of MERCHANTABILITY Or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License For more details.

ATTENTION:
You are not authorized to change the name of the resource and the resources within it.

If you want to contribute you can open a pull request.

You are not authorized to sell this software (this is free project).

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ CREATE TABLE `addon_inventory_items` (
INDEX `index_addon_inventory_items_inventory_name_name` (`inventory_name`, `name`),
INDEX `index_addon_inventory_items_inventory_name_name_owner` (`inventory_name`, `name`, `owner`),
INDEX `index_addon_inventory_inventory_name` (`inventory_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
fx_version 'adamant'

game 'gta5'

description 'Adds a way for resources to store items for players'
lua54 'yes'

version '1.0.1'

server_scripts {
'@es_extended/imports.lua',
'@oxmysql/lib/MySQL.lua',
'server/classes/addoninventory.lua',
'server/main.lua'
}

server_exports {
'GetSharedInventory',
'AddSharedInventory'
}

dependency 'es_extended'
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
local self, name, owner, items = {}, {}, {}, {}
self.name = name
self.owner = owner
self.items = items

function CreateAddonInventory()
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
131 changes: 131 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,131 @@
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 _ = 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] = {}

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

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)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AddEventHandler("bpt_ammujob:getStockItem", function(itemName, count)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == "ammu" then
TriggerEvent("esx_addoninventory:getSharedInventory", "society_ammu", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_ammu", function(inventory)
local item = inventory.getItem(itemName)

-- is there enough in the society?
Expand All @@ -56,7 +56,7 @@ AddEventHandler("bpt_ammujob:getStockItem", function(itemName, count)
end)

ESX.RegisterServerCallback("bpt_ammujob:getStockItems", function(_, cb)
TriggerEvent("esx_addoninventory:getSharedInventory", "society_ammu", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_ammu", function(inventory)
cb(inventory.items)
end)
end)
Expand All @@ -67,7 +67,7 @@ AddEventHandler("bpt_ammujob:putStockItems", function(itemName, count)
local sourceItem = xPlayer.getInventoryItem(itemName)

if xPlayer.job.name == "ammu" then
TriggerEvent("esx_addoninventory:getSharedInventory", "society_ammu", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_ammu", function(inventory)
local item = inventory.getItem(itemName)

if sourceItem.count >= count and count > 0 then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ AddEventHandler("bpt_bakerjob:getStockItem", function(itemName, count)
local xPlayer = ESX.GetPlayerFromId(source)

if xPlayer.job.name == "baker" then
TriggerEvent("esx_addoninventory:getSharedInventory", "society_baker", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_baker", function(inventory)
local item = inventory.getItem(itemName)

-- is there enough in the society?
Expand All @@ -56,7 +56,7 @@ AddEventHandler("bpt_bakerjob:getStockItem", function(itemName, count)
end)

ESX.RegisterServerCallback("bpt_bakerjob:getStockItems", function(_, cb)
TriggerEvent("esx_addoninventory:getSharedInventory", "society_baker", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_baker", function(inventory)
cb(inventory.items)
end)
end)
Expand All @@ -67,7 +67,7 @@ AddEventHandler("bpt_bakerjob:putStockItems", function(itemName, count)
local sourceItem = xPlayer.getInventoryItem(itemName)

if xPlayer.job.name == "baker" then
TriggerEvent("esx_addoninventory:getSharedInventory", "society_baker", function(inventory)
TriggerEvent("bpt_addoninventory:getSharedInventory", "society_baker", function(inventory)
local item = inventory.getItem(itemName)

if sourceItem.count >= count and count > 0 then
Expand Down
Loading

0 comments on commit e7918b1

Please sign in to comment.