71 lines
2.3 KiB
Lua
71 lines
2.3 KiB
Lua
local fs = require("filesystem")
|
|
local recipeLib = load(fs:readAllText("./recipeParseLib.lua"), "recipeParseLib.lua")()
|
|
|
|
local args = table.pack(...)
|
|
|
|
local resultItem = args[1]
|
|
assert(resultItem, "supply the item you want to add a recipe for as the first item")
|
|
|
|
local crafter = components:getFirst("digitalCrafter")
|
|
|
|
local recipes = crafter:searchRecipesWithResult(resultItem)
|
|
|
|
assert(#recipes > 0, ("no recipe found for %s."):format(resultItem))
|
|
|
|
|
|
local function acToLuaRecipe(acRecipe)
|
|
return {
|
|
resultCount = acRecipe.resultCount,
|
|
slots = acRecipe.ingredients,
|
|
resultItem = acRecipe.result
|
|
}
|
|
end
|
|
|
|
local kernel = require("kernel")
|
|
local keyEvents = {}
|
|
kernel:registerEventCallback("charTyped", function(...) table.insert(keyEvents, {...}) end)
|
|
kernel:registerEventCallback("keyPressed", function(...) table.insert(keyEvents, {...}) end)
|
|
|
|
---@param eventFilterOrNil "charTyped"|"keyPressed"|nil
|
|
local function waitForInputEvent(eventFilterOrNil)
|
|
sleep(0)
|
|
while #keyEvents == 0 do sleep(0.5) end
|
|
local res = table.remove(keyEvents, 1)
|
|
if eventFilterOrNil == nil or res[1] == eventFilterOrNil then
|
|
return res
|
|
else
|
|
return waitForInputEvent(eventFilterOrNil) or tailCallsAreSus
|
|
end
|
|
end
|
|
|
|
local recipe = recipes[1]
|
|
if #recipes > 1 then
|
|
print("Looks like there are multiple recipes. Please pick one of the following by typing the number on the left and then enter.")
|
|
for index, value in ipairs(recipes) do
|
|
print(("%d: %s"):format(index, recipeLib:serializeSingleRecipe(acToLuaRecipe(value))))
|
|
end
|
|
|
|
local buf = ""
|
|
while true do
|
|
printInline(("\rSelection: %s_"):format(buf))
|
|
local ctyped, repr = table.unpack(waitForInputEvent("charTyped"))
|
|
if repr == "\b" then
|
|
buf = buf:sub(1, #buf-1)
|
|
elseif repr == "\n" then
|
|
break
|
|
elseif repr ~= nil then
|
|
buf = buf .. repr
|
|
end
|
|
end
|
|
|
|
print()
|
|
local selection = tonumber(buf)
|
|
if selection == nil or selection < 1 or selection > #recipes then
|
|
error(("The inputted number '%s' is not a valid option"):format(buf))
|
|
end
|
|
recipe = recipes[selection]
|
|
end
|
|
|
|
recipeLib:addNewRecipe(acToLuaRecipe(recipe))
|
|
|
|
print(("Done, added the following recipe:\n%s"):format(recipeLib:serializeSingleRecipe(acToLuaRecipe(recipe)))) |