Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local targetChannel = 666
- if fs.exists("event")==false then shell.run("pastebin get UKPy4iiE event") end
- if os.loadAPI("event") == false then error("Failed to load event API") end
- if fs.exists("utils") == false then shell.run("pastebin get dyvydHtK utils") end
- if os.loadAPI("utils") == false then error("Failed to load utils API") end
- if fs.exists("data") == false then shell.run("pastebin get LnvzL7ur data") end
- if os.loadAPI("data") == false then error("Failed to load data API") end
- if fs.exists("button") == false then shell.run("pastebin get UmerzDUF button") end
- if os.loadAPI("button") == false then error("Failed to load button API") end
- button.setMonitor(term)
- --button.setTextColor(colors.green)
- if data.get("channel","channel") == nil then
- io.write("Channel number: ")
- data.set("channel",read(),"channel")
- end
- local localChannel = tonumber(data.get("channel","channel"))
- local previous = os.clock()
- local modem = peripheral.wrap("back")
- local ping
- local itemlist = {}
- local recipes = {}
- local cachedItems = {}
- local cachedRecipes = {}
- local searchScroll = 0
- local searchQuery = ""
- local requestCount = ""
- local requestItemString = ""
- local requestCountString = ""
- local craftRecipeString = ""
- local recipeSearchString = ""
- local itemCount
- local turtles = {}
- local que = {}
- local currentRecipe = {}
- modem.open(localChannel)
- local screens = {
- home = {},
- search = {},
- getCount = {},
- request = {},
- turtles = {},
- que = {},
- requestItem = {},
- craft = {},
- recipes = {},
- }
- screens.update = button.addButton(1,20,26,1,"Update",false,false)
- screens.homeButton = button.addButton(1,2,26,1,"Home",false,false)
- screens.home.search = button.addButton(2,5,11,1,"Search",false,false)
- screens.home.getCount = button.addButton(15,5,11,1,"Get Count",false,false)
- screens.home.request = button.addButton(2,7,11,1,"Request",false,false)
- screens.home.turtles = button.addButton(15,7,11,1,"Turtles",false,false)
- screens.home.que = button.addButton(2,9,11,1,"View Que",false,false)
- screens.home.ping = button.addButton(15,9,11,1,"Ping",false,false)
- screens.home.craft = button.addButton(2,11,11,1,"Craft",false,false)
- screens.home.addRecipe = button.addButton(15,11,11,1,"New recipes",false,false)
- screens.recipes.newRecipe = button.addButton(4,18,21,1,"Add recipe",false,false)
- screens.request.request = button.addButton(2,10,24,1,"request",false,false)
- screens.requestItem.request = button.addButton(2,10,24,1,"request",false,false)
- screens.getCount.request = button.addButton(2,10,24,1,"request",false,false)
- local currentScreen = screens.home
- term.setCursorPos(2,10)
- term.setBackgroundColor(colors.black)
- function drawReadBox(title)
- term.setBackgroundColor(colors.green)
- term.setCursorPos(1,11)
- term.write(title.." ")
- term.setCursorPos(1,12)
- term.write(" ")
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setBackgroundColor(colors.green)
- term.write(" ")
- term.setBackgroundColor(colors.green)
- term.setCursorPos(1,13)
- term.write(" ")
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(2,12)
- end
- local buttonFunctions = {
- [screens.homeButton] = function()
- currentScreen = screens.home
- end,
- [screens.update] = function()
- error(shell.run("update"))
- end,
- [screens.home.ping] = function()
- previous = os.clock()
- modem.transmit(targetChannel,localChannel,"ping")
- end,
- [screens.home.search] = function()
- currentScreen = screens.search
- modem.transmit(targetChannel,localChannel,"requestItemList")
- searchQuery = ""
- end,
- [screens.home.request] = function()
- requestItemString = ""
- currentScreen = screens.request
- end,
- [screens.home.getCount] = function()
- requestCountString = ""
- currentScreen = screens.getCount
- end,
- [screens.home.turtles] = function()
- currentScreen = screens.turtles
- modem.transmit(targetChannel,localChannel,"requestTurtles")
- local tEvent,side,channel,replyChannel,message = os.pullEvent("modem_message")
- event.trigger(tEvent,side,channel,replyChannel,message)
- end,
- [screens.home.que] = function()
- currentScreen = screens.que
- modem.transmit(targetChannel,localChannel,"requestQue")
- local tEvent,side,channel,replyChannel,message = os.pullEvent("modem_message")
- event.trigger(tEvent,side,channel,replyChannel,message)
- end,
- [screens.home.craft] = function()
- currentScreen = screens.craft
- craftRecipeString=""
- searchScroll = 10000
- modem.transmit(targetChannel,localChannel,"requestRecipes")
- local tEvent,side,channel,replyChannel,message = os.pullEvent("modem_message")
- event.trigger(tEvent,side,channel,replyChannel,message)
- end,
- [screens.home.addRecipe] = function()
- currentScreen = screens.recipes
- currentRecipe = { }
- end,
- [screens.request.request] = function()
- requestCount = ""
- currentScreen = screens.requestItem
- end,
- [screens.requestItem.request] = function()
- local count = tonumber(requestCount)
- if type(count)~= "number" or count == 0 then
- return
- end
- local message = "requestItem|"..tostring(requestItemString).."|"..tostring(count)
- currentScreen = screens.home
- modem.transmit(targetChannel,localChannel,message)
- end,
- [screens.getCount.request] = function()
- local message = "getItemCount|"..tostring(requestCountString)
- modem.transmit(targetChannel,localChannel,message)
- local tEvent,side,channel,replyChannel,message = os.pullEvent("modem_message")
- event.trigger(tEvent,side,channel,replyChannel,message)
- requestCountString = ""
- onChar("")
- end,
- [screens.recipes.newRecipe] = function()
- drawReadBox("Recipe name:")
- local recipeString = read() .. "|"
- for i=1,9 do
- recipeString = recipeString .. ( currentRecipe[i] or "" ).. ";"
- end
- -- currentRecipe
- currentScreen = screens.home
- modem.transmit(targetChannel,localChannel,"addRecipe|"..recipeString)
- end,
- }
- function handleClicks(buttonID)
- if buttonFunctions[buttonID] == nil then return end
- buttonFunctions[buttonID]()
- end
- event.addHandler("onButtonClick",handleClicks)
- function onChar(letter)
- if string.len(letter)>0 then
- local letter = string.lower(letter)
- end
- if currentScreen == screens.search then
- searchQuery = searchQuery..letter
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- if string.len(searchQuery)>= 23 then
- term.write(string.sub(searchQuery,string.len(searchQuery)-22,string.len(searchQuery)))
- else
- term.write(searchQuery)
- for i = string.len(searchQuery),23 do
- term.write(" ")
- end
- end
- cachedItems = {}
- for name,count in pairs(itemlist) do
- if string.len(searchQuery) == 0 or string.match(name,searchQuery)~= nil then
- cachedItems[#cachedItems+1] = {name = name,count = count}
- end
- end
- searchScroll = 0
- drawSearch(true)
- elseif currentScreen == screens.requestItem then
- requestCount = requestCount..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestCount)>= 4 then
- term.write(string.sub(requestCount,string.len(requestCount)-4,string.len(requestCount)))
- else
- term.write(requestCount)
- for i = string.len(requestCount),4 do
- term.write(" ")
- end
- end
- elseif currentScreen == screens.request then
- requestItemString = requestItemString..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestItemString)>= 23 then
- term.write(string.sub(requestItemString,string.len(requestItemString)-22,string.len(requestItemString)))
- else
- term.write(requestItemString)
- for i = string.len(requestItemString),23 do
- term.write(" ")
- end
- end
- elseif currentScreen == screens.getCount then
- requestCountString = requestCountString..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- if string.len(requestCountString)>= 23 then
- term.write(string.sub(requestCountString,string.len(requestCountString)-22,string.len(requestCountString)))
- else
- term.write(requestCountString)
- for i = string.len(requestCountString),23 do
- term.write(" ")
- end
- end
- elseif currentScreen == screens.craft then
- searchScroll = 0
- recipeSearchString = recipeSearchString..letter
- term.setCursorPos(2,6)
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.white)
- if string.len(recipeSearchString)>= 23 then
- term.write(string.sub(recipeSearchString,string.len(recipeSearchString)-22,string.len(recipeSearchString)))
- else
- term.write(recipeSearchString)
- for i = string.len(recipeSearchString),23 do
- term.write(" ")
- end
- end
- cachedRecipes = {}
- for _,name in pairs(recipes) do
- if string.len(recipeSearchString) == 0 or string.match(name,recipeSearchString)~= nil then
- cachedRecipes[#cachedRecipes+1] = name
- end
- end
- drawCraft(true)
- end
- end
- event.addHandler("char",onChar)
- function onKey(key)
- if currentScreen == screens.search then
- if key == 14 then -- backspace
- searchQuery = string.sub(searchQuery,1,string.len(searchQuery)-1)
- onChar("")
- elseif key == 28 then
- cachedItems = {}
- for name,count in pairs(itemlist) do
- if string.len(searchQuery) == 0 or string.match(name,searchQuery)~= nil then
- cachedItems[#cachedItems+1] = {name = name,count = count}
- end
- end
- searchScroll = 0
- drawSearch(true)
- end
- elseif currentScreen == screens.requestItem then
- if key == 14 then -- backspace
- requestCount = string.sub(requestCount,1,string.len(requestCount)-1)
- onChar("")
- elseif key == 28 then
- buttonFunctions[screens.requestItem.request]()
- button.drawAllButtons()
- end
- elseif currentScreen == screens.request then
- if key == 14 then -- backspace
- requestItemString = string.sub(requestItemString,1,string.len(requestItemString)-1)
- onChar("")
- elseif key == 28 then
- buttonFunctions[screens.request.request]()
- button.drawAllButtons()
- end
- elseif currentScreen == screens.getCount then
- if key == 14 then -- backspace
- requestCountString = string.sub(requestCountString,1,string.len(requestCountString)-1)
- onChar("")
- elseif key == 28 then
- buttonFunctions[screens.getCount.request]()
- button.drawAllButtons()
- end
- elseif currentScreen == screens.craft then
- if key == 14 then -- backspace
- recipeSearchString = string.sub(recipeSearchString,1,string.len(recipeSearchString)-1)
- onChar("")
- elseif key == 28 then
- buttonFunctions[screens.craft.craft]()
- button.drawAllButtons()
- end
- end
- end
- event.addHandler("key",onKey)
- local drawFunctions = {
- [screens.search] = function (fromChar)
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Search:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(searchQuery)
- for i=1,24 - string.len(searchQuery) do
- term.write(" ")
- end
- --term.setBackgroundColor(colors.white)
- --term.write(" ")
- term.setCursorPos(2,6)
- if fromChar == nil then
- --onChar("")
- end
- term.setTextColor(colors.yellow)
- for i = 1,10 do
- local id = i+searchScroll
- local item = cachedItems[id]
- if item == nil then
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,8+i)
- term.write(" ")
- else
- term.setCursorPos(1,8+i)
- if math.floor(i/2) == math.ceil(i/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- local itemString = tostring(item.name).." x "..tostring(item.count)
- term.write(itemString)
- for i = string.len(itemString),25 do
- term.write(" ")
- end
- end
- end
- end,
- [screens.requestItem] = function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Count:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- [screens.request] = function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Name:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- [screens.turtles] = function()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,4)
- term.write("Turtles:")
- for id,turtle in pairs(turtles) do
- term.setCursorPos(1,5+id*2)
- if math.floor(id/2) == math.ceil(id/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- term.setTextColor(colors.yellow)
- term.write("id: "..turtle.id.." Status: "..turtle.status)
- if turtle.task~= "nil" then
- term.setCursorPos(1,5+id*2+1)
- term.write(turtle.task)
- end
- end
- end,
- [screens.que] = function()
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,4)
- term.write("Que:")
- for id,command in pairs(que) do
- term.setCursorPos(1,5+id)
- if math.floor(id/2) == math.ceil(id/2) then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- term.setTextColor(colors.yellow)
- if command~= nil then
- term.write(command)
- end
- end
- end,
- [screens.getCount] = function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Name:")
- term.setCursorPos(2,8)
- term.write("Result: "..tostring(itemCount))
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setCursorPos(2,6)
- onChar("")
- end,
- [screens.craft] = function()
- term.setCursorPos(2,5)
- term.setBackgroundColor(colors.black)
- term.write("Search recipes:")
- term.setCursorPos(2,6)
- term.setBackgroundColor(colors.white)
- term.write(recipeSearchString)
- for i=1,24 - string.len(recipeSearchString) do
- term.write(" ")
- end
- term.setCursorPos(2,6)
- term.setTextColor(colors.yellow)
- for i = 1,10 do
- local id = i+searchScroll
- local item = cachedRecipes[id]
- if item == nil then
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,8+i)
- term.write(" ")
- else
- term.setCursorPos(1,8+i)
- if i%2==1 then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.lightGray)
- end
- local itemString = tostring(item)
- term.write(itemString)
- for i = string.len(itemString),25 do
- term.write(" ")
- end
- end
- end
- end,
- [screens.recipes] = function()
- term.setCursorPos(2,4)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.write("Create recipe:")
- for i = 0,8 do
- if i%2==1 then
- term.setBackgroundColor(colors.lightGray)
- else
- term.setBackgroundColor(colors.gray)
- end
- local left = 5 + (i * 6) - ( ( math.floor(i/3) * 3 ) * 6)
- local top = 7 + ( math.floor(i/3) * 3)
- term.setCursorPos(left,top)
- term.write(" ")
- term.setCursorPos(left,top+1)
- term.write(" ")
- term.setCursorPos(left,top+1)
- if currentRecipe[i+1]~=nil then
- term.write(string.sub(currentRecipe[i+1],0,6))
- end
- term.setCursorPos(left,top+2)
- term.write(" ")
- end
- end,
- }
- function recipeClicked(button,x,y)
- if currentScreen == screens.recipes and button == 1 then
- local column = math.floor((x - 5) / 6 + 1 )
- if column < 1 or column > 3 then return end
- local row = math.floor((y - 7) / 3 + 1)
- if row < 1 or row > 3 then return end
- local position = math.floor(column + ( row - 1 ) * 3)
- drawReadBox("Item name (slot "..position..") ")
- local name = read()
- currentRecipe[position] = name
- term.setBackgroundColor(colors.green)
- term.clear()
- drawFunctions[currentScreen]()
- drawOthers()
- end
- end
- event.addHandler("mouse_click",recipeClicked)
- function onClick(mouseButton,x,y)
- if currentScreen == screens.search then
- if y>8 and y<20 then
- local item = cachedItems[y-8+searchScroll]
- if item == nil then return end
- requestItemString = item.name
- requestCount = ""
- currentScreen = screens.requestItem
- button.drawAllButtons()
- end
- elseif currentScreen == screens.craft then
- if y>8 and y<20 then
- local item = cachedRecipes[y-8+searchScroll]
- if item == nil then return end
- drawReadBox("Procedure count: ")
- local count = tonumber(read())
- if count==nil then return end
- local message = "requestCraft|"..item.."|"..count
- currentScreen = screens.home
- button.drawAllButtons()
- modem.transmit(targetChannel,localChannel,message)
- end
- end
- end
- event.addHandler("mouse_click",onClick)
- local messageHandles = {
- ["ping"] = function(replyChannel,message)
- return "pong"
- end,
- ["pong"] = drawPing,
- ["returnItemList"] = function(replyChannel,message)
- itemlist = {}
- local items = utils.split(message,"|",2)
- local i = 1
- while true do
- local item = utils.split(items,";",i)
- if string.len(item) == 0 then
- break
- end
- local name = utils.split(item,",",1)
- local count = tonumber(utils.split(item,",",2))
- if name == nil or count == nil then
- break
- end
- itemlist[name] = count
- i = i+1
- end
- end,
- ["returnRecipes"] = function(replyChannel,message)
- recipes = {}
- local recipesString = utils.split(message,"|",2)
- local i = 1
- while true do
- local recipe = utils.split(recipesString,";",i)
- print(recipe)
- if string.len(recipe) == 0 then
- break
- end
- recipes[#recipes+1] = recipe
- i = i+1
- end
- end,
- ["returnQue"] = function(_,message)
- que = {}
- local i = 1
- while true do
- local command = utils.split(message,"|",i+1)
- if command == nil then
- break
- end
- que[#que+1] = command
- i = i+1
- end
- drawFunctions[currentScreen](false)
- end,
- ["returnTurtles"] = function(_,message)
- turtles = {}
- local turtlesString = utils.split(message,"|",2)
- local i = 1
- while true do
- local turtle = utils.split(turtlesString,";",i)
- if turtle == nil or string.len(turtle) == 0 then
- break
- end
- local id = tostring(utils.split(turtle,",",1))
- local status = tostring(utils.split(turtle,",",2))
- local task = tostring(utils.split(turtle,",",3))
- if id == nil or status == nil then
- break
- end
- turtles[#turtles+1] = {id = id,status = status,task = task}
- i = i+1
- end
- drawFunctions[currentScreen](false)
- end,
- ["returnItemCount"] = function(_,message)
- itemCount = utils.split(message,"|",2)
- drawFunctions[currentScreen](false)
- end,
- }
- function handleMessages(side,channel,replyChannel,message)
- if replyChannel == nil then return end
- local messageType = utils.split(message,"|",1)
- if messageHandles[messageType] == nil then return end
- local returnMessage = messageHandles[messageType](replyChannel,message)
- if returnMessage == nil then return end
- wireless.transmit(replyChannel,localChannel,returnMessage)
- end
- event.addHandler("modem_message",handleMessages)
- function drawSearch(...)
- drawFunctions[screens.search](...)
- end
- function drawCraft(...)
- drawFunctions[screens.craft](...)
- end
- function drawPing()
- if previous~= nil then
- ping = os.clock()-previous
- previous = nil
- end
- term.setCursorPos(1,1)
- term.setBackgroundColor(256)
- --term.setTextColor(colors.green)
- term.write(ping.." ")
- end
- function drawOthers()
- for screenName,buttons in pairs(screens) do
- if type(buttons)~= "number" then
- for name,buttonID in pairs(buttons) do
- if buttons == currentScreen then
- button.setVisible(buttonID,true)
- else
- button.setVisible(buttonID,false)
- end
- end
- end
- end
- button.drawAllButtons(true)
- drawPing()
- if drawFunctions[currentScreen] == nil then return end
- drawFunctions[currentScreen]()
- end
- event.addHandler("onButtonsDrawn",drawOthers)
- drawOthers()
- function scrolled(direction)
- if currentScreen == screens.search then
- searchScroll = searchScroll+direction
- drawSearch()
- elseif currentScreen == screens.craft then
- searchScroll = searchScroll+direction
- drawFunctions[screens.craft]()
- end
- end
- event.addHandler("mouse_scroll",scrolled)
- while true do
- event.handleCCEvents()
- end
Add Comment
Please, Sign In to add comment