Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- --local util = require("util")
- local __ = dofile("underscore.lua")
- local recipes = dofile("recipes.lua")
- local Screen = dofile("screen.lua")
- local config = dofile("config.lua")
- local destinations = config.destinations
- local screen = Screen:new(peripheral.wrap("monitor_1"))
- local me = peripheral.wrap("appliedenergistics2:interface_1") -- me_interface/controller
- local buffer_chests = __.map(config.buffer_chests, function(i) return {name="minecraft:ironchest_diamond_"..i, peripheral=peripheral.wrap("minecraft:ironchest_diamond_"..i), in_use=false} end)
- local me_chest_name = config.me_chest_name
- local me_chest_location = config.me_chest_location
- local item_name_length = 37
- rednet.open("right")
- screen:reset()
- string.rpad = function(str, len, char)
- if char == nil then char = ' ' end
- return string.rep(char, len - #str) .. str
- end
- string.lpad = function(str, len, char)
- if char == nil then char = ' ' end
- return str .. string.rep(char, len - #str)
- end
- local crafter = {}
- local function main()
- while true do
- local e,p1,p2,p3,p4,p5 = os.pullEventRaw()
- --print(e,p1,p2,p3,p4,p5)
- if e == "modem_message" then
- --print(e .. ": " .. p4.message[1])
- local name = p4.message[1]
- local count = 1
- if p4.message[2] then
- count = tonumber(p4.message[2]) or 1
- end
- screen:reset_stack()
- screen:reset()
- --local count = p4.message[2] or 1
- if name ~= nil then
- if count == 1 then
- crafter.craft(name)
- screen:push("finished")
- else
- for i=1,count do
- screen:reset_stack()
- screen:reset()
- screen:push_and_indent(name .. " ("..i.."/"..count..")", 2)
- screen:reset()
- crafter.craft(name)
- end
- screen:reset_stack()
- screen:push_and_indent(name .. " ("..count.."/"..count..")", 2)
- screen:push("finished")
- screen:reset()
- end
- end
- else
- crafter.check_requests()
- end
- os.sleep(1)
- end
- end
- crafter.get_buffer_chest = function()
- -- get first buffer chest not in use
- return __.detect(buffer_chests, function(c) return not c.in_use end)
- end
- crafter.craft = function(name, is_subcraft)
- is_subcraft = is_subcraft or false
- local buffer_chest = crafter.get_buffer_chest()
- if buffer_chest == nil then
- error("No buffer chest available")
- return
- end
- local recipe = recipes[name]
- if not is_subcraft then
- screen:push_and_indent(name, 2)
- else
- screen:push_and_indent(string.lpad(name .. "...", item_name_length) .. "crafting", 2)
- end
- for i,item in ipairs(recipe) do
- local item_name = item.item.name or item.item
- local display_name = item.item.display_name or item.display_name
- if type(item.item) == 'table' and item.item.damage and display_name == nil then
- display_name = item_name .. "@" .. tostring(item.item.damage)
- end
- local finished = false
- local success, ae2_item = crafter.get_item(item)
- if success and (ae2_item ~= nil) then
- local item_meta = ae2_item.getMetadata()
- screen:push(string.lpad(item_meta.displayName .. "...", item_name_length))
- if crafter.get_or_craft_ae2(item) then
- --screen:pop_many(2)
- screen:push(string.lpad(item_meta.displayName .. "...", item_name_length) .. "ready")
- screen:reset()
- finished = true
- end
- else
- screen:push(string.lpad(display_name .. "...", item_name_length) .. "missing")
- screen:reset()
- crafter.wait_for_item(item) -- in case of the 'missing' condition above, we can wait for it to show up
- screen:pop()
- screen:push(string.lpad(display_name .. "...", item_name_length) .. "ready")
- screen:reset()
- end
- if item.recipe ~= nil and not finished then
- screen:pop()
- crafter.craft(item.recipe, true)
- crafter.wait_for_item(item)
- screen:pop_many(2)
- screen:push(string.lpad(item.recipe .. "...", item_name_length) .. "ready")
- screen:reset()
- end
- crafter.export_to_buffer_chest(item, buffer_chest)
- end
- if destinations[name] and peripheral.isPresent(destinations[name]) then
- crafter.transfer_chest(buffer_chest.peripheral, destinations[name])
- buffer_chest.in_use = false
- else
- screen:push("unable to find destination: " .. tostring(destinations[name]))
- screen:reset()
- crafter.import_buffer_chest(buffer_chest)
- exit() -- this crashes the program, which works, I guess
- end
- screen:pop_many(#recipe)
- screen:reset()
- end
- crafter.wait_for_item = function(item)
- while true do
- local success, ae2item = crafter.get_item(item)
- if success and (ae2item ~= nil) then
- local meta = ae2item.getMetadata()
- local missing = (item.count or 1) - meta.count
- if missing <= 0 then
- return
- end
- end
- os.sleep(1)
- end
- end
- crafter.export_to_buffer_chest = function(item, buffer_chest)
- local success, ae2item = crafter.get_item(item)
- if success and (ae2item ~= nil) then
- print("exporting to " .. me_chest_location)
- print("item.count: " .. tostring(item.count))
- print("item: " .. ae2item.getMetadata().displayName)
- local count = item.count or 1
- local exported = ae2item.export(me_chest_location, count)
- crafter.transfer_chest(peripheral.wrap(me_chest_name), buffer_chest.name)
- buffer_chest.in_use = true
- else
- error("couldn't export " .. tostring(item.name))
- end
- end
- crafter.transfer_chest = function(from, to)
- local i = 1
- while true do
- local count = from.pushItems(to, i)
- if count == 0 then
- break
- end
- i = i + 1
- end
- end
- crafter.import_buffer_chest = function(buffer_chest)
- local chest = buffer_chest.peripheral
- local i = 1
- buffer_chest.in_use = false
- while true do
- local count = chest.pushItems(me_chest_name, i)
- if count == 0 then
- break
- end
- i = i + 1
- end
- crafter.import_me_chest()
- end
- crafter.import_me_chest = function()
- local i = 1
- while true do
- local count = me.pullItems(me_chest_location, i)
- if count == 0 then
- return
- end
- i = i + 1
- end
- end
- crafter.get_item = function(item)
- return pcall(me.findItem, item.item)
- end
- crafter.get_or_craft_ae2 = function(item)
- local success, ae2item = pcall(me.findItem, item.item)
- if success then
- if ae2item ~= nil then
- local meta = ae2item.getMetadata()
- local missing = (item.count or 1) - meta.count
- if missing > 0 then
- local craft = ae2item.craft(missing)
- if craft ~= nil then
- screen:pop_many(1)
- screen:push(string.lpad(meta.displayName .. "...", item_name_length) .. "crafting")
- screen:reset()
- while not craft.isFinished() do
- os.sleep(1)
- if craft.isCanceled() then break end
- end
- screen:pop_many(1)
- screen:reset()
- return craft.isFinished()
- end
- else
- screen:pop_many(1)
- screen:reset()
- return true
- end
- end
- end
- return false
- end
- --crafter.transfer_chest(buffer_chests[1], buffer_chests[2])
- __.each(buffer_chests, function(chest) crafter.import_buffer_chest(chest) end)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement