overextended / ox_inventory

Slot-based inventory with metadata.
https://overextended.dev/ox_inventory
GNU General Public License v3.0
442 stars 687 forks source link

Improvement of the `GetInventory` function for offline player management and new inventory search features #1787

Open Mesrine67 opened 2 weeks ago

Mesrine67 commented 2 weeks ago

I propose an enhancement to the ox_inventory:GetInventory function so that it works correctly when attempting to retrieve the inventory of an offline player using their citizenid. Currently, the function does not seem to return the inventory as expected in this scenario, limiting the ability to manage critical items (such as keys) for disconnected players. Additionally, I would like to suggest adding a new feature that allows searching for specific items across all existing inventory types.

Identified Issues:

  1. Retrieving inventory for offline players: Currently, the ox_inventory:GetInventory function does not allow retrieving the inventory of an offline player via their citizenid, which prevents access to specific items such as rental keys when the player is not connected. This limitation complicates resource and interaction management in the game, especially in contexts where a player’s offline status should not be a hindrance.

  2. Global item search: There is currently no simple way to search for a specific item across all inventories, whether they are player inventories (online or offline), vehicle trunks, job stashes, or even temporary containers. This makes it challenging to manage items globally on the server.

Problematic Example Code for Inventory Retrieval:

local function getInventorykeyRentOffline(citizenid, metadata)
    local inventory = ox_inventory:GetInventory({owner = citizenid})
    if inventory then
        local item = ox_inventory:GetItem({
            type = 'player',
            owner = inventory.owner},
            'key',
            metadata,
            false
        )
        if item then
            return true, {
                code = 'getInventoryRentMetadata',
                message = 'Item found successfully',
                item = item
            }
        end
        return true, {code = 'GetInventorykeyRent', message = 'Inventory retrieved:', inventory = inventory}
    else
        return false, {code = 'GetInventorykeyRent', message = 'Error: Inventory not found'}
    end
end

Proper Functioning of getInventorykeyRent:

Note that the getInventorykeyRent function works correctly for online players:

local function getInventorykeyRent(source, metadata)
    local inventory = ox_inventory:GetInventory(source)
    if inventory then
        local item = ox_inventory:GetItem(
             source
            'key',
            metadata,
            false
        )
        if item then
            return true, {
                code = 'getInventoryRentMetadata',
                message = 'Item found successfully',
                item = item
            }
        end
        return true, {code = 'GetInventorykeyRent', message = 'Inventory retrieved:', inventory = inventory}
    else
        return false, {code = 'GetInventorykeyRent', message = 'Error: Inventory not found'}
    end
end

Proposed Enhancements:

  1. Offline Inventory Retrieval: I suggest that the ox_inventory:GetInventory function be improved to support the retrieval of offline player inventories using their citizenid. This could include:

    • Automatic loading of offline inventory: If the player is not online, GetInventory should be able to automatically load the inventory from the database or another persistent source.
    • Return of the full inventory: If successful, the function should return the complete inventory, allowing scripts to continue functioning normally, whether the player is online or offline.
  2. Global Item Search: I also propose adding a new feature that allows searching for specific items across all existing inventory types (players, stashes, vehicles, etc.). Here’s an idea of the function to be added:

    local function getInventoryByItems(items, filter)
       if type(items) == 'table' then 
           -- Search across multiple items
       else 
           -- Search for a single item
       end
       -- Search through all inventories based on the specified filter
       -- If the filter is empty, search in all types of inventories: 'player' (all/offline/online), 'stash', 'container', 'drop', 'glovebox', 'trunk', 'dumpster'
    end

Expected Benefits:

  1. Simplified management of offline items: Allows for the retrieval, management, and manipulation of items for offline players in a transparent manner.

  2. Improved server functionality: Facilitates the management of critical resources (such as keys) that could otherwise remain inaccessible when players are offline.

  3. Easier global search: Allows administrators and scripts to perform quick and efficient searches across all available inventories on the server.

  4. Gameplay continuity: This functionality ensures continuity in gameplay and inventory systems, avoiding interruptions due to technical limitations.

I hope this suggestion will be considered to improve ox_inventory and offer greater flexibility and better inventory management, particularly in scenarios involving offline players or specific item searches.

thelindat commented 2 weeks ago

https://github.com/overextended/ox_inventory/issues/1699

Offline Inventory Retrieval

Requires a lot of changes to be made that would require many ugly workarounds to even maintain compatibility with existing code.

Global Item Search

With the current database design it's would also be horrendous to search for just the inventories with the specified item(s) and load those. Loading every inventory from the database into the resource would also have many issues.

Mesrine67 commented 2 weeks ago

ok thanks for your reply

Louisegiss commented 9 hours ago

please where put this