Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ocs/apis/sensor")
- gui = peripheral.wrap("right")
- ae = peripheral.wrap("bottom")
- namer = sensor.wrap("left")
- gui.clear()
- gui.addText(20,20,"AE GUI Starting up. Please wait...",1)
- for i,v in pairs(namer.getTargets()) do
- if v.Name == "Ender Chest" then
- chest = i
- end
- end
- function getChest()
- return namer.getTargetDetails(chest)
- end
- function loadDefs()
- ids = {}
- names = {}
- local handle = fs.open("defs","r")
- if not handle then
- handle = fs.open("defs","w")
- handle.write("")
- handle.close()
- handle = fs.open("defs","r")
- end
- local ln = handle.readLine()
- while ln do
- local id,name = ln:match("(%d+)%s(.+)")
- if id and name then
- ids[name] = tonumber(id)
- names[tonumber(id)] = name
- end
- ln = handle.readLine()
- end
- handle.close()
- local items = ae.listItems()
- for i,v in pairs(items) do
- if not names[i] then
- ae.retrieve(i,1,1)
- turtle.drop()
- saveDef(i,getChest().Slots[1].Name)
- turtle.suck()
- turtle.dropUp()
- end
- end
- end
- function saveDef(id,name)
- local handle = fs.open("defs","a")
- handle.writeLine(id.." "..name)
- handle.close()
- ids[name] = id
- names[id] = name
- end
- function loadPrefs()
- local handle = fs.open("prefs","r")
- if not handle then
- handle = fs.open("prefs","w")
- handle.write("")
- handle.close()
- handle = fs.open("prefs","r")
- end
- color = tonumber(handle.readLine() or "1")
- tcolor = tonumber(handle.readLine() or "1")
- rate = tonumber(handle.readLine() or "2")
- stupwatch = handle.readLine() or ""
- opac = tonumber(handle.readLine() or ".3")
- handle.close()
- end
- function savePrefs()
- local handle = fs.open("prefs","w")
- handle.write("")
- handle.close()
- handle = fs.open("prefs","a")
- handle.writeLine(tostring(color))
- handle.writeLine(tostring(tcolor))
- handle.writeLine(tostring(rate))
- stupwatch = ""
- for i,v in ipairs(watchers) do
- stupwatch = stupwatch .. " " .. v.id
- end
- handle.writeLine(stupwatch)
- handle.writeLine(tostring(opac))
- handle.close()
- end
- function ApplyPrefs()
- items = ae.listItems()
- for s in string.gmatch(stupwatch,"%S+") do
- addWatcher(tonumber(s),display(tonumber(s)))
- end
- end
- function createGui()
- gui.clear()
- local stup = gui.addText(20,20,"AE GUI Starting up. Please wait...",tcolor)
- labels = {}
- box = gui.addBox(15,15,0,0,color,opac)
- box.setZIndex(-1)
- maxlen = 0
- watchers = {}
- addLabel("AE Storage:")
- addLabel("")
- stup.delete()
- end
- function addLabel(init)
- local label = gui.addText(20,20+10*(#labels+1),"",tcolor)
- table.insert(labels,label)
- box.setHeight(20+10*(#labels))
- updateText(label,init or "")
- return #label,label
- end
- function deleteLabel(lid)
- local label = table.remove(labels,lid)
- --[[label.delete()
- maxlen = 0
- for i,v in pairs(labels) do
- v.setY(20+10*i)
- updateText(v,v.getText())
- end
- box.setHeight(20+10*#labels)]]
- gui.clear()
- oldwa = watchers
- createGui()
- for i,v in ipairs(oldwa) do
- addWatcher(v.id,display(v.id))
- end
- end
- function updateText(label,text)
- label.setText(text)
- --local len = gui.getStringWidth(text)
- local len = 150
- if len > maxlen then
- maxlen = len
- box.setWidth(20+maxlen)
- end
- end
- function addWatcher(i,init)
- local lid,label = addLabel(init)
- table.insert(watchers,{id=i,lid=lid,label=label})
- end
- function deleteWatcher(i)
- local watcher = table.remove(watchers,i)
- deleteLabel(watcher.lid)
- end
- function getID(str)
- if ids[str] then return ids[str] end
- if tonumber(str) then return tonumber(str) end
- for i,v in pairs(names) do
- if string.match(string.lower(v),"^"..string.lower(str)) then
- return i
- end
- end
- return false
- end
- function error(str)
- print("ERROR: "..str)
- if not errmsg then errmsg = gui.addText(20,10,"ERROR: "..str,16711680) end
- end
- function display(id)
- return names[id].." ("..(items[id] or "0")..")"
- end
- function getCommand()
- while true do
- local evt,cmd = os.pullEvent("chat_command")
- print("GOT " .. cmd)
- local args = {}
- for w in string.gmatch(cmd,"%S+") do
- w = string.gsub(w,"~"," ")
- table.insert(args,w)
- end
- cmd = table.remove(args,1)
- if cmd == "show" then
- if #args == 0 then
- gui.clear()
- createGui()
- for i,v in ipairs(oldw) do
- addWatcher(v.id,display(v.id))
- end
- return
- end
- for i,v in ipairs(args) do
- local id = getID(v)
- if not id then
- error("Item "..v.." not found!")
- else
- addWatcher(id,display(id))
- end
- end
- savePrefs()
- elseif cmd == "hide" then
- if #args == 0 then
- gui.clear()
- oldw = watchers
- watchers = {}
- return
- end
- for i,v in ipairs(args) do
- local id = getID(v)
- local watcher
- if not id then
- error("Item "..v.." not found!")
- else
- for ii,vv in ipairs(watchers) do
- if vv.id == id then
- watcher = ii
- break
- end
- end
- if not watcher then
- error("Item "..v.." not on watcher list!")
- else
- deleteWatcher(watcher)
- end
- end
- end
- savePrefs()
- elseif cmd == "color" then
- if args[1] and tonumber(args[1]) then
- color = tonumber(args[1])
- box.setColor(color)
- savePrefs()
- else
- error("Invalid color value!")
- end
- elseif cmd == "tcolor" then
- if args[1] and tonumber(args[1]) then
- tcolor = tonumber(args[1])
- for i,v in pairs(labels) do
- v.setColor(tcolor)
- end
- savePrefs()
- else
- error("Invalid color value!")
- end
- elseif cmd == "rate" then
- if args[1] and tonumber(args[1]) then
- rate = tonumber(args[1])
- savePrefs()
- else
- error("Invalid rate amount!")
- end
- elseif cmd == "pull" then
- repeat
- turtle.dropUp()
- until not turtle.suck()
- turtle.dropUp()
- elseif cmd == "push" then
- if (#args)%2 == 0 then
- craftable = ae.listCraft()
- for i=1,#args,2 do
- local n = tonumber(args[i])
- local item = getID(args[i+1])
- if item then
- if (not items[item] or items[item] < n) then
- if craftable[item] then
- ae.craft(item,n-(items[item] or 0))
- repeat until items[item] >= n
- else
- error("Item "..names[item].." not in system!")
- return
- end
- end
- ae.retrieve(item,n,1)
- for ii=1,16 do
- if turtle.getItemCount(ii) ~= 0 then
- turtle.select(ii)
- repeat until turtle.drop()
- end
- end
- turtle.select(1)
- else
- error("Item "..args[i+1].." not found!")
- end
- end
- else
- error("Invalid number of arguments!")
- end
- elseif cmd == "clear" then
- gui.clear()
- createGui()
- elseif cmd == "redef" then
- loadDefs()
- elseif cmd == "opac" then
- if args[1] and tonumber(args[1]) then
- opac = tonumber(args[1])
- box.setAlpha(opac)
- savePrefs()
- else
- error("Invalid alpha value!")
- end
- else
- error("Unkown command!")
- end
- end
- end
- function updateGui()
- while true do
- sleep(rate)
- if errmsg then errmsg.delete() errmsg = nil end
- items = ae.listItems()
- for i,v in pairs(watchers) do
- updateText(v.label,display(v.id))
- end
- end
- end
- loadDefs()
- loadPrefs()
- createGui()
- ApplyPrefs()
- while true do
- parallel.waitForAny(getCommand,updateGui)
- end
Advertisement
Add Comment
Please, Sign In to add comment