Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- turn this on to enable debug logging
- debugLogging = false
- -- where to write the things
- logfile = fs.open("/autocrafter9000.log", "w")
- -- Initialize ME Interface
- interface = peripheral.wrap("right")
- -- Initialize Monitor
- monitor = peripheral.wrap("left")
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.black)
- monitor.setTextScale(1)
- monitor.clear()
- -- time to sleep between checks
- sleepSeconds = .05
- -- Max Crafting CPUs to be active at once
- maxCPUs = 8
- -- Number of jobs started by this code rather than manually
- autocraftJobs = 0
- -- Stock Table
- stock = {
- { name = "Enriched Alloy", id = "mekanism:enrichedalloy", quantity = 60000, maxCraft = 2000},
- { name = "Reinforced Alloy", id = "mekanism:reinforcedalloy", quantity = 20000, maxCraft = 2000},
- { name = "Atomic Alloy", id = "mekanism:atomicalloy", quantity = 10000, maxCraft = 2000},
- { name = "Energy Tablet", id = "mekanism:energytablet", quantity = 20000, maxCraft = 1000},
- { name = "Ultimate Energy Cube", id = "mekanism:energycube", quantity = 64, maxCraft = 4, metadata = {"displayName", "Ultimate Energy Cube"}},
- { name = "Ultimate Induction Cell", id = "mekanism:basicblock2@3", quantity = 64, maxCraft = 1, metadata = {"displayName", "Ultimate Induction Cell"}},
- { name = "Lithium Dust", id = "ic2:dust@11", quantity = 10000},
- { name = "Logic Processor", id = "appliedenergistics2:material@22", quantity = 16000, maxCraft = 400},
- { name = "Calculation Processor", id = "appliedenergistics2:material@23", quantity = 16000, maxCraft = 400},
- { name = "Engineering Processor", id = "appliedenergistics2:material@24", quantity = 16000, maxCraft = 400}
- }
- -- Logs event to file and prints to screen
- function log(str)
- logfile.writeLine(str)
- print(str)
- end
- -- Logs event to file and prints to screen if debug is set to true
- function debug(str)
- if debugLogging then
- log(str)
- end
- end
- -- Tests if any crafting CPUs are in use
- function isAvailableCpu(interface)
- cpus = interface.getCraftingCPUs()
- count = 0
- for cpu, metadata in pairs(cpus) do
- if metadata.busy == true then
- count = count + 1
- end
- end
- return (count == autocraftJobs) and (count < maxCPUs)
- end
- -- Finds the item in the attached ME network
- function findItem(stockInfo)
- items = interface.findItems(stockInfo["id"])
- if items[1] then
- if stockInfo["metadata"] then
- meta = stockInfo["metadata"]
- for i, item in ipairs(items) do
- if item.getMetadata()[meta[1]] == meta[2] then
- items = {item}
- end
- end
- end
- return items[1]
- end
- end
- function padStringRight(str, length)
- pad = length - string.len(str)
- for i = 1, pad do
- str = str .. " "
- end
- return str
- end
- -- Outputs GUI to attached monitor
- function drawGUI()
- x, y = monitor.getSize()
- for i, stockInfo in ipairs(stock) do
- monitor.setCursorPos(1, y)
- item = findItem(stockInfo)
- if item.getMetadata().count >= stockInfo.quantity then
- monitor.setBackgroundColor(colors.green)
- elseif stockInfo["job"] then
- monitor.setBackgroundColor(colors.lightBlue)
- else
- monitor.setBackgroundColor(colors.red)
- end
- str = padStringRight(findItem(stockInfo).getMetadata().count .. "/" .. stockInfo["quantity"], 12) .. " | " .. stockInfo["name"]
- monitor.write(padStringRight(str, x))
- y = y - 1
- end
- end
- -- Initialize connected monitor
- if monitor then
- monitor.setCursorBlink(false)
- monitor.clear()
- end
- -- Core autostocking loop
- while true do
- -- For each item in the Stock Table
- for i, stockInfo in ipairs(stock) do
- id = stockInfo["id"]
- quantity = stockInfo["quantity"]
- debug("Testing Stock for " .. id)
- -- Find item in attached network
- item = findItem(stockInfo)
- if item then
- metadata = item.getMetadata()
- count = metadata.count
- debug("Got network item " .. id .. " qty " .. count)
- else break end
- -- Test if any open jobs for this item have finished
- if (stockInfo["job"] and stockInfo["job"].isFinished()) then
- stockInfo["job"] = nil
- autocraftJobs = autocraftJobs - 1
- log("Crafting Job for " .. id .. " finished")
- end
- -- Check that crafting is necessary
- if quantity > count and stockInfo["job"] == nil and redstone.getInput("top") then
- if isAvailableCpu(interface) then
- -- Check max craft size
- if stockInfo["maxCraft"] then
- craftAmount = math.min(quantity - count, stockInfo["maxCraft"])
- else
- craftAmount = quantity - count
- end
- -- Start crafting job
- job = item.craft(craftAmount)
- log("Crafting " .. craftAmount .. " of " .. id)
- if job.status() == "missing" then
- log("Missing Ingredients to craft " .. id)
- else
- stockInfo["job"] = job
- autocraftJobs = autocraftJobs + 1
- end
- else
- debug("Missing CPU to craft " .. id)
- end
- end
- drawGUI()
- end
- os.sleep(sleepSeconds)
- end
Add Comment
Please, Sign In to add comment