Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ox_inventory native compatibility #528

Merged
merged 28 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
f3813da
feat(server/player): disable weapons via statebag
solareon Aug 12, 2024
574202f
feat(server/player): add inventory functions to player object
solareon Aug 12, 2024
6fc7f62
refactor(server/player): use money vice cash
solareon Aug 12, 2024
6e0ecbc
feat(server/player): add groups field to playerdata
solareon Aug 12, 2024
ab375aa
refactor(server/player): error on unsupported functions
solareon Aug 12, 2024
36cf5c3
feat(functions): HasGroup export
solareon Aug 12, 2024
481d937
feat(server/functions): GetInventorySetup export
solareon Aug 12, 2024
be0e267
feat: GetGroups export
solareon Aug 12, 2024
50d0917
feat: setMoney event
solareon Aug 12, 2024
8415fc3
feat: IsGradeBoss export and export rename
solareon Aug 12, 2024
c81fac9
initial inv compat
solareon Aug 12, 2024
f277bbc
feat: generate shared weapons from ox_inventory
solareon Aug 12, 2024
ace4327
Revert "refactor(server/player): use money vice cash"
solareon Aug 12, 2024
029eefe
feat: convert items to ox format
solareon Aug 12, 2024
95994b9
fix: remove empty return
solareon Aug 12, 2024
d18a54a
refactor: move compat module
solareon Aug 12, 2024
2e190bb
feat: create Shared.Items from oxinv
solareon Aug 12, 2024
4a20053
Revert "feat: generate shared weapons from ox_inventory"
solareon Aug 12, 2024
6f11fbd
refactor: use qbCoreCompat for shared items
solareon Aug 12, 2024
e04daea
refactor: revert deprecation notice
solareon Aug 12, 2024
1d46a94
fix(bridge/qb): item name spacing
solareon Aug 12, 2024
5cea17d
fix(functions): rename GetGroups to GetPlayerGroups
solareon Aug 12, 2024
fd8ccee
feat: inventoryLoad statebag
solareon Aug 12, 2024
1147ff8
fix: use proper function name
solareon Aug 12, 2024
ecaa311
refactor: qbCoreCompat as global
solareon Aug 12, 2024
26b5dc1
chore: update deprecation notice for items.lua
solareon Aug 12, 2024
cb1a12f
refactor(server/player): extract money events to function
solareon Aug 13, 2024
5a6cffd
chore: merge type annotations
solareon Aug 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions bridge/qb/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ local function AddItem(itemName, item)
return false, 'invalid_item_name'
end

if QBX.Shared.Items[itemName] then
if qbCoreCompat.Shared.Items[itemName] then
return false, 'item_exists'
end

QBX.Shared.Items[itemName] = item
lib.print.warn(('New item %s added but not found in ox_inventory. Printing item data'):format(itemName))
lib.print.warn(item)
qbCoreCompat.Shared.Items[itemName] = item

TriggerClientEvent('QBCore:Client:OnSharedUpdate', -1, 'Items', itemName, item)
TriggerEvent('QBCore:Server:UpdateObject')
Expand All @@ -160,10 +162,10 @@ local function UpdateItem(itemName, item)
if type(itemName) ~= 'string' then
return false, 'invalid_item_name'
end
if not QBX.Shared.Items[itemName] then
if not qbCoreCompat.Shared.Items[itemName] then
return false, 'item_not_exists'
end
QBX.Shared.Items[itemName] = item
qbCoreCompat.Shared.Items[itemName] = item
TriggerClientEvent('QBCore:Client:OnSharedUpdate', -1, 'Items', itemName, item)
TriggerEvent('QBCore:Server:UpdateObject')
return true, 'success'
Expand All @@ -188,14 +190,16 @@ local function AddItems(items)
break
end

if QBX.Shared.Items[key] then
if qbCoreCompat.Shared.Items[key] then
message = 'item_exists'
shouldContinue = false
errorItem = items[key]
break
end
lib.print.warn(('New item %s added but not found in ox_inventory. Printing item data'):format(key))
lib.print.warn(value)

QBX.Shared.Items[key] = value
qbCoreCompat.Shared.Items[key] = value
end

if not shouldContinue then return false, message, errorItem end
Expand All @@ -215,11 +219,11 @@ local function RemoveItem(itemName)
return false, 'invalid_item_name'
end

if not QBX.Shared.Items[itemName] then
if not qbCoreCompat.Shared.Items[itemName] then
return false, 'item_not_exists'
end

QBX.Shared.Items[itemName] = nil
qbCoreCompat.Shared.Items[itemName] = nil

TriggerClientEvent('QBCore:Client:OnSharedUpdate', -1, 'Items', itemName, nil)
TriggerEvent('QBCore:Server:UpdateObject')
Expand Down
7 changes: 6 additions & 1 deletion bridge/qb/server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ if GetConvar('qbx:enablebridge', 'true') == 'false' then return end
require 'bridge.qb.server.debug'
require 'bridge.qb.server.events'

local qbCoreCompat = {}
CreateThread(function()
local convertItems = require 'bridge.qb.shared.compat'.convertItems
convertItems(require '@ox_inventory.data.items', require 'shared.items')
end)

qbCoreCompat = {}

qbCoreCompat.Config = lib.table.merge(require 'config.server', require 'config.shared')
qbCoreCompat.Shared = require 'bridge.qb.shared.main'
Expand Down
98 changes: 98 additions & 0 deletions bridge/qb/shared/compat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-- Item conversion borrowed from ox_inventory: https://github.com/overextended/ox_inventory/blob/52d99285eef1dc7df31c084679db3fdf6b1c0150/modules/items/server.lua#L115-L217
-- Copyright (c) 2023 Overextended

return {
convertItems = function(ItemList, items)
local dump = {}
local count = 0
local ignoreList = {
"weapon_",
"pistol_",
"pistol50_",
"revolver_",
"smg_",
"combatpdw_",
"shotgun_",
"rifle_",
"carbine_",
"gusenberg_",
"sniper_",
"snipermax_",
"tint_",
"_ammo"
}

local function checkIgnoredNames(name)
for i = 1, #ignoreList do
if string.find(name, ignoreList[i]) then
return true
end
end
return false
end

for k, item in pairs(items) do
if type(item) == 'table' then
if not item.name then item.name = k end

if not ItemList[item.name] and not checkIgnoredNames(item.name) then
item.close = item.shouldClose == nil and true or item.shouldClose
item.stack = not item.unique and true
item.description = item.description
item.weight = item.weight or 0
dump[k] = item
count += 1
end
end
end

if table.type(dump) ~= 'empty' then
local file = {string.strtrim(LoadResourceFile('ox_inventory', 'data/items.lua'))}
file[1] = file[1]:gsub('}$', '')

local itemFormat = [[

[%q] = {
label = %q,
weight = %s,
stack = %s,
close = %s,
description = %q,
client = {
status = {
hunger = %s,
thirst = %s,
stress = %s
},
image = %q,
}
},
]]

local fileSize = #file

for _, item in pairs(dump) do
if not ItemList[item.name] then
fileSize += 1

---@todo cry
local itemStr = itemFormat:format(item.name, item.label, item.weight, item.stack, item.close, item.description or 'nil', item.hunger or 'nil', item.thirst or 'nil', item.stress or 'nil', item.image or 'nil')
-- temporary solution for nil values
itemStr = itemStr:gsub('[%s]-[%w]+ = "?nil"?,?', '')
-- temporary solution for empty status table
itemStr = itemStr:gsub('[%s]-[%w]+ = %{[%s]+%},?', '')
-- temporary solution for empty client table
itemStr = itemStr:gsub('[%s]-[%w]+ = %{[%s]+%},?', '')
file[fileSize] = itemStr
ItemList[item.name] = item
end
end

file[fileSize+1] = '}'

SaveResourceFile('ox_inventory', 'data/items.lua', table.concat(file), -1)
print('^2[warning]^7 '..count..' items have been added to ox_inventory')
print('^2[warning]^7 You MUST restart the resource to load the new items.')
end
end
}
64 changes: 64 additions & 0 deletions bridge/qb/shared/main.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
local qbShared = require 'shared.main'

qbShared.Items = {}
local oxItems = require '@ox_inventory.data.items'
for item, data in pairs(oxItems) do
qbShared.Items[item] = {
name = item,
label = data.label,
weight = data.weight or 0,
type = 'item',
image = data.client?.image or string.strjoin(item,'.png'),
unique = false,
useable = true,
shouldClose = data.close or true,
combinable = nil,
description = data.description or nil
}
end
local oxWeapons = require '@ox_inventory.data.weapons'
for weapon, data in pairs(oxWeapons.Weapons) do
weapon = string.lower(weapon)
qbShared.Items[weapon] = {
name = weapon,
label = data.label,
weight = data.weight,
type = 'weapon',
ammotype = data.ammoname or nil,
image = data.client?.image or string.strjoin(weapon,'.png'),
unique = true,
useable = false,
shouldClose = true,
combinable = nil,
description = nil
}
end
for component, data in pairs(oxWeapons.Components) do
component = string.lower(component)
qbShared.Items[component] = {
name = component,
label = data.label,
weight = data.weight,
type = 'component',
image = data.client?.image or string.strjoin(component,'.png'),
unique = true,
useable = false,
shouldClose = true,
combinable = nil,
description = data.description
}
end
for ammo, data in pairs(oxWeapons.Ammo) do
ammo = string.lower(ammo)
qbShared.Items[ammo] = {
name = ammo,
label = data.label,
weight = data.weight,
type = 'ammo',
image = data.client?.image or string.strjoin(ammo,'.png'),
unique = true,
useable = false,
shouldClose = true,
combinable = nil,
description = data.description
}
end

local starterItems = require 'config.shared'.starterItems
---@deprecated use starterItems in config/shared.lua
qbShared.StarterItems = {}
Expand Down
18 changes: 17 additions & 1 deletion client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ exports('GetPlayerData', GetPlayerData)
---@param filter string | string[] | table<string, number>
---@return boolean
function HasPrimaryGroup(filter)
return HasPlayerGotGroup(filter, QBX.PlayerData, true)
end

exports('HasPrimaryGroup', HasPrimaryGroup)

---@param filter string | string[] | table<string, number>
---@return boolean
function HasGroup(filter)
return HasPlayerGotGroup(filter, QBX.PlayerData)
end

exports('HasPrimaryGroup', HasPrimaryGroup)
exports('HasGroup', HasGroup)

---@return table<string, integer>
function GetGroups()
local playerData = QBX.PlayerData
return GetPlayerGroups(playerData)
end

exports('GetGroups', GetGroups)
1 change: 1 addition & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ end

exports('GetWeapons', GetWeapons)

---@deprecated
---@return table<string, vector4>
function GetLocations()
return QBX.Shared.Locations
Expand Down
40 changes: 39 additions & 1 deletion server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,45 @@ exports('ExploitBan', ExploitBan)
---@return boolean
function HasPrimaryGroup(source, filter)
local playerData = QBX.Players[source].PlayerData
return HasPlayerGotGroup(filter, playerData)
return HasPlayerGotGroup(filter, playerData, true)
end

exports('HasPrimaryGroup', HasPrimaryGroup)

---@param source Source
---@param filter string | string[] | table<string, number>
---@return boolean
function HasGroup(source, filter)
local playerData = QBX.Players[source].PlayerData
return HasPlayerGotGroup(filter, playerData)
end

exports('HasGroup', HasGroup)

---@param source Source
---@return table<string, integer>
function GetGroups(source)
local playerData = QBX.Players[source].PlayerData
return GetPlayerGroups(playerData)
end

exports('GetGroups', GetGroups)

---@return PlayerData[]
local function getPlayersData()
local playersData = {}
for _, player in pairs(QBX.Players) do
playersData[#playersData + 1] = player.PlayerData
end
return playersData
end

exports('GetPlayersData', getPlayersData)
Manason marked this conversation as resolved.
Show resolved Hide resolved

local function isGradeBoss(group, grade)
local groupData = GetJob(group) or GetGang(group)
if not groupData then return end
return groupData[grade].IsBoss
end

exports('IsGradeBoss', isGradeBoss)
1 change: 1 addition & 0 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ end

exports('GetWeapons', GetWeapons)

---@deprecated
---@return table<string, vector4>
function GetLocations()
return QBX.Shared.Locations
Expand Down
Loading