multiCraft() refactor

An attempt to speed up `multiCraft()` function creation logic

Before it created a table of items while it was trying to check them I've separated this so it just gets that out of the way and checks items in it's own `for` loop

Also added a `maxCreation` variable that users can edit if needed which decides how many of an item can be created (I'll change this soon probably to tie into the config options in scripts for multicraft again soon

Also removed a few `Wait()`s that were there to help it not get confused, but after testing I believe this didn't make a difference, let me know if there's any issues with stuff like it saying it can craft when you dont have the items
This commit is contained in:
Jim Shield
2025-05-05 12:24:38 +01:00
parent 4e8a15590f
commit e9c54cbdd4

View File

@@ -80,11 +80,6 @@ function craftingMenu(data)
-- Build a table of all required ingredients (default quantity is 1). -- Build a table of all required ingredients (default quantity is 1).
for i = 1, #Recipes do for i = 1, #Recipes do
for k in pairs(Recipes[i]) do for k in pairs(Recipes[i]) do
if k == "hasCrafted" and not data.craftable.craftedItems then
-- Retreive list of already crafted items from playermetadata to see if we should class this recipe as "new"
craftedItems = GetMetadata(nil, "craftedItems") or {}
data.craftable.craftedItems = craftedItems
end
if k ~= "amount" and k ~= "metadata" and k ~= "job" and k ~= "gang" then if k ~= "amount" and k ~= "metadata" and k ~= "job" and k ~= "gang" then
tempCarryTable[k] = Recipes[i].amount or 1 tempCarryTable[k] = Recipes[i].amount or 1
end end
@@ -114,7 +109,7 @@ function craftingMenu(data)
settext = settext..(settext ~= "" and br or "")..(Items[l] and Items[l].label or "error - "..l)..(b > 1 and " x"..b or "") settext = settext..(settext ~= "" and br or "")..(Items[l] and Items[l].label or "error - "..l)..(b > 1 and " x"..b or "")
metaTable[Items[l] and Items[l].label or "error - "..l] = b metaTable[Items[l] and Items[l].label or "error - "..l] = b
itemTable[l] = b itemTable[l] = b
Wait(0) --Wait(0)
end end
while not canCarryTable do Wait(0) end while not canCarryTable do Wait(0) end
@@ -122,25 +117,16 @@ function craftingMenu(data)
setheader = ((metadata and metadata.label) or (Items[tostring(k)] and Items[tostring(k)].label) or "error - "..tostring(k)) setheader = ((metadata and metadata.label) or (Items[tostring(k)] and Items[tostring(k)].label) or "error - "..tostring(k))
..(Recipes[i]["amount"] > 1 and " x"..Recipes[i]["amount"] or "") ..(Recipes[i]["amount"] > 1 and " x"..Recipes[i]["amount"] or "")
if not disable then local statusEmoji = disable and "" or not canCarryTable[k] and " 📦" or " ✔️"
if not canCarryTable[k] then local isNew = (Recipes[i]["hasCrafted"] ~= nil and craftedItems[k] == nil) and "" or ""
setheader = setheader.." 📦" setheader = isNew .. setheader .. statusEmoji
else
setheader = setheader.." ✔️"
end
elseif not canCarryTable[k] then
setheader = setheader.." 📦"
end
if Recipes[i]["hasCrafted"] ~= nil and craftedItems[k] == nil then
setheader = ""..setheader
end
Menu[#Menu + 1] = { Menu[#Menu + 1] = {
arrow = not disable and canCarryTable[k], arrow = not disable and canCarryTable[k],
isMenuHeader = disable or not canCarryTable[k], isMenuHeader = disable or not canCarryTable[k],
icon = invImg((metadata and metadata.image) or tostring(k)), icon = invImg((metadata and metadata.image) or tostring(k)),
image = invImg((metadata and metadata.image) or tostring(k)), image = invImg((metadata and metadata.image) or tostring(k)),
header = setheader..((disable or not canCarryTable[k]) and "" or ""), header = setheader,
txt = (isStarted(QBMenuExport) or disable) and settext or nil, txt = (isStarted(QBMenuExport) or disable) and settext or nil,
metadata = metaTable, metadata = metaTable,
onSelect = (not disable and canCarryTable[k]) and function() onSelect = (not disable and canCarryTable[k]) and function()
@@ -162,7 +148,7 @@ function craftingMenu(data)
} }
end end
end end
Wait(0) --Wait(0)
end end
end end
@@ -208,16 +194,23 @@ end
function multiCraft(data) function multiCraft(data)
local max = 0 local max = 0
local stashName = nil local stashName = nil
for i = 1, 100 do local maxCreation = 100
local itemTable = {}
-- Generate item table to check against stash or inventory
-- takes into account the ingredients needed for multiple items
local multiItemTable = {}
for i = 1, maxCreation do
multiItemTable[i] = {}
for l, b in pairs(data.craft[data.item]) do for l, b in pairs(data.craft[data.item]) do
debugPrint("") multiItemTable[i][l] = (b * i)
itemTable[l] = (b * i) end
end end
for i = 1, maxCreation do
-- if its received a stash name, check if the items are in the stash
if data.stashName then if data.stashName then
debugPrint("") local hasItems, stashname = checkStashItem(data.stashName, multiItemTable[i])
local hasItems, stashname = checkStashItem(data.stashName, itemTable)
if hasItems == true then if hasItems == true then
max += 1 max += 1
stashName = stashname stashName = stashname
@@ -225,15 +218,14 @@ function multiCraft(data)
break break
end end
else else
debugPrint("") -- if not check the players inventory for the items
local has, _ = hasItem(itemTable, nil, nil) local has, _ = hasItem(multiItemTable[i], nil, nil)
if has then if has then
max += 1 max += 1
else else
break break
end end
end end
Wait(10)
end end
local dialog = createInput(data.craftable.Header, { local dialog = createInput(data.craftable.Header, {