iconmaster

aegui

Jun 13th, 2013
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.16 KB | None | 0 0
  1.  
  2. os.loadAPI("ocs/apis/sensor")
  3.  
  4. gui = peripheral.wrap("right")
  5. ae = peripheral.wrap("bottom")
  6. namer = sensor.wrap("left")
  7.  
  8. gui.clear()
  9. gui.addText(20,20,"AE GUI Starting up. Please wait...",1)
  10.  
  11. for i,v in pairs(namer.getTargets()) do
  12.     if v.Name == "Ender Chest" then
  13.         chest = i
  14.     end
  15. end
  16.  
  17. function getChest()
  18.     return namer.getTargetDetails(chest)
  19. end
  20.  
  21. function loadDefs()
  22.     ids = {}
  23.     names = {}
  24.  
  25.     local handle = fs.open("defs","r")
  26.     if not handle then
  27.         handle = fs.open("defs","w")
  28.         handle.write("")
  29.         handle.close()
  30.         handle = fs.open("defs","r")
  31.     end
  32.     local ln = handle.readLine()
  33.     while ln do
  34.         local id,name = ln:match("(%d+)%s(.+)")
  35.         if id and name then
  36.             ids[name] = tonumber(id)
  37.             names[tonumber(id)] = name
  38.         end
  39.         ln = handle.readLine()
  40.     end
  41.     handle.close()
  42.  
  43.     local items = ae.listItems()
  44.     for i,v in pairs(items) do
  45.         if not names[i] then
  46.             ae.retrieve(i,1,1)
  47.             turtle.drop()
  48.             saveDef(i,getChest().Slots[1].Name)
  49.             turtle.suck()
  50.             turtle.dropUp()
  51.         end
  52.     end
  53. end
  54.  
  55. function saveDef(id,name)
  56.     local handle = fs.open("defs","a")
  57.     handle.writeLine(id.." "..name)
  58.     handle.close()
  59.     ids[name] = id
  60.     names[id] = name
  61. end
  62.  
  63. function loadPrefs()
  64.     local handle = fs.open("prefs","r")
  65.     if not handle then
  66.         handle = fs.open("prefs","w")
  67.         handle.write("")
  68.         handle.close()
  69.         handle = fs.open("prefs","r")
  70.     end
  71.     color = tonumber(handle.readLine() or "1")
  72.     tcolor = tonumber(handle.readLine() or "1")
  73.     rate = tonumber(handle.readLine() or "2")
  74.     stupwatch = handle.readLine() or ""
  75.     opac = tonumber(handle.readLine() or ".3")
  76.     handle.close()
  77. end
  78.  
  79. function savePrefs()
  80.     local handle = fs.open("prefs","w")
  81.     handle.write("")
  82.     handle.close()
  83.     handle = fs.open("prefs","a")
  84.     handle.writeLine(tostring(color))
  85.     handle.writeLine(tostring(tcolor))
  86.     handle.writeLine(tostring(rate))
  87.     stupwatch = ""
  88.     for i,v in ipairs(watchers) do
  89.         stupwatch = stupwatch .. " " .. v.id
  90.     end
  91.     handle.writeLine(stupwatch)
  92.     handle.writeLine(tostring(opac))
  93.     handle.close()
  94. end
  95.  
  96. function ApplyPrefs()
  97.     items = ae.listItems()
  98.     for s in string.gmatch(stupwatch,"%S+") do
  99.         addWatcher(tonumber(s),display(tonumber(s)))
  100.     end
  101. end
  102.  
  103. function createGui()
  104.     gui.clear()
  105.     local stup = gui.addText(20,20,"AE GUI Starting up. Please wait...",tcolor)
  106.     labels = {}
  107.     box = gui.addBox(15,15,0,0,color,opac)
  108.     box.setZIndex(-1)
  109.     maxlen = 0
  110.     watchers = {}
  111.     addLabel("AE Storage:")
  112.     addLabel("")
  113.     stup.delete()
  114. end
  115.  
  116. function addLabel(init)
  117.     local label = gui.addText(20,20+10*(#labels+1),"",tcolor)
  118.     table.insert(labels,label)
  119.     box.setHeight(20+10*(#labels))
  120.     updateText(label,init or "")
  121.     return #label,label
  122. end
  123.  
  124. function deleteLabel(lid)
  125.     local label = table.remove(labels,lid)
  126.     --[[label.delete()
  127.     maxlen = 0
  128.     for i,v in pairs(labels) do
  129.         v.setY(20+10*i)
  130.         updateText(v,v.getText())
  131.     end
  132.     box.setHeight(20+10*#labels)]]
  133.  
  134.     gui.clear()
  135.     oldwa = watchers
  136.     createGui()
  137.     for i,v in ipairs(oldwa) do
  138.         addWatcher(v.id,display(v.id))
  139.     end
  140. end
  141.  
  142. function updateText(label,text)
  143.     label.setText(text)
  144.     --local len = gui.getStringWidth(text)
  145.     local len = 150
  146.     if len > maxlen then
  147.         maxlen = len
  148.         box.setWidth(20+maxlen)
  149.     end
  150. end
  151.  
  152. function addWatcher(i,init)
  153.     local lid,label = addLabel(init)
  154.     table.insert(watchers,{id=i,lid=lid,label=label})
  155. end
  156.  
  157. function deleteWatcher(i)
  158.     local watcher = table.remove(watchers,i)
  159.     deleteLabel(watcher.lid)
  160. end
  161.  
  162. function getID(str)
  163.     if ids[str] then return ids[str] end
  164.     if tonumber(str) then return tonumber(str) end
  165.     for i,v in pairs(names) do
  166.         if string.match(string.lower(v),"^"..string.lower(str)) then
  167.             return i
  168.         end
  169.     end
  170.     return false
  171. end
  172.  
  173. function error(str)
  174.     print("ERROR: "..str)
  175.     if not errmsg then errmsg = gui.addText(20,10,"ERROR: "..str,16711680) end
  176. end
  177.  
  178. function display(id)
  179.     return names[id].."  ("..(items[id] or "0")..")"
  180. end
  181.  
  182. function getCommand()
  183.     while true do
  184.         local evt,cmd = os.pullEvent("chat_command")
  185.         print("GOT " .. cmd)
  186.         local args = {}
  187.         for w in string.gmatch(cmd,"%S+") do
  188.             w = string.gsub(w,"~"," ")
  189.             table.insert(args,w)
  190.         end
  191.         cmd = table.remove(args,1)
  192.         if cmd == "show" then
  193.             if #args == 0 then
  194.                 gui.clear()
  195.                 createGui()
  196.                 for i,v in ipairs(oldw) do
  197.                     addWatcher(v.id,display(v.id))
  198.                 end
  199.                 return
  200.             end
  201.             for i,v in ipairs(args) do
  202.                 local id = getID(v)
  203.                 if not id then
  204.                     error("Item "..v.." not found!")
  205.                 else
  206.                     addWatcher(id,display(id))
  207.                 end
  208.             end
  209.             savePrefs()
  210.         elseif cmd == "hide" then
  211.             if #args == 0 then
  212.                 gui.clear()
  213.                 oldw = watchers
  214.                 watchers = {}
  215.                 return
  216.             end
  217.             for i,v in ipairs(args) do
  218.                 local id = getID(v)
  219.                 local watcher
  220.                 if not id then
  221.                     error("Item "..v.." not found!")
  222.                 else
  223.                     for ii,vv in ipairs(watchers) do
  224.                         if vv.id == id then
  225.                             watcher = ii
  226.                             break
  227.                         end
  228.                     end
  229.                 if not watcher then
  230.                     error("Item "..v.." not on watcher list!")
  231.                 else
  232.                     deleteWatcher(watcher)
  233.                 end
  234.                 end
  235.  
  236.             end
  237.             savePrefs()
  238.         elseif cmd == "color" then
  239.             if args[1] and tonumber(args[1]) then
  240.                 color = tonumber(args[1])
  241.                 box.setColor(color)
  242.                 savePrefs()
  243.             else
  244.                 error("Invalid color value!")
  245.             end
  246.         elseif cmd == "tcolor" then
  247.             if args[1] and tonumber(args[1]) then
  248.                 tcolor = tonumber(args[1])
  249.                 for i,v in pairs(labels) do
  250.                     v.setColor(tcolor)
  251.                 end
  252.                 savePrefs()
  253.             else
  254.                 error("Invalid color value!")
  255.             end
  256.         elseif cmd == "rate" then
  257.             if args[1] and tonumber(args[1]) then
  258.                 rate = tonumber(args[1])
  259.                 savePrefs()
  260.             else
  261.                 error("Invalid rate amount!")
  262.             end
  263.         elseif cmd == "pull" then
  264.             repeat
  265.                 turtle.dropUp()
  266.             until not turtle.suck()
  267.             turtle.dropUp()
  268.         elseif cmd == "push" then
  269.             if (#args)%2 == 0 then
  270.                 craftable = ae.listCraft()
  271.                 for i=1,#args,2 do
  272.                     local n = tonumber(args[i])
  273.                     local item = getID(args[i+1])
  274.                     if item then
  275.                         if (not items[item] or items[item] < n) then
  276.                             if craftable[item] then
  277.                                 ae.craft(item,n-(items[item] or 0))
  278.                                 repeat until items[item] >= n
  279.                             else
  280.                                 error("Item "..names[item].." not in system!")
  281.                                 return
  282.                             end
  283.                         end
  284.                         ae.retrieve(item,n,1)
  285.                         for ii=1,16 do
  286.                             if turtle.getItemCount(ii) ~= 0 then
  287.                                 turtle.select(ii)
  288.                                 repeat until turtle.drop()
  289.                             end
  290.                         end
  291.                         turtle.select(1)
  292.                     else
  293.                         error("Item "..args[i+1].." not found!")
  294.                     end
  295.                 end
  296.             else
  297.                 error("Invalid number of arguments!")
  298.             end
  299.         elseif cmd == "clear" then
  300.             gui.clear()
  301.             createGui()
  302.         elseif cmd == "redef" then
  303.             loadDefs()
  304.         elseif cmd == "opac" then
  305.             if args[1] and tonumber(args[1]) then
  306.                 opac = tonumber(args[1])
  307.                 box.setAlpha(opac)
  308.                 savePrefs()
  309.             else
  310.                 error("Invalid alpha value!")
  311.             end
  312.         else
  313.             error("Unkown command!")
  314.         end
  315.     end
  316. end
  317.  
  318. function updateGui()
  319.     while true do
  320.         sleep(rate)
  321.         if errmsg then errmsg.delete() errmsg = nil end
  322.         items = ae.listItems()
  323.         for i,v in pairs(watchers) do
  324.             updateText(v.label,display(v.id))
  325.         end
  326.     end
  327. end
  328.  
  329. loadDefs()
  330. loadPrefs()
  331. createGui()
  332. ApplyPrefs()
  333. while true do
  334.     parallel.waitForAny(getCommand,updateGui)
  335. end
Advertisement
Add Comment
Please, Sign In to add comment