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 = {
- ["elementalcraft:swift_alloy_ingot"] = {
- "minecraft:gold_ingot", "elementalcraft:drenched_iron_ingot",
- "minecraft:redstone", "elementalcraft:aircrystal"
- },
- ["elementalcraft:pedestal_fire"] = {
- "elementalcraft:infuser", "elementalcraft:gems/fine_fire",
- "elementalcraft:swift_alloy_ingot", "elementalcraft:whiterock",
- "elementalcraft:whiterock"
- },
- ["elementalcraft:pedestal_water"] = {
- "elementalcraft:infuser", "elementalcraft:gems/fine_water",
- "elementalcraft:swift_alloy_ingot", "elementalcraft:whiterock",
- "elementalcraft:whiterock"
- },
- ["elementalcraft:pedestal_earth"] = {
- "elementalcraft:infuser", "elementalcraft:gems/fine_earth",
- "elementalcraft:swift_alloy_ingot", "elementalcraft:whiterock",
- "elementalcraft:whiterock"
- },
- ["elementalcraft:pedestal_air"] = {
- "elementalcraft:infuser", "elementalcraft:gems/fine_air",
- "elementalcraft:swift_alloy_ingot", "elementalcraft:whiterock",
- "elementalcraft:whiterock"
- },
- ["elementalcraft:fireite_ingot"] = {
- "minecraft:netherite_ingot", "elementalcraft:swift_alloy_ingot",
- "elementalcraft:purecrystal"
- }
- }
- 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 function mainLoop()
- if not binder then binder = peripheral.find("elementalcraft:binder") 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
- turtle.drop(1) -- binder is in front of turtle
- end
- logMsg("Waiting for result...")
- while not getItemInPeripheral(binder, result) do
- os.sleep(0.1)
- end
- logMsg("Produced result!")
- while nKeys(binder.list()) > 0 do
- if not selectItemOrEmpty(result) then
- error("inventory full!")
- else
- turtle.suck(1) -- binder is in front of turtle
- while not turtle.dropUp(1) do -- output chest below
- os.sleep(0.1)
- end
- end
- end
- 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("Elemental binder crafter started up.")
- while true do
- local _, shouldExit = xpcall(mainLoop, errorHandler)
- if shouldExit then break end
- -- mainLoop() -- for debugging
- end
Add Comment
Please, Sign In to add comment