Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function startup()
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(peripheral.getNames()[k]) == "modem" then
- rednet.open(peripheral.getNames()[k])
- end
- end
- scrollmenuLen={}
- scrollmenuInfos={}
- width, height = term.getSize()
- storageComputerID = 54
- storageFilterReader = BetterRead:new(9,7)
- inventoryFilterReader = BetterRead:new(9,7)
- end
- function main()
- while true do
- info1,info2,info3,info4 = os.pullEvent()
- if info1 == "rednet_message" then
- if type(info3) == "table" then
- if info3["action"] == "sendingStorageContent" and curEnv == storageMenu then
- storageContent = createStorageScrollmenu(info3["storageContent"],"storageContent",storageFilterReader:getOutput())
- printScrollmenu("storageContent")
- end
- if info3["action"] == "sendingPlayerInventory" then
- inventoryContent = info3["inventory"]
- inventoryContent = createInventoryScroll(inventoryContent)
- end
- else
- end
- end
- if curEnv == homeMenu then
- if buttonCheck(3,4,11,6) then
- storageMenu()
- end
- if buttonCheck(13,4,23,6) then
- inventoryMenu()
- end
- end
- if curEnv == storageMenu then
- scrollOut = checkScrollmenu("storageContent")
- if scrollOut and info2 == 1 then
- if type(storageContent[scrollmenuInfos["storageContent"]["content"][scrollOut]]) == "table" then
- isStackable = false
- for k,v in pairs(storageContent[scrollmenuInfos["storageContent"]["content"][scrollOut]]) do
- if v["stackable"] then
- isStackable = true
- break
- end
- end
- if isStackable then
- rednet.send(storageComputerID,{["action"] = "inputToPlayer", ["itemName"] = storageContent[scrollmenuInfos["storageContent"]["content"][scrollOut]]["name"],["amount"] = 64})
- else
- rednet.send(storageComputerID,{["action"] = "inputToPlayer", ["itemName"] = storageContent[scrollmenuInfos["storageContent"]["content"][scrollOut]]["name"],["amount"] = 1})
- end
- end
- end
- if buttonCheck(width-5,4,width,6) then
- homeMenu()
- end
- term.setBackgroundColor(colors.black)
- if storageFilterReader:read() then
- createScrollmenu("storageContent",1,8,width,height,searchItems(storageFilterReader:getOutput(),itemNames,storageMods),0)
- filledSquare(1,8,width,height,colors.black)
- printScrollmenu("storageContent")
- end
- end
- if curEnv == inventoryMenu then
- scrollOut = checkScrollmenu("inventoryContent")
- if scrollOut and info2 == 1 then
- if type(inventoryContent[scrollmenuInfos["inventoryContent"]["content"][scrollOut]]) == "table" then
- rednet.send(storageComputerID,{["action"] = "inputToStorage",["slot"] = inventoryContent[scrollmenuInfos["inventoryContent"]["content"][scrollOut]]["slot"], ["amount"] = inventoryContent[scrollmenuInfos["inventoryContent"]["content"][scrollOut]]["count"], ["itemName"] = inventoryContent[scrollmenuInfos["inventoryContent"]["content"][scrollOut]]["name"]})
- end
- end
- if buttonCheck(width-5,4,width,6) then
- homeMenu()
- end
- term.setBackgroundColor(colors.black)
- if inventoryFilterReader:read() then
- createScrollmenu("inventoryContent",1,8,width,height,searchItems(inventoryFilterReader:getOutput(),itemNames,storageMods),0)
- filledSquare(1,8,width,height,colors.black)
- printScrollmenu("inventoryContent")
- end
- end
- end
- end
- function createInventoryScroll(inventoryContent)
- inventoryItemNames = {}
- for k,v in pairs(inventoryContent) do
- inventoryItemNames[v["name"]] = v
- end
- term.setTextColor(colors.white)
- newInventoryContent = createStorageScrollmenu(inventoryItemNames,"inventoryContent",inventoryFilterReader:getOutput())
- printScrollmenu("inventoryContent")
- for slot, item in pairs(inventoryContent) do
- for k,v in pairs(newInventoryContent) do
- if item["name"] == v["name"] then
- newInventoryContent[k]["slot"] = slot
- end
- end
- end
- inventoryContent = newInventoryContent
- newInventoryContent = nil
- return inventoryContent
- end
- function inventoryMenu()
- curEnv = inventoryMenu
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- inventoryContent = waitForRedMessage({["action"]="sendingPlayerInventory"},storageComputerID,nil,"requestingPlayerInventory")["inventory"]
- term.clear()
- inventoryContent = createInventoryScroll(inventoryContent)
- button("Back",width-5,4,width,6)
- term.setCursorPos(1,7)
- term.write("Search:")
- term.setTextColor(colors.white)
- inventoryFilterReader:startReading()
- headline("Inventory")
- end
- function searchItems(searchText,items,mods)
- local returnTable = {}
- searchText = string.lower(searchText)
- local searchItemNames = {}
- for k,v in pairs(items) do
- searchItemNames[k] = string.lower(items[k])
- end
- local Qpos = string.find(searchText,"@")
- local spacePos = string.find(searchText," ",Qpos)
- if Qpos then
- if spacePos then
- modText = string.sub(searchText, Qpos+1, spacePos-1)
- nameText = string.sub(searchText, 1, Qpos-1) .. string.sub(searchText, spacePos+1, -1)
- else
- modText = string.sub(searchText, Qpos+1, -1)
- nameText = ""
- end
- else
- nameText = searchText
- end
- if modText then
- for k,item in pairs(items) do
- if string.find(searchItemNames[k],nameText) and string.find(mods[item],modText) then
- table.insert(returnTable,item)
- end
- end
- else
- for k,item in pairs(items) do
- if string.find(searchItemNames[k],nameText) then
- table.insert(returnTable,item)
- end
- end
- end
- return returnTable
- end
- function createStorageScrollmenu(storageContent,scrollName,searchText)
- itemNames = {}
- storageMods = {}
- for k,v in pairs(storageContent) do
- table.insert(itemNames,k)
- end
- itemNames = formatItemNames(itemNames)
- local contentNames = {}
- for k,v in pairs(storageContent) do
- table.insert(contentNames,k)
- end
- for k,v in pairs(contentNames) do
- storageContent[itemNames[k]] = storageContent[v]
- storageContent[itemNames[k]]["name"] = v
- storageMods[itemNames[k]] = string.sub(v,1,string.find(v,":")-1)
- storageContent[v] = nil
- end
- table.sort(itemNames)
- searchedItemNames = searchItems(searchText,itemNames,storageMods)
- if scrollmenuInfos[scrollName] then
- createScrollmenu(scrollName,1,8,width,height,searchedItemNames,scrollmenuInfos[scrollName]["position"])
- else
- createScrollmenu(scrollName,1,8,width,height,searchedItemNames,0)
- end
- filledSquare(1,8,width,height,colors.black)
- if scrollmenuInfos[scrollName]["position"] == scrollmenuLen[scrollName]-(scrollmenuInfos[scrollName]["y2"]-scrollmenuInfos[scrollName]["y1"]) and scrollmenuInfos[scrollName]["position"] > 0 then
- scrollmenuInfos[scrollName]["position"] = scrollmenuInfos[scrollName]["position"] -1
- end
- return storageContent
- end
- function homeMenu()
- curEnv = homeMenu
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- headline("Home")
- button("Storage",3,4,11,6)
- button("Inventory",13,4,23,6)
- end
- function storageMenu()
- curEnv = storageMenu
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- storageContent = waitForRedMessage({["action"]="sendingStorageContent"},storageComputerID,nil,"requestingStorageContent")["storageContent"]
- term.clear()
- button("Back",width-5,4,width,6)
- term.setCursorPos(1,7)
- term.write("Search:")
- term.setTextColor(colors.white)
- storageFilterReader:startReading()
- headline("Storage")
- storageContent = createStorageScrollmenu(storageContent,"storageContent",storageFilterReader:getOutput())
- printScrollmenu("storageContent")
- end
- function waitForRedMessage(rednetMessage,ID,waitingscreenMessage,repeatedMessage)
- term.setTextColor(colors.green)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- waitingscreenMessage = waitingscreenMessage or "Waiting to receive a rednet message."
- print(waitingscreenMessage)
- if repeatedMessage then
- rednet.send(ID,repeatedMessage)
- repeatTimer = os.startTimer(0.2)
- end
- while true do
- info1,info2,info3,info4 = os.pullEvent()
- if info1 == "timer" and info2 == repeatTimer then
- rednet.send(ID,repeatedMessage)
- repeatTimer = os.startTimer(0.2)
- end
- if info2 == ID and info1 == "rednet_message" then
- if type(rednetMessage) ~= "table" and type(info3) ~= "table" then
- if info3 == rednetMessage then
- break
- end
- elseif type(rednetMessage) == "table" and type(info3) == "table" then
- local matches = true
- for k,v in pairs(rednetMessage) do
- if info3[k] ~= v then
- matches = false
- end
- end
- if matches then
- break
- end
- end
- end
- end
- return info3, info2
- end
- function formatItemNames(itemNames)
- local returnTable = {}
- for k,v in pairs(itemNames) do
- local curString = string.sub(v, string.find(v,":")+1, -1)
- curString = string.upper(string.sub(curString, 1, 1)) .. string.sub(curString,2)
- local underscorePos = string.find(curString,"_")
- while underscorePos ~= nil do
- curString = string.sub(curString, 1, underscorePos) .. string.upper(string.sub(curString, underscorePos+1, underscorePos+1)) .. string.sub(curString, underscorePos+2, -1)
- underscorePos = string.find(curString,"_", underscorePos+1)
- end
- curString = string.gsub(curString,"_"," ")
- table.insert(returnTable,curString)
- end
- return returnTable
- end
- -- buttons and all that stuff
- function saveTable(folderName,variables)
- local file = fs.open(folderName .. "/variables","w")
- for k,v in pairs(variables) do
- if type(v) ~= "table" then
- file.writeLine(k .. string.sub(type(k),1,1) .. "=" .. string.sub(type(v),1,1) .. v)
- else
- saveTable(folderName .. "/" .. k,v)
- end
- end
- file.flush()
- file.close()
- end
- function readFile(fileName)
- local file = fs.open(fileName,"r")
- local returnTable = {}
- repeat
- local curLine = file.readLine()
- local equalPos = string.find(curLine or "","=")
- if equalPos then
- local indexType = string.sub(curLine,equalPos-1,equalPos-1)
- local valueType = string.sub(curLine,equalPos+1,equalPos+1)
- local index = string.sub(curLine,1,equalPos-2)
- if indexType == "i" then
- index = tonumber(index)
- elseif indexType == "b" then
- if index == "true" then
- index = true
- else
- index = false
- end
- end
- local value = string.sub(curLine,equalPos+2)
- if valueType == "i" then
- value = tonumber(value)
- elseif valueType == "b" then
- if value == "true" then
- value = true
- else
- value = false
- end
- end
- returnTable[index] = value
- end
- until not curLine
- file.close()
- return returnTable
- end
- function readFolder(folderName)
- local returnTable = {}
- for k,v in pairs(fs.list(folderName)) do
- if fs.isDir(folderName .. "/" .. v) then
- returnTable[v] = readFolder(folderName .. "/" .. v)
- else
- returnTable[v] = readFile(folderName .. "/" .. v)
- end
- end
- return returnTable
- end
- BetterRead = {
- new = function(self,xPos,yPos,isBlacklist,filter)
- if type(xPos) ~= "number" then
- error("bad argument #1 (xPos is supposed to be a number)")
- elseif type(yPos) ~= "number" then
- error("bad argument #2 (yPos is supposed to be a number)")
- elseif type(isBlacklist) ~= "boolean" then
- isBlacklist = true
- end
- returnTable = {["xPos"]=xPos,["yPos"]=yPos,["isBlacklist"]=isBlacklist,["filter"]=filter or {},["isReading"]=false,["output"]="",["blinkIsThere"]=false}
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- startReading = function(self)
- self["isReading"] = true
- self["blinkTimer"] = os.startTimer(0.4)
- self["blinkIsThere"] = true
- term.setCursorPos(self["xPos"],self["yPos"])
- term.write(self["output"] .. "_")
- end
- ,
- stopReading = function(self)
- self["isReading"] = false
- if self["blinkIsThere"] then
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write(" ")
- end
- end
- ,
- read = function(self)
- if self["isReading"] then
- if info1 == "key" then
- if info2 == keys.backspace then
- term.setCursorPos(self["xPos"]+string.len(self["output"])- math.min(1,string.len(self["output"])),self["yPos"])
- if self["blinkIsThere"] then
- term.write("_ ")
- else
- term.write(" ")
- end
- self["output"] = string.sub(self["output"],1,string.len(self["output"])-1)
- return true
- end
- if info2 == keys.enter then
- self:stopReading()
- info1 = nil
- end
- end
- if info1 == "timer" and info2 == self["blinkTimer"] then
- self["blinkIsThere"] = not self["blinkIsThere"]
- if self["blinkIsThere"] then
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write("_")
- else
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write(" ")
- end
- self["blinkTimer"] = os.startTimer(0.5)
- end
- if info1 == "char" then
- if not self["isBlacklist"] then
- for k,v in pairs(self["filter"]) do
- if info2 == v then
- self["output"] = self["output"] .. info2
- term.setCursorPos(self["xPos"],self["yPos"])
- if self["blinkIsThere"] then
- term.write(self["output"] .. "_")
- else
- term.write(self["output"])
- end
- end
- end
- return true
- else
- for k,v in pairs(self["filter"]) do
- if info2 == v then
- return
- end
- end
- self["output"] = self["output"] .. info2
- term.setCursorPos(self["xPos"],self["yPos"])
- if self["blinkIsThere"] then
- term.write(self["output"] .. "_")
- else
- term.write(self["output"])
- end
- return true
- end
- end
- else
- if info1 == "mouse_click" and info4 == self["yPos"] then
- self:startReading()
- end
- end
- end
- ,
- getOutput = function(self)
- return self["output"]
- end
- }
- function resetEvent()
- info1 = nil
- end
- function createScrollmenu(name,x1,y1,x2,y2,content,position)
- scrollmenuLen[name]=0
- scrollmenuInfos[name]={["x1"]=x1,["y1"]=y1,["x2"]=x2,["y2"]=y2,["content"]=content,["position"]=position,["previewText"]={}}
- for k,v in pairs(content) do
- scrollmenuLen[name]=scrollmenuLen[name]+1
- end
- for k,v in pairs(content) do
- if string.len(v) > x2 - x1 + 1 then
- scrollmenuInfos[name]["previewText"][k] = string.sub(v,1,x2-x1-2) .. "..."
- else
- scrollmenuInfos[name]["previewText"][k] = v
- end
- end
- end
- function printScrollmenu(name)
- for i=1,math.min(scrollmenuLen[name],scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1) do
- if type(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]]) == "string" then
- if (i+scrollmenuInfos[name]["position"])%2==0 then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.black)
- end
- term.setCursorPos(scrollmenuInfos[name]["x1"],scrollmenuInfos[name]["y1"]+i-1)
- term.write(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]])
- for i2=1,scrollmenuInfos[name]["x2"]-scrollmenuInfos[name]["x1"]+1-string.len(scrollmenuInfos[name]["content"][i+scrollmenuInfos[name]["position"]]) do
- term.write(" ")
- end
- end
- end
- end
- function checkScrollmenu(name)
- if info1 == "mouse_click" and info3 > scrollmenuInfos[name]["x1"]-1 and info3 < scrollmenuInfos[name]["x2"]+1 and info4 > scrollmenuInfos[name]["y1"]-1 and info4 < scrollmenuInfos[name]["y2"]+1 then
- resetEvent()
- return info4-scrollmenuInfos[name]["y1"]+1+scrollmenuInfos[name]["position"]
- end
- if info1 == "mouse_scroll" and not (scrollmenuInfos[name]["position"]+info2 > scrollmenuLen[name]-(scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1)) and not (scrollmenuInfos[name]["position"]+info2 < 0) then
- scrollmenuInfos[name]["position"] = scrollmenuInfos[name]["position"] + info2
- printScrollmenu(name)
- end
- end
- function headline(headline)
- width = term.getSize()
- headline_lenght=string.len(" "..headline.." ")
- headline_pos=(width/2)-(headline_lenght/2)+1
- term.setCursorPos(headline_pos,1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.blue)
- term.write(" "..headline.." ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
- term.setTextColor(colors.green)
- if button_color~=nil then
- term.setTextColor(button_color)
- end
- square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
- term.setCursorPos(math.ceil(button_pos_x1-string.len(button_text)/2+(button_pos_x2-button_pos_x1)/2),button_pos_y1+(button_pos_y2-button_pos_y1)/2)
- term.write(button_text)
- end
- function buttonCheck(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
- if info1=="mouse_click" and info3 > b_check_x1-1 and info3 < b_check_x2+1 and info4 > b_check_y1-1 and info4 < b_check_y2+1 then
- resetEvent()
- return true
- else
- return false
- end
- end
- function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
- x_1=x1
- y_1=y1
- x_2=x2
- y_2=y2
- while x_1~=x2+1 do
- term.setBackgroundColor(back_color_square)
- term.setCursorPos(x_1,y_1)
- if text_color~=nil then
- term.setTextColor(text_color)
- end
- term.write(symbol_square_x)
- x_1=x_1+1
- end
- while y_1~=y2-1 do
- term.setCursorPos(x_1-1,y_1+1)
- term.write(symbol_square_y)
- y_1=y_1+1
- end
- x2=x2-1
- while x_2~=x1-1 do
- term.setCursorPos(x_2,y_2)
- term.write(symbol_square_x)
- x_2=x_2-1
- end
- while y_2~=y1+1 do
- term.setCursorPos(x_2+1,y_2-1)
- term.write(symbol_square_y)
- y_2=y_2-1
- end
- term.setBackgroundColor(colors.black)
- end
- DropdownMenu = {
- new = function(self,x1,y,x2,content)
- if type(x1) ~= "number" then
- error("bad argument #1 (x1 is supposed to be a number)")
- elseif type(y) ~= "number" then
- error("bad argument #2 (y is supposed to be a number)")
- elseif type(x2) ~= "number" then
- error("bad argument #3 (x2 is supposed to be a number)")
- end
- local width,hight = term.getSize()
- returnTable = {["x1"]=x1,["y"]=y,["x2"]=x2,["content"]=content or {},["position"]=0,["isOpen"]=false,["maxLen"] = hight - y}
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- print = function(self)
- local lastTextColor = term.getTextColor()
- local lastBackColor = term.getBackgroundColor()
- if self["isOpen"] then
- for i = 1, math.min(self["maxLen"],table.maxn(self["content"])) do
- if i%2 == 0 then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(self["x1"],self["y"]+i-1)
- if string.len(self["content"][i+self["position"]]) > self["x2"] - self["x1"] + 1 then
- term.write(string.sub(self["content"][i+self["position"]],1,self["x2"] - self["x1"] - 2) .. "...")
- else
- term.write(self["content"][i+self["position"]])
- for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][i+self["position"]]) do
- term.write(" ")
- end
- end
- end
- if math.min(self["maxLen"],table.maxn(self["content"])) < table.maxn(self["content"]) - self["position"] then
- term.setCursorPos(self["x1"],self["y"]+math.min(self["maxLen"],table.maxn(self["content"]))-1)
- term.write("+")
- for i = 1, self["x2"] - self["x1"] do
- term.write(" ")
- end
- end
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(self["x1"],self["y"])
- if string.len(self["content"][1]) > self["x2"] - self["x1"] + 1 then
- term.write(string.sub(self["content"][1],1,self["x2"] - self["x1"] - 2) .. "...")
- else
- term.write(self["content"][1])
- for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][1]) do
- term.write(" ")
- end
- end
- end
- term.setTextColor(lastTextColor)
- term.setBackgroundColor(lastBackColor)
- end
- ,
- check = function(self)
- if info1 == "mouse_click" then
- if info3 >= self["x1"] and info3 <= self["x2"] and info4 == self["y"] and self["position"] == 0 then -- if first element is clicked
- if self["isOpen"] then
- self:close()
- else
- self:open()
- end
- elseif info3 >= self["x1"] and info3 <= self["x2"] and info4 > self["y"]-1 and info4 < self["y"] + self["maxLen"] and self["isOpen"] then -- if is open and an element thats not the first one is clicked
- local clickedPos = info4 - self["y"] + 1
- if clickedPos == self["maxLen"] and self["maxLen"] + self["position"] < table.maxn(self["content"]) then -- checks if "+" is clicked
- self["position"] = self["position"] + 1
- self:print()
- else -- changes the first element to the clicked one
- local contentSave = self["content"][1]
- self["content"][1] = self["content"][clickedPos + self["position"]]
- self["content"][clickedPos + self["position"]] = contentSave
- self:close()
- end
- info1 = nil
- end
- end
- if info1 == "mouse_scroll" and self["isOpen"] then -- checks for scrolling
- if self["position"] + info2 + self["maxLen"] <= table.maxn(self["content"]) and self["position"] + info2 > -1 then
- self["position"] = self["position"] + info2
- curEnv()
- end
- end
- end
- ,
- close = function(self)
- self["isOpen"] = false
- self["position"] = 0
- curEnv()
- end
- ,
- open = function(self)
- self["isOpen"] = true
- self["position"] = 0
- curEnv()
- end
- ,
- getOutput = function(self,index)
- return self["content"][index or 1]
- end
- ,
- changePos = function(self,x1,y,x2)
- self["x1"],self["y"],self["x2"] = x1,y,x2
- end
- }
- function filledSquare(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
- term.setBackgroundColor(fill_color)
- curr_fill_pos_x=fill_x1
- curr_fill_pos_y=fill_y1
- while curr_fill_pos_y~=fill_y2+1 do
- while curr_fill_pos_x~=fill_x2+1 do
- term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
- term.write(" ")
- curr_fill_pos_x=curr_fill_pos_x+1
- end
- curr_fill_pos_x=fill_x1
- curr_fill_pos_y=curr_fill_pos_y+1
- end
- term.setBackgroundColor(colors.black)
- end
- function lineCheck(y)
- if info1 == "mouse_click" and info4 == y then
- return true
- else
- return false
- end
- end
- -------------------------------------------------
- startup()
- homeMenu()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement