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,67 +375,72 @@ 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 available_items = read_chest_contents() local repeating = false
if not available_items then
print('Not facing a chest')
return false
end
local required_items, crafting_order = craft_recipe_recursion(recipe, clone(available_items), {}) local count = math.ceil(amount / recipe.count)
print('\nRequires these items:') for _ = 1, count do
local failed = false local available_items = read_chest_contents()
for required_item, required_count in pairs(required_items) do if not available_items then
local available = available_items[required_item] print('Not facing a chest')
if not available then return false
available = 0
end
if available < required_count then
print(available .. "/" .. required_count .. "\t" .. required_item .. "\t- Missing " .. required_count - available)
failed = true
else
print(available .. "/" .. required_count .. "\t" .. required_item)
end end
end
if failed then
return false
end
if not confirm() then local required_items, crafting_order = craft_recipe_recursion(recipe, clone(available_items), {})
print('Operation cancelled')
return false
end
-- print('\nCrafting in order:') print('\nRequires these items:')
-- for i, rec in ipairs(crafting_order) do local failed = false
-- print(i, rec.result) for required_item, required_count in pairs(required_items) do
-- end local available = available_items[required_item]
if not available then
-- if not confirm() then available = 0
-- print('Operation cancelled') end
-- return false if available < required_count then
-- end print(available .. "/" .. required_count .. "\t" .. required_item .. "\t- Missing " .. required_count - available)
failed = true
-- proceed to crafting the things else
for i, rec in ipairs(crafting_order) do print(available .. "/" .. required_count .. "\t" .. required_item)
print('Step #' .. i .. ' - ' .. rec.result) end
if not craft_single_recipe(rec, 1) then end
print('Unexpected error') if failed then
return false return false
end end
if not repeating then
if not confirm() then
print('Operation cancelled')
return false
end
end
-- proceed to crafting the things
local steps = #crafting_order
for i, rec in ipairs(crafting_order) do
print('Step ' .. i .. '/' .. steps .. ' - ' .. rec.result)
if not craft_single_recipe(rec, 1) then
print('Unexpected error')
return false
end
end
repeating = true
end 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