Qbox-project/qbx_core

Optimization of Item Management: Eliminating `items.lua` in Favor of `item.lua` from `ox_inventory`

Mesrine67 opened this issue · 10 comments

The problem

  • I find it frustrating to manage similar item information in two separate files (items.lua from qbx_core and item.lua from ox_inventory).
  • It would be beneficial to have a single reference source for items, thus reducing complexity and improving consistency.

Ideal solution

The ideal solution would be to remove the items.lua file in qbx_core and redirect all references to this file to item.lua in ox_inventory. This would allow for:

  • Simpler and centralized maintenance of item information.
  • Increased consistency and reduced risks of conflicts or data inconsistencies.
  • Potential performance improvements by eliminating duplicate item management systems.

Alternative solutions

An alternative might be to closely synchronize both files to ensure they are always aligned, although this could result in higher maintenance workload.

Additional context

This suggestion aims to simplify the project structure and enhance the overall efficiency of item management in qbx_core. I believe that this optimization would be beneficial for both the community and developers working on the project.

This exists for compatibility reasons and config devs. You don't have to maintain this after the initial setup and can basically ignore it but there is some underlying qbcore logic that depends on stuff being in here.

As solareon said, items.lua is purely for backwards compatibility thus it can't be removed without causing issues. Once the recipe is done and the server is set up, your main items table is going to be ox_inventory/data/items.lua. That is where you should be inserting all of the items needed for your server. While we understand having both can be annoying, it is something that can't be changed.

create a code that returns the items.lua and the weapons.lua of ox_inventory, this would avoid writing the items twice, because the options of ox_inventory are much more complete

why not make a code if ox_inventory and present add the ox_inventory files to not use the items.lua of qbx

create a code that returns the items.lua and the weapons.lua of ox_inventory, this would avoid writing the items twice, because the options of ox_inventory are much more complete

qbx_items are returned only through the bridge layer and have a different format with different info than ox_inventory. If ox_inventory data is a superset of the qbx_items data then we could write a conversion function to convert the ox_inventory to the qbx format.

why not make a code if ox_inventory and present add the ox_inventory files to not use the items.lua of qbx

ox_inventory is the required inventory system of Qbox. The core issue here is that certain QB scripts rely on the qb items.lua format. We need to provide that format to maintain compatibility through the bridge layer. I'll leave this open for someone to explore the possibility of converting the ox_inventory format to qbx items.lua format at runtime.

create a code that returns the items.lua and the weapons.lua of ox_inventory, this would avoid writing the items twice, because the options of ox_inventory are much more complete

qbx_items are returned only through the bridge layer and have a different format with different info than ox_inventory. If ox_inventory data is a superset of the qbx_items data then we could write a conversion function to convert the ox_inventory to the qbx format.

why not make a code if ox_inventory and present add the ox_inventory files to not use the items.lua of qbx

ox_inventory is the required inventory system of Qbox. The core issue here is that certain QB scripts rely on the qb items.lua format. We need to provide that format to maintain compatibility through the bridge layer. I'll leave this open for someone to explore the possibility of converting the ox_inventory format to qbx items.lua format at runtime.

Could we create the code that returns the items to qbx items in the correct format, only if ox_inventory is detected? Whether it's weapons, ammunition, accessories, or items, for images, we can add the default item name, etc., and other details.

I don't think anything really needs to be done at all as the items.lua in qbx_core is effectively unused and useless except for backwards compat but if someone wants to frankenstein some code for this, by all means.

I will find the time to try to implement the code for redirecting ox_inventory.

I tried to create this, could this code work? it must be completed further so that it is 100% functional.

qbx_core\shared\main.lua

local qbShared = {}
local OxInventory = GetResourceState("ox_inventory") ~= 'missing'
local itemsOxInventory = require '@ox_inventory.data.items' -- or lib.table.merge(require '@ox_inventory.data.items', require '@ox_inventory.data.weapons')
local weaponsOxInventory = require '@ox_inventory.data.weapons' -- or lib.table.merge(require '@ox_inventory.data.items', require '@ox_inventory.data.weapons')
local OxInventoryLoaded = false
qbShared.Gangs = require 'shared.gangs'
qbShared.Items = nil
if OxInventory then
    qbShared.Items = {}
	local itemCounter = 0
	local weaponCounter = 0
	local componentsCounter = 0
	local ammoCounter = 0
    for itemOx, dataOx in pairs(itemsOxInventory) do
		itemCounter = itemCounter + 1 
        print(("[ox_inventory -> items.lua] Item N°%d name: %s, label: %s"):format(itemCounter, itemOx, dataOx.label))
		qbShared.Items[itemOx] = {
			['name'] = itemOx, 			  	  	 
			['label'] = dataOx.label, 				
			['weight'] = dataOx.weight or 0, 		
			['type'] = 'item',
			['image'] = 'https://cfx-nui-ox_inventory/web/images/'..itemOx..'.png',
			['unique'] = false, 	
			['useable'] = true, 	
			['shouldClose'] = dataOx.close or true,   
			['combinable'] = nil,   
			['description'] = dataOx.description or nil
		}
    end
    for Weapons, dataWeapons in pairs(weaponsOxInventory.Weapons) do -- string.lower(Weapons)
		weaponCounter = weaponCounter + 1 
		local Weaponss = string.lower(Weapons)
        print(("[ox_inventory -> weapons.lua] Weapon N°%d -> name: %s info -> %s"):format(weaponCounter, Weapons, json.encode(dataWeapons)))
		qbShared.Items[Weaponss] = {
			['name'] = Weaponss, 			  	  	 
			['label'] = dataWeapons.label, 				
			['weight'] = dataWeapons.weight, 		
			['type'] = 'weapon',
			['ammotype'] = dataWeapons.ammoname or nil,
			['image'] = 'https://cfx-nui-ox_inventory/web/images/'..Weapons..'.png',
			['unique'] = true, 	
			['useable'] = false, 	
			['shouldClose'] = true,   
			['combinable'] = nil,   
			['description'] = nil
		}
    end
	for Components, dataComponents in pairs(weaponsOxInventory.Components) do
		componentsCounter = componentsCounter + 1 
        print(("[ox_inventory -> weapons.lua] Components -> N°%d, name: %s info -> %s"):format(ammoCounter, Components, json.encode(dataComponents))) -- Ajustez le log
    end
	for Ammo, dataAmmo in pairs(weaponsOxInventory.Ammo) do
		ammoCounter = ammoCounter + 1
		print(("[ox_inventory -> weapons.lua] Ammo -> N°%d, name: %s, label: %s, weight: %s"):format(ammoCounter, Ammo, dataAmmo.label, dataAmmo.weight))
		qbShared.Items[Ammo] = {
			['name'] = Ammo, 			  	  	 
			['label'] = 'Munitions '..dataAmmo.label, 				
			['weight'] = dataAmmo.weight, 		
			['type'] = 'item', 		
			['image'] = 'https://cfx-nui-ox_inventory/web/images/'..Ammo..'.png',
			['unique'] = false, 	
			['useable'] = true, 	
			['shouldClose'] = true,   
			['combinable'] = nil,   
			['description'] = 'Munitions '..dataAmmo.label
		}
	end
    OxInventoryLoaded = true
else
	qbShared.Items = require 'shared.items'
end
-- qbShared.Items = require 'shared.items'
qbShared.ForceJobDefaultDutyAtLogin = true -- true: Force duty state to jobdefaultDuty | false: set duty state from database last saved
qbShared.Jobs = require 'shared.jobs'
qbShared.Locations = require 'shared.locations'
qbShared.Vehicles = require 'shared.vehicles'
qbShared.Weapons = require 'shared.weapons'

---@type table<number, Vehicle>
qbShared.VehicleHashes = {}

for _, v in pairs(qbShared.Vehicles) do
	qbShared.VehicleHashes[v.hash] = v
end

return qbShared