Advertisement
HPWebcamAble

[CC][1.0] Enderchest Manager

Apr 14th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. --[[
  2. Coded by HPWebcamAble for YUGATUG
  3.  
  4. Forum Post:
  5. http://www.computercraft.info/forums2/index.php?/topic/22918-program-request/
  6.  
  7.  
  8. ==== Description ====
  9. Helps you keep track of the enderchest colors you've used
  10. (From the mod Ender Storage)
  11.  
  12. Run with the argument 'help' for usages
  13.  
  14.  
  15. ==== Variables you can change ====
  16. ]]
  17. local listHeading = "Enderchest List:"
  18. local showInfoInList = true --Wasn't sure if you wanted this by default or not
  19.  
  20. local fileName = "enderchest"
  21.  
  22. --==== Other Variables ====--
  23. local args = {...}
  24. local pName = shell.getRunningProgram()
  25.  
  26. local f = fs.open(fileName,"r")
  27. local items
  28. if f then
  29.   items = textutils.unserialize(f.readAll())
  30. else
  31.   f = fs.open(fileName,"w")
  32.   items = {}
  33. end
  34. f.close()
  35.  
  36. --==== Functions ====--
  37. local function search(toFind)
  38.   for i = 1, #items do
  39.     if items[i][1] == toFind then
  40.       return i
  41.     end
  42.   end
  43.   return false
  44. end
  45.  
  46. local function tc(color)
  47.   if term.isColor() then term.setTextColor(color) end
  48. end
  49.  
  50. local function list() --From my getMethods program... Like I said to do :)
  51.   local w,h = term.getSize()
  52.   term.setCursorPos(1,1)
  53.   term.clear()
  54.   tc(colors.yellow)
  55.   local heading = listHeading
  56.   local hLines = print(heading)+1
  57.   tc(colors.white)
  58.   local pFunctions = items
  59.   local pages = math.ceil(#pFunctions/(h-hLines))
  60.   local fPerPage = math.floor(#pFunctions/pages)
  61.   local cPage = 1
  62.    
  63.   if pages > 1 then
  64.     while true do
  65.       for i = 1, h-hLines do
  66.         local curF = i+fPerPage*(cPage-1)
  67.         if curF > #pFunctions then break end
  68.         term.setCursorPos(1,hLines+i-1)
  69.         if showInfoInList then
  70.           term.write(pFunctions[curF][1].. (pFunctions[curF][2]~=nil and (": "..pFunctions[curF][2]) or "") )
  71.         else
  72.           term.write(pFunctions[curF][1])
  73.         end
  74.       end
  75.       term.setCursorPos(1,h)
  76.       if cPage == 1 then
  77.         tc(colors.yellow)
  78.         term.write("Use arrow keys to navigate (Right) >")
  79.         tc(colors.white)
  80.       elseif cPage < pages then
  81.         tc(colors.yellow)
  82.         term.write("< (Left) Back / Forward (Right) >")
  83.         tc(colors.white)
  84.       else
  85.         tc(colors.yellow)
  86.         term.write("< (Left) Back / Exit (Right) >")
  87.         tc(colors.white)
  88.       end
  89.       local continue = true
  90.       while continue do
  91.         local event,key = os.pullEvent("key")
  92.         if key == keys.right then
  93.           if cPage < pages then
  94.             cPage = cPage+1
  95.             break
  96.           else
  97.             term.setCursorPos(1,h)
  98.             tc(colors.yellow)
  99.             term.write("Press (Right) again to exit     ")
  100.             tc(colors.white)
  101.             while true do
  102.               local event1,key1 = os.pullEvent("key")
  103.               if key1 == keys.right then
  104.                 term.setCursorPos(1,h)
  105.                 term.clearLine()
  106.                 term.setCursorPos(1,math.ceil(#pFunctions/pages)+hLines)
  107.                 error()
  108.               elseif key1 == keys.left then
  109.                 continue = false
  110.                 break
  111.               end
  112.             end
  113.           end
  114.         elseif key == keys.left then
  115.           if cPage > 1 then
  116.             cPage = cPage-1
  117.             break
  118.           end
  119.         end
  120.       end
  121.       term.setCursorPos(1,1)
  122.       term.clear()
  123.       tc(colors.yellow)
  124.       print(heading)
  125.       tc(colors.white)
  126.     end
  127.   else
  128.     for a,b in pairs(pFunctions) do
  129.       if showInfoInList then
  130.         print(b[1].. (b[2]~=nil and (": "..b[2]) or "") )
  131.       else
  132.         print(b[1])
  133.       end
  134.     end
  135.   end
  136. end
  137.  
  138. local function saveList()
  139.   local f = fs.open(fileName,"w")
  140.   f.write(textutils.serialize(items))
  141.   f.close()
  142. end
  143.  
  144. --==== Program ====--
  145. function main()
  146.   if args[1] == "add" then
  147.     if not args[2] then
  148.       print("Usage:")
  149.       print(pName.." add <string>")
  150.       print(pName.." add <string> <info>")
  151.       return
  152.     elseif search(args[2]) then
  153.       print(args[2].." is already on the list")
  154.       return
  155.     end
  156.     local name = args[2]
  157.     table.remove(args,1)
  158.     table.remove(args,1)
  159.     table.insert(items,{name,table.concat(args," ")})
  160.     print("Added "..name.." to the list")
  161.     saveList()
  162.   elseif args[1] == "list" then
  163.     list()
  164.   elseif args[1] == "search" then
  165.     if not args[2] then
  166.       print("Usage:")
  167.       print(pName.." search <string>")
  168.       return
  169.     elseif not search(args[2]) then
  170.       print(args[2].." isn't on the list")
  171.       return
  172.     end
  173.     if items[search(args[2])][2] then
  174.       print(args[2]..": "..items[search(args[2])][2])
  175.     else
  176.       print(args[2].." exists, but doesn't have info")
  177.     end
  178.   elseif args[1] == "delete" then
  179.     if not args[2] then
  180.       print("Usage:")
  181.       print(pName.." delete <string>")
  182.       return
  183.     elseif not search(args[2]) then
  184.       print(args[2].." isn't on the list")
  185.       return
  186.     end
  187.     table.remove(items,search(args[2]))
  188.     saveList()
  189.   else
  190.     print("Usages:")
  191.     print(pName.." add <string>")
  192.     print(pName.." add <string> <info>")
  193.     print(pName.." list")
  194.     print(pName.." search <string>")
  195.     print(pName.." delete <string>")
  196.     return
  197.   end
  198. end
  199.  
  200. local state,err = pcall(main)
  201.  
  202. if not state and err then
  203.   print()
  204.   print("Error!")
  205.   printError(err)
  206. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement