Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- initialize Monitor
- local monitor = peripheral.find("monitor")
- if not monitor then error("Monitor not found.") end
- monitor.setTextScale(0.5)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setCursorBlink(false)
- print("Monitor initialized.")
- -- Init ColonyIntergrator
- local colony = peripheral.find("colonyIntegrator")
- if colony == nil then error("colonyIntegrator not found") end
- if not colony.isInColony then error("Block is not in a colony") end
- -- Init RSBridge
- local bridge = peripheral.find("rsBridge")
- if bridge == nil then error("rsBridge not found") end
- --bridge.exportItem({name="minecraft:birch_stairs", count=1}, "left")
- function PullRequiredMats(colony_requests)
- colony_requests = colony.getRequests()
- local UnableToCraft = {}
- --get requests
- local item_needed_key = {}
- local item_needed_state = {}
- local item_needed_name = {}
- local item_needed_count = {}
- local item_needed_target = {}
- local item_needed_detailname = {}
- local requestcounter = 0
- for key, item in pairs(colony_requests) do
- item_needed_key[requestcounter] = key
- item_needed_name[requestcounter] = item.name
- item_needed_state[requestcounter] = item.state
- item_needed_count[requestcounter] = item.count
- item_needed_target[requestcounter] = item.target
- --print("Item " .. key)
- --print("Name: " .. item.name)
- --print("State: " .. item.state)
- --print("Count: " .. item.count)
- --print("Target: " .. item.target)
- --print("Items: ")
- for j, item_detail in ipairs(item.items) do
- print("\t" .. item_detail.name)
- item_needed_detailname[requestcounter] = item_detail.name
- end
- requestcounter = requestcounter + 1
- end
- -- get items in system
- local item_current_name = {}
- local item_current_amount = {}
- local item_current_isCraftable = {}
- local item_current_displayName = {}
- current_items = bridge.listItems()
- local currentitemcounter = 0
- for _, item in pairs(current_items) do
- item_current_name[currentitemcounter] = (item.name or "Unknown")
- item_current_amount[currentitemcounter] = (item.amount)
- item_current_isCraftable[currentitemcounter] = (tostring(item.isCraftable))
- item_current_displayName[currentitemcounter] = (item.displayName)
- currentitemcounter = currentitemcounter + 1
- end
- print (currentitemcounter)
- for j = 0, requestcounter-1 do
- local RSUseable = 0
- local itemrequestedbutnotfound = item_needed_detailname[j]
- for i = 0, currentitemcounter-1 do
- if item_needed_state[j] == "IN_PROGRESS" then
- --print("Item1: " .. item_needed_detailname[j])
- --print("Itemfind: " .. item_current_name[i])
- --print("Needed: " .. item_current_isCraftable[i])
- --print("Available: " .. item_current_displayName[i])
- if item_current_name[i]:match(item_needed_detailname[j]) then
- --print("Item: " .. item_current_displayName[i])
- --print ("Item Needed 1: " .. item_needed_detailname[j])
- if item_current_amount[i] >= item_needed_count[j] then
- provided = bridge.exportItem({name=item_current_name[i], count=item_needed_count[j]}, "left")
- if provided == 0 then
- print("Unable to Export " .. item_current_name[i])
- else
- print("Exporting " .. provided .. " of " .. item_current_name[i])
- local item = item_current_name[i]
- local needed = item_needed_count[j]
- RSUseable = 1
- end
- if item_current_name[i] == "domum_ornamentum:shingle" then
- print ("Unable to export " .. item_current_name[i])
- RSUseable = 0
- end
- else
- -- If you don't find enough craft the missing ammount if craftable.
- if item_current_isCraftable[i] == "true" then
- print("Craft more")
- print(item_current_name[i])
- bridge.craftItem({name=item_current_name[i], count=item_needed_count[j]})
- RSUseable = 1
- else
- print("Unable to craft")
- print(item_current_isCraftable[i])
- RSUseable = 0
- print(item_needed_detailname[j])
- NotCraftable = { name=item_needed_detailname[j]}
- table.insert(UnableToCraft, NotCraftable)
- end
- end
- else
- --print("No Has")
- --print("Item: " .. item_current_displayName[i])
- --print ("Item Needed: " .. item_needed_name[j])
- end
- else
- RSUseable = 1
- end
- end
- if RSUseable == 0 then
- --file.write(itemrequestedbutnotfound)
- --file.write("\n")
- end
- end
- ---- Display lists
- row = 3
- monitor.clear()
- header_shown = 0
- for e in pairs(UnableToCraft) do
- NotCraftable = UnableToCraft[e]
- if header_shown == 0 then
- mPrintRowJustified(monitor, row, "center", "Unable To Craft")
- header_shown = 1
- row = row + 1
- end
- text = string.format("%d %s", 1, NotCraftable.name)
- mPrintRowJustified(monitor, row, "left", text)
- row = row + 1
- end
- if row == 3 then mPrintRowJustified(monitor, row, "center", "No Open Requests") end
- print("Scan completed at", textutils.formatTime(os.time(), false) .. " (" .. os.time() ..").")
- end
- --file.close()
- --itemfile.close()
- -- Prints strings left, centered, or right justified at a specific row and
- -- specific foreground/background color.
- function mPrintRowJustified(mon, y, pos, text, ...)
- w, h = mon.getSize()
- fg = mon.getTextColor()
- bg = mon.getBackgroundColor()
- if pos == "left" then x = 1 end
- if pos == "center" then x = math.floor((w - #text) / 2) end
- if pos == "right" then x = w - #text end
- if #arg > 0 then mon.setTextColor(arg[1]) end
- if #arg > 1 then mon.setBackgroundColor(arg[2]) end
- mon.setCursorPos(x, y)
- mon.write(text)
- mon.setTextColor(fg)
- mon.setBackgroundColor(bg)
- end
- ---Display countdown timer
- function displayTimer(mon, t)
- now = os.time()
- cycle = "day"
- cycle_color = colors.orange
- if now >= 4 and now < 6 then
- cycle = "sunrise"
- cycle_color = colors.orange
- elseif now >= 6 and now < 18 then
- cycle = "day"
- cycle_color = colors.yellow
- elseif now >= 18 and now < 19.5 then
- cycle = "sunset"
- cycle_color = colors.orange
- elseif now >= 19.5 or now < 5 then
- cycle = "night"
- cycle_color = colors.red
- end
- timer_color = colors.orange
- if t < 15 then timer_color = colors.yellow end
- if t < 5 then timer_color = colors.red end
- mPrintRowJustified(mon, 1, "left", string.format("Time: %s [%s] ", textutils.formatTime(now, false), cycle), cycle_color)
- if cycle ~= "night" then mPrintRowJustified(mon, 1, "right", string.format(" Remaining: %ss", t), timer_color)
- else mPrintRowJustified(mon, 1, "right", " Remaining: PAUSED", colors.red) end
- end
- ---Main Function
- local time_between_runs = 30
- local current_run = time_between_runs
- displayTimer(monitor, current_run)
- local TIMER = os.startTimer(1)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "timer" and e[2] == TIMER then
- now = os.time()
- if now >= 5 and now < 19.5 then
- current_run = current_run - 1
- if current_run <= 0 then
- colony_requests = colony.getRequests()
- PullRequiredMats(colony_requests)
- current_run = time_between_runs
- end
- end
- displayTimer(monitor, current_run)
- TIMER = os.startTimer(1)
- elseif e[1] == "monitor_touch" then
- os.cancelTimer(TIMER)
- colony_requests = colony.getRequests()
- PullRequiredMats(colony_requests)
- current_run = time_between_runs
- displayTimer(monitor, current_run)
- TIMER = os.startTimer(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement