Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----- ME Network Minimum Count -----
- -- Created by: MrKMG V.1
- -- Please keep this header
- --Sides of
- monitor_location = "left"
- chest_location = "top"
- menet_location = "bottom"
- -- Warning!!!
- -- Nothing should be changed below
- -- Wrap the peripherals
- local monitor = peripheral.wrap("left")
- local chest = peripheral.wrap("top")
- local menet = peripheral.wrap("bottom")
- -- Setup screen variables
- local monitor_width
- local monitor_height
- local screen_rows
- local screen_columns
- local screen_center_x
- local screen_center_y
- local left_padding
- -- Other variables
- local items = {}
- local total_items
- local items_per_page
- -- local length_names
- local header_height = 2
- local length_qty = 4
- local length_cur = 4
- local is_popup = false
- local current_pop = 0
- local pages = 0
- local c_page = 1
- local item_pos = {}
- --Setup and calculate information for monitor
- function setupMonitor()
- monitor_width, monitor_height = monitor.getSize()
- screen_columns = math.floor(monitor_width/18)
- screen_rows = math.floor((monitor_height - header_height)/2)
- left_padding = math.floor((monitor_width - (screen_columns * 18))/2)
- screen_center_x = math.floor(monitor_width/2)
- screen_center_y = math.floor(monitor_height/2)
- items_per_page = screen_rows * screen_columns
- monitor.setTextScale(0.5)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- end
- function printHeader()
- monitor.setBackgroundColor(colors.gray)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(screen_center_x - 12,1)
- monitor.clearLine()
- monitor.write("MENetwork Minimum Counts")
- end
- function printFooter()
- monitor.setBackgroundColor(colors.gray)
- monitor.setCursorPos(2,monitor_height)
- monitor.clearLine()
- monitor.setBackgroundColor(colors.green)
- monitor.setTextColor(colors.white)
- monitor.write("Prev")
- monitor.setCursorPos(8,monitor_height)
- monitor.write("Scan")
- monitor.setCursorPos(14,monitor_height)
- monitor.write("Save")
- monitor.setCursorPos(20,monitor_height)
- monitor.write("Load")
- monitor.setCursorPos(26,monitor_height)
- monitor.write("Next")
- end
- -- TODO Use this to parse a more readable name
- function getNameOfItem(raw)
- return raw
- end
- -- Scan items in chest and add them to the system - Will also reset count to 1
- function scanItems()
- local i, item, rawName
- for i,item in pairs(chest.getAllStacks()) do
- rawName = item.rawName
- items[rawName] = {}
- items[rawName].id = item.id
- items[rawName].dmg = item.dmg
- items[rawName].needed = 1
- items[rawName].name = item.name
- items[rawName].qty = 0
- items[rawName].prev_qty = 0
- items[rawName].is_error = false
- items[rawName].did_request = false
- items[rawName].request_timeout = 0
- end
- end
- function getItemCounts()
- local i, item
- for i,item in pairs(items) do
- items[i].qty = menet.countOfItemType(item.id,item.dmg)
- end
- end
- function requestItem(rawName)
- local requestItem = {}
- requestItem.id = items[rawName].id
- requestItem.dmg = items[rawName].dmg
- requestItem.qty = 1
- menet.requestCrafting(requestItem)
- items[rawName].did_request = true
- items[rawName].request_timeout = 60
- items[rawName].prev_qty = items[rawName].qty
- end
- function checkAllItems()
- local i, item
- for i,item in pairs(items) do
- if item.did_request then
- if item.qty ~= item.prev_qty then
- items[i].did_request = false
- elseif item.request_timeout == 0 then
- items[i].did_request = false
- items[i].is_error = true
- else
- items[i].request_timeout = item.request_timeout - 1
- end
- end
- if item.qty < item.needed and item.did_request == false and item.is_error == false then
- requestItem(i)
- end
- end
- end
- function showPopup(rawName)
- if items[rawName].did_request then
- monitor.setBackgroundColor(colors.yellow)
- monitor.setTextColor(colors.yellow)
- elseif items[rawName].is_error then
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.red)
- else
- monitor.setBackgroundColor(colors.green)
- monitor.setTextColor(colors.green)
- end
- monitor.setCursorPos(screen_center_x - 12,screen_center_y-2)
- monitor.write(" ")
- monitor.setCursorPos(screen_center_x - 12,screen_center_y-1)
- monitor.write(" ")
- monitor.setCursorPos(screen_center_x - 12,screen_center_y)
- monitor.write(" ")
- monitor.setCursorPos(screen_center_x - 12,screen_center_y+1)
- monitor.write(" ")
- monitor.setCursorPos(screen_center_x - 12,screen_center_y+2)
- monitor.write(" ")
- monitor.setTextColor(colors.black)
- monitor.setCursorPos(screen_center_x + 11,screen_center_y-2)
- monitor.write("X")
- monitor.setCursorPos(screen_center_x - 9,screen_center_y-1)
- monitor.write(string.sub(items[rawName].name,-18))
- monitor.setCursorPos(screen_center_x - 10,screen_center_y)
- monitor.write(items[rawName].qty)
- monitor.setCursorPos(screen_center_x + 1 ,screen_center_y)
- monitor.write(items[rawName].needed)
- monitor.setCursorPos(screen_center_x - 3, screen_center_y + 1)
- monitor.write("<")
- monitor.setCursorPos(screen_center_x - 7, screen_center_y + 1)
- monitor.write("<<")
- monitor.setCursorPos(screen_center_x - 11, screen_center_y + 1)
- monitor.write("<<<")
- monitor.setCursorPos(screen_center_x + 1,screen_center_y + 1)
- monitor.write(">")
- monitor.setCursorPos(screen_center_x + 4,screen_center_y + 1)
- monitor.write(">>")
- monitor.setCursorPos(screen_center_x + 8,screen_center_y + 1)
- monitor.write(">>>")
- is_popup = true
- current_pop = rawName
- end
- function calcTotalItems()
- total_items = 0
- local i,j
- for i,j in pairs(items) do
- total_items = total_items + 1
- end
- end
- function calcPages()
- pages = math.floor(total_items / items_per_page)
- end
- function clearPageArea()
- monitor.setBackgroundColor(colors.black)
- for i=header_height+1,monitor_height-1 do
- monitor.setCursorPos(1,i)
- monitor.clearLine()
- end
- end
- function printPage()
- local starting_item = (c_page - 1) * items_per_page
- local ending_item = (c_page) * items_per_page
- local rawName, item
- item_pos = {}
- local i = 0
- local r = 1
- local c = 1
- for rawName,item in pairs(items) do
- if i >= starting_item and i < ending_item then
- if c == 1 then
- item_pos[r] = {}
- monitor.setBackgroundColor(colors.black)
- monitor.setCursorPos(1,(r * 2) + header_height)
- monitor.clearLine()
- end
- if item.did_request then
- monitor.setBackgroundColor(colors.yellow)
- elseif item.is_error then
- monitor.setBackgroundColor(colors.red)
- else
- monitor.setBackgroundColor(colors.green)
- end
- monitor.setTextColor(colors.black)
- monitor.setCursorPos(left_padding + ((c-1)*18) + 2,(r * 2) + header_height)
- monitor.write(string.sub(item.name,-16))
- item_pos[r][c] = rawName
- if c >= screen_columns then
- c = 1
- r = r + 1
- else
- c = c + 1
- end
- end
- i = i + 1
- end
- end
- function saveItems()
- local file = io.open("MENetSave","w")
- file:write(textutils.serialize(items))
- file:close()
- end
- function loadItems()
- local file = io.open("MENetSave","r")
- if file ~= nil then
- items = textutils.unserialize(file:read("*a"))
- file:close()
- else
- items = {}
- end
- refresh()
- end
- function refresh()
- calcTotalItems()
- calcPages()
- clearPageArea()
- end
- function changePage()
- clearPageArea()
- end
- function onClick(x,y,side)
- if is_popup then
- if x == screen_center_x + 12 and y == screen_center_y - 2 then --check for close of popup
- clearPageArea()
- is_popup = false
- elseif y == screen_center_y + 1 then -- Clicked an Arrow
- if x == screen_center_x - 3 then -- Remove One
- items[current_pop].needed = items[current_pop].needed - 1
- showPopup(current_pop)
- elseif x >= screen_center_x - 7 and x <= screen_center_x - 6 then -- Remove 10
- items[current_pop].needed = items[current_pop].needed - 10
- showPopup(current_pop)
- elseif x >= screen_center_x - 11 and x <= screen_center_x - 9 then -- Remove 64
- items[current_pop].needed = items[current_pop].needed - 64
- showPopup(current_pop)
- elseif x == screen_center_x + 2 then -- Add 1
- items[current_pop].needed = items[current_pop].needed + 1
- showPopup(current_pop)
- elseif x >= screen_center_x + 5 and x <= screen_center_x + 6 then -- Add 10
- items[current_pop].needed = items[current_pop].needed + 10
- showPopup(current_pop)
- elseif x >= screen_center_x + 9 and x <= screen_center_x + 11 then -- Add 64
- items[current_pop].needed = items[current_pop].needed + 64
- showPopup(current_pop)
- end
- end
- else
- if y == monitor_height then -- check if bottom row
- if x >= 2 and x <= 5 then --check for Prev
- if c_page > 1 then c_page = c_page - 1 end
- elseif x >= 8 and x <= 11 then --check for Scan
- print("Should Scan")
- scanItems()
- refresh()
- elseif x >= 14 and x <= 17 then --check for Save
- saveItems()
- elseif x >= 20 and x <= 23 then --check for Load
- loadItems()
- elseif x >= 26 and x <= 29 then --check for Next
- if c_page < pages then c_page = c_page + 1 end
- end
- else
- local isRow = (y - header_height) % 2 == 0
- if isRow then
- local row = (y - header_height) / 2
- local col = (math.floor((x - left_padding) / 18) % screen_columns) + 1
- for c_row,cols in pairs(item_pos) do
- for c_col, rawname in pairs(cols) do
- if c_row == row and c_col == col then
- showPopup(rawname)
- end
- end
- end
- end
- end
- end
- end
- function WriteCorners()
- monitor.setCursorPos(1,1)
- monitor.write(" ")
- monitor.setCursorPos(1,monitor_height-1)
- monitor.write(" ")
- monitor.setCursorPos(monitor_width-1,1)
- monitor.write(" ")
- monitor.setCursorPos(monitor_width-1,monitor_height-1)
- monitor.write(" ")
- end
- function showRunning()
- print("Running")
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.red)
- WriteCorners()
- end
- function showWaiting()
- print("Waiting")
- monitor.setBackgroundColor(colors.green)
- monitor.setTextColor(colors.green)
- WriteCorners()
- end
- setupMonitor()
- printHeader()
- printFooter()
- loadItems()
- refresh()
- while true do
- showWaiting()
- os.startTimer(3)
- local event,side,x,y = os.pullEvent()
- showRunning()
- if event == "monitor_touch" then
- onClick(x,y,side)
- else
- getItemCounts()
- checkAllItems()
- end
- if is_popup then
- showPopup(current_pop)
- else
- printPage()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement