Allow crafting multiple items

master
D4VID 9 months ago
parent 03f0922e4e
commit d30888cb69

@ -326,8 +326,9 @@ end
---accepts a recipe table ---accepts a recipe table
---@param search string search for an craftable item ---@param search string search for an craftable item
---@param amount number how many items to craft
---@return boolean ---@return boolean
local function craft_item(search) local function craft_item(search, amount)
search = string.lower(search) -- convert to unified lowercase search = string.lower(search) -- convert to unified lowercase
local matches = {} local matches = {}
@ -374,8 +375,13 @@ local function craft_item(search)
local recipe = recipes[selected] local recipe = recipes[selected]
print('Trying to craft ' .. recipe.result) print('Trying to craft ' .. amount .. 'x ' .. recipe.result)
local repeating = false
local count = math.ceil(amount / recipe.count)
for _ = 1, count do
local available_items = read_chest_contents() local available_items = read_chest_contents()
if not available_items then if not available_items then
print('Not facing a chest') print('Not facing a chest')
@ -402,39 +408,39 @@ local function craft_item(search)
return false return false
end end
if not repeating then
if not confirm() then if not confirm() then
print('Operation cancelled') print('Operation cancelled')
return false return false
end end
end
-- print('\nCrafting in order:')
-- for i, rec in ipairs(crafting_order) do
-- print(i, rec.result)
-- end
-- if not confirm() then
-- print('Operation cancelled')
-- return false
-- end
-- proceed to crafting the things -- proceed to crafting the things
local steps = #crafting_order
for i, rec in ipairs(crafting_order) do for i, rec in ipairs(crafting_order) do
print('Step #' .. i .. ' - ' .. rec.result) print('Step ' .. i .. '/' .. steps .. ' - ' .. rec.result)
if not craft_single_recipe(rec, 1) then if not craft_single_recipe(rec, 1) then
print('Unexpected error') print('Unexpected error')
return false return false
end end
end end
repeating = true
end
return true return true
end end
local target = ... local target, amount = ...
if not target then if not target then
print("Usage: craft <item>") print("Usage: craft <item> [count]")
return return
end end
craft_item(target) if not amount then
amount = 1
end
craft_item(target, amount)

Loading…
Cancel
Save