Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function logMsg(text) print("[" .. os.date("%T") .. "] " .. text) end
- local function selectItem(id)
- -- match exact
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data and data.name == id then
- turtle.select(i)
- return true
- end
- end
- -- match by tags
- for i = 1, 16 do
- local data = turtle.getItemDetail(i, true)
- if data then
- for tag, _ in pairs(data.tags) do
- if tag == id then
- turtle.select(i)
- return true
- end
- end
- end
- end
- -- no match
- return false
- end
- local function nKeys(t)
- local i = 0
- for _, _ in pairs(t) do i = i + 1 end
- return i
- end
- local function getInventory()
- local result = {}
- for i = 1, 16 do
- local details = turtle.getItemDetail(i, true)
- if details then
- if details.name then result[details.name] = true end
- if details.tags then
- for tag, _ in pairs(details.tags) do
- result[tag] = true
- end
- end
- end
- end
- return result
- end
- local function getItemInPeripheral(peripheral, id)
- -- match exact
- for i = 1, peripheral.size() do
- local data = peripheral.getItemDetail(i)
- if data and data.name == id then return i end
- end
- -- match by tags
- for i = 1, peripheral.size() do
- local data = peripheral.getItemDetail(i, true)
- if data then
- for tag, _ in pairs(data.tags) do
- if tag == id then return i end
- end
- end
- end
- -- no match
- return nil
- end
- local function selectEmptySlot()
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if not data then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- local function selectItemOrEmpty(id) return selectItem(id) or selectEmptySlot() end
- local RECIPES = {
- ["iceandfire:dragonsteel_fire_ingot"] = {
- "minecraft:iron_ingot", "iceandfire:fire_dragon_blood"
- },
- ["iceandfire:dragonsteel_ice_ingot"] = {
- "minecraft:iron_ingot", "iceandfire:ice_dragon_blood"
- },
- ["iceandfire:dragonsteel_lightning_ingot"] = {
- "minecraft:iron_ingot", "iceandfire:lightning_dragon_blood"
- },
- ["allthemodium:vibranium_allthemodium_alloy_ingot"] = {
- "allthemodium:vibranium_dust", "allthemodium:allthemodium_dust"
- },
- ["allthemodium:unobtainium_vibranium_alloy_ingot"] = {
- "allthemodium:unobtainium_dust", "allthemodium:vibranium_dust"
- },
- ["allthemodium:unobtainium_allthemodium_alloy_ingot"] = {
- "allthemodium:unobtainium_dust", "allthemodium:allthemodium_dust"
- }
- }
- local function possibleRecipes()
- local inventory = getInventory()
- local possible = {}
- for result, recipe in pairs(RECIPES) do
- local isPossible = true
- for _, ingredient in ipairs(recipe) do
- if not inventory[ingredient] then
- isPossible = false
- break
- end
- end
- if isPossible then possible[result] = recipe end
- end
- return possible
- end
- local forgeInput, forgeOutput
- local function mainLoop()
- if not forgeInput then forgeInput = peripheral.wrap("front") end
- if not forgeOutput then forgeOutput = peripheral.wrap("top") end
- local recipes = possibleRecipes()
- if nKeys(recipes) > 0 then
- for result, ingredients in pairs(recipes) do
- logMsg("Crafting " .. result .. "...")
- for _, ingredient in ipairs(ingredients) do
- logMsg("Adding " .. ingredient .. "...")
- while not selectItem(ingredient) do os.sleep(0.1) end
- while not turtle.drop(1) do -- input to the front
- os.sleep(0.1)
- end
- end
- logMsg("Waiting for result...")
- while not getItemInPeripheral(forgeOutput, result) do
- os.sleep(0.1)
- end
- while not turtle.suckUp(1) do os.sleep(.1) end -- output above
- logMsg("Craft complete.")
- break
- end
- end
- os.sleep(0.1)
- end
- local function errorHandler(error)
- if error == "Terminated" then
- return true
- else
- logMsg("internal error: " .. error)
- return false
- end
- end
- logMsg("Dragonforge crafter started up.")
- while true do
- local _, shouldExit = xpcall(mainLoop, errorHandler)
- if shouldExit then break end
- -- mainLoop() -- for debugging
- end
Advertisement
Add Comment
Please, Sign In to add comment