From 0b09b6b1a170a8745857deaaf499aacce7f0cb05 Mon Sep 17 00:00:00 2001 From: Subham PRO <33375250+subhampro@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:03:24 +0530 Subject: [PATCH] fix(server/crafting): weight discrepancy during crafting (#1849) Resolves #1848. --- modules/crafting/server.lua | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/modules/crafting/server.lua b/modules/crafting/server.lua index ddb65ca7f..705279f3d 100644 --- a/modules/crafting/server.lua +++ b/modules/crafting/server.lua @@ -113,12 +113,32 @@ lib.callback.register('ox_inventory:craftItem', function(source, id, index, reci local craftedItem = Items(recipe.name) local craftCount = (type(recipe.count) == 'number' and recipe.count) or (table.type(recipe.count) == 'array' and math.random(recipe.count[1], recipe.count[2])) or 1 - local newWeight = left.weight + (craftedItem.weight + (recipe.metadata?.weight or 0)) * craftCount + + -- Modified weight calculation + local newWeight = left.weight + local items = Inventory.Search(left, 'slots', tbl) or {} ---@todo new iterator or something to accept a map + -- First subtract weight of ingredients that will be removed + for name, needs in pairs(recipe.ingredients) do + if needs > 0 then + local item = Items(name) + if item then + newWeight -= (item.weight * needs) + end + end + end + + -- Add weight of crafted item + newWeight += (craftedItem.weight + (recipe.metadata?.weight or 0)) * craftCount + + if newWeight > left.maxWeight then return false, 'cannot_carry' end + local items = Inventory.Search(left, 'slots', tbl) or {} table.wipe(tbl) for name, needs in pairs(recipe.ingredients) do + if needs == 0 then break end + local slots = items[name] or items if #slots == 0 then return end @@ -149,13 +169,10 @@ lib.callback.register('ox_inventory:craftItem', function(source, id, index, reci end end elseif needs <= slot.count then - local itemWeight = slot.weight / slot.count - newWeight = (newWeight - slot.weight) + (slot.count - needs) * itemWeight tbl[slot.slot] = needs break else tbl[slot.slot] = slot.count - newWeight -= slot.weight needs -= slot.count end @@ -165,10 +182,6 @@ lib.callback.register('ox_inventory:craftItem', function(source, id, index, reci end end - if newWeight > left.maxWeight then - return false, 'cannot_carry' - end - if not TriggerEventHooks('craftItem', { source = source, benchId = id,