From f84f13430008d94689e429ab4e4d5672caf3f1fc Mon Sep 17 00:00:00 2001 From: Your Name <50252724+DrMeepso@users.noreply.github.com> Date: Sun, 23 Jun 2024 12:48:50 +1200 Subject: [PATCH 1/6] add Create Inventory --- server/functions.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/server/functions.lua b/server/functions.lua index 9cea1e8f1..6a3e2fede 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -549,6 +549,19 @@ end exports('OpenInventory', OpenInventory) +--- Creates a new inventory and returns the inventory object. +--- @param identifier string The identifier of the inventory to create. +--- @param data table Additional data for initializing the inventory. +--- @return table|nil - The inventory object if created successfully, nil otherwise. +function CreateInventory(identifier, data) + if Inventories[identifier] then return end + if not identifier then return end + Inventories[identifier] = InitializeInventory(identifier, data) + return Inventories[identifier] +end + +exports('CreateInventory', CreateInventory) + --- Adds an item to the player's inventory or a specific inventory. --- @param identifier string The identifier of the player or inventory. --- @param item string The name of the item to add. From 049468a3fd6bce728c98ac71f880a0e130279d47 Mon Sep 17 00:00:00 2001 From: Your Name <50252724+DrMeepso@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:10:39 +1200 Subject: [PATCH 2/6] Update "SetInventory" export --- server/functions.lua | 64 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 6a3e2fede..2e45ec685 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -125,17 +125,40 @@ end exports('SaveInventory', SaveInventory) ---- Sets the inventory of a player. ---- @param source number The player's server ID. ---- @param items table The items to set in the player's inventory. -function SetInventory(source, items) - local Player = QBCore.Functions.GetPlayer(source) - if not Player then return end - Player.Functions.SetPlayerData('items', items) - if not Player.Offline then - local logMessage = string.format('**%s (citizenid: %s | id: %s)** items set: %s', GetPlayerName(source), Player.PlayerData.citizenid, source, json.encode(items)) - TriggerEvent('qb-log:server:CreateLog', 'playerinventory', 'SetInventory', 'blue', logMessage) +-- Sets the items in a inventory. +--- @param identifier string The identifier of the player or inventory. +--- @param items table The items to set in the inventory. +--- @param reason string The reason for setting the items. +function SetInventory(identifier, items, reason) + if not Inventories[identifier] then return end + local player = QBCore.Functions.GetPlayer(identifier) + + if player then + player.Functions.SetPlayerData('items', items) + if not player.Offline then + local logMessage = string.format('**%s (citizenid: %s | id: %s)** items set: %s', GetPlayerName(source), player.PlayerData.citizenid, source, json.encode(items)) + TriggerEvent('qb-log:server:CreateLog', 'playerinventory', 'SetInventory', 'blue', logMessage) + end + elseif Drops[identifier] then + Drops[identifier].items = items + elseif Inventories[identifier] then + Inventories[identifier].items = items end + + local invName = player and GetPlayerName(identifier) .. ' (' .. identifier .. ')' or identifier + local setReason = reason or 'No reason specified' + local resourceName = GetInvokingResource() or 'qb-inventory' + TriggerEvent( + 'qb-log:server:CreateLog', + 'playerinventory', + 'Inventory Set', + 'blue', + '**Inventory:** ' .. invName .. '\n' .. + '**Items:** ' .. json.encode(items) .. '\n' .. + '**Reason:** ' .. setReason .. '\n' .. + '**Resource:** ' .. resourceName + ) + end exports('SetInventory', SetInventory) @@ -552,16 +575,33 @@ exports('OpenInventory', OpenInventory) --- Creates a new inventory and returns the inventory object. --- @param identifier string The identifier of the inventory to create. --- @param data table Additional data for initializing the inventory. ---- @return table|nil - The inventory object if created successfully, nil otherwise. function CreateInventory(identifier, data) if Inventories[identifier] then return end if not identifier then return end Inventories[identifier] = InitializeInventory(identifier, data) - return Inventories[identifier] end exports('CreateInventory', CreateInventory) +--- Retrieves an inventory by its identifier. +--- @param identifier string The identifier of the inventory to retrieve. +--- @return table|nil - The inventory object if found, nil otherwise. +function GetInventory(identifier) + return Inventories[identifier] +end + +exports('GetInventory', GetInventory) + +--- Removes an inventory by its identifier. +--- @param identifier string The identifier of the inventory to remove. +function RemoveInventory(identifier) + if Inventories[identifier] then + Inventories[identifier] = nil + end +end + +exports('RemoveInventory', RemoveInventory) + --- Adds an item to the player's inventory or a specific inventory. --- @param identifier string The identifier of the player or inventory. --- @param item string The name of the item to add. From c570eebf22713606ca3a177b72bbd7a3fcca000a Mon Sep 17 00:00:00 2001 From: Your Name <50252724+DrMeepso@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:20:29 +1200 Subject: [PATCH 3/6] Update "CanAddItem" for other inventories --- server/functions.lua | 45 +++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 2e45ec685..dc7d1e18f 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -337,29 +337,48 @@ end exports('GetItemCount', GetItemCount) ---- Checks if an item can be added to a player's inventory. ---- @param source number The player's server ID. +--- Checks if an item can be added to a inventory based on the weight and slots available. +--- @param identifier string The identifier of the player or inventory. --- @param item string The item name. --- @param amount number The amount of the item. --- @return boolean - Returns true if the item can be added, false otherwise. --- @return string|nil - Returns a string indicating the reason why the item cannot be added (e.g., 'weight' or 'slots'), or nil if it can be added. -function CanAddItem(source, item, amount) - local Player = QBCore.Functions.GetPlayer(source) +function CanAddItem(identifier, item, amount) + + if not Inventories[identifier] then return false end + + local Player = QBCore.Functions.GetPlayer(identifier) if not Player then return false end + local itemData = QBCore.Shared.Items[item:lower()] if not itemData then return false end + + local inventory, items + if Player then + inventory = { + maxweight = Config.MaxWeight, + slots = Config.MaxSlots + } + items = Player.PlayerData.items + elseif Inventories[identifier] then + inventory = Inventories[identifier] + items = Inventories[identifier].items + end + + if not inventory or not items then + print("CanAddItem: Inventory not found") + return false + end + local weight = itemData.weight * amount - local totalWeight = GetTotalWeight(Player.PlayerData.items) + weight - if totalWeight > Config.MaxWeight then + local totalWeight = GetTotalWeight(items) + weight + if totalWeight > inventory.maxweight then return false, 'weight' end - local slotsUsed = 0 - for _, v in pairs(Player.PlayerData.items) do - if v then - slotsUsed = slotsUsed + 1 - end - end - if slotsUsed >= Config.MaxSlots then + + local slotsUsed, slotsFree = GetSlots(identifier) + + if slotsUsed >= inventory.slots then return false, 'slots' end return true From c8e1f5cef806c44fa5d426d2f9db680d71cf17fa Mon Sep 17 00:00:00 2001 From: Your Name <50252724+DrMeepso@users.noreply.github.com> Date: Sun, 23 Jun 2024 13:23:31 +1200 Subject: [PATCH 4/6] Removed unreachable code --- server/functions.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index dc7d1e18f..04258b45d 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -365,11 +365,6 @@ function CanAddItem(identifier, item, amount) items = Inventories[identifier].items end - if not inventory or not items then - print("CanAddItem: Inventory not found") - return false - end - local weight = itemData.weight * amount local totalWeight = GetTotalWeight(items) + weight if totalWeight > inventory.maxweight then From 556c52d357cd0bbfb40cea9106f63eae71319d9a Mon Sep 17 00:00:00 2001 From: Your Name <50252724+DrMeepso@users.noreply.github.com> Date: Sun, 23 Jun 2024 14:05:40 +1200 Subject: [PATCH 5/6] Fixed a silly mistake! --- server/functions.lua | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/functions.lua b/server/functions.lua index 04258b45d..646985330 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -130,13 +130,19 @@ exports('SaveInventory', SaveInventory) --- @param items table The items to set in the inventory. --- @param reason string The reason for setting the items. function SetInventory(identifier, items, reason) - if not Inventories[identifier] then return end local player = QBCore.Functions.GetPlayer(identifier) + print('Setting inventory for ' .. identifier) + + if not player and not Inventories[identifier] and not Drops[identifier] then + print('SetInventory: Inventory not found') + return + end + if player then player.Functions.SetPlayerData('items', items) if not player.Offline then - local logMessage = string.format('**%s (citizenid: %s | id: %s)** items set: %s', GetPlayerName(source), player.PlayerData.citizenid, source, json.encode(items)) + local logMessage = string.format('**%s (citizenid: %s | id: %s)** items set: %s', GetPlayerName(identifier), player.PlayerData.citizenid, identifier, json.encode(items)) TriggerEvent('qb-log:server:CreateLog', 'playerinventory', 'SetInventory', 'blue', logMessage) end elseif Drops[identifier] then @@ -345,10 +351,7 @@ exports('GetItemCount', GetItemCount) --- @return string|nil - Returns a string indicating the reason why the item cannot be added (e.g., 'weight' or 'slots'), or nil if it can be added. function CanAddItem(identifier, item, amount) - if not Inventories[identifier] then return false end - local Player = QBCore.Functions.GetPlayer(identifier) - if not Player then return false end local itemData = QBCore.Shared.Items[item:lower()] if not itemData then return false end @@ -365,6 +368,11 @@ function CanAddItem(identifier, item, amount) items = Inventories[identifier].items end + if not inventory then + print('CanAddItem: Inventory not found') + return false + end + local weight = itemData.weight * amount local totalWeight = GetTotalWeight(items) + weight if totalWeight > inventory.maxweight then From 1e7fbdce83603ef9a26537bbba13695acb4c4bd4 Mon Sep 17 00:00:00 2001 From: Meep <50252724+DrMeepso@users.noreply.github.com> Date: Sat, 21 Sep 2024 23:27:11 +1200 Subject: [PATCH 6/6] Update functions.lua --- server/functions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/functions.lua b/server/functions.lua index 40ebc61e4..79b3b27f6 100644 --- a/server/functions.lua +++ b/server/functions.lua @@ -379,7 +379,7 @@ function CanAddItem(identifier, item, amount) return false, 'weight' end - local slotsUsed, slotsFree = GetSlots(identifier) + local slotsUsed, _ = GetSlots(identifier) if slotsUsed >= inventory.slots then return false, 'slots'