Skip to main content

cobblestone

--[[
The purpose of this program is to craft compressed cobblestone.
]]--

--GLOBAL VARIABLES--
args = {...}
args[1] = tonumber(args[1])

--FUNCTIONS--
function pause()
    print("Press any key to continue...")
    os.pullEvent("key")
end

--Read recipe from user.
function get_recipe()
    print("Please input recipe...")
    pause()
end

--Check if recipe is valid.
function is_recipe()
    print("Checking recipe...")
    if (turtle.craft(0) == false) then
        print("Not a valid recipe!")
        return false
    else
        print("Recipe is valid...")
        return true
    end
end

--Save recipe in memory for continuous crafting.
function save_recipe()
    local recipe = {} --Construct table.
    for i=1,12 do
        --Select 3x3 grid
        if (i == 4 or i == 8 or i==12) then
            i = i + 1
        else
            turtle.select(i) --Select each slot.
            local data = turtle.getItemDetail(i) --Save data of each slot.
            recipe[i] = data.name --Save recipe name.
        end
    end
    turtle.select(1)
end

--Craft cobblestone.
function craft(amount)
    if (#args == 1) then --Check for argv.
        turtle.craft(args[1]) --Craft argv.
    else
        turtle.craft(amount)
        if (turtle.craft(amount) == false) then --If cannot create more, then attempt to grab more
            --TODO: Make the robot check if object sucked is in the recipe.
            --If the object is in the recipe, input into correct slot. Otherwise, put the object in the
            --correct slot.
            return false
        end
    end
end

--Return amount of cobblestone to craft
function get_amount_to_craft()
    io.write("Input amount to craft: ")
    local amount = read()
    amount = tonumber(amount)
    return amount
end

--BEGINNING OF MAIN()--
if (#args == 1) then
    craft(args[1])
else
    get_recipe()
    local valid = is_recipe()
    if (valid == true) then
        save_recipe()
        local amount = get_amount_to_craft()
        turtle.craft(amount)
    end
end