kd2bwzgen

CCSnippet

Jul 2nd, 2017
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. --CCSnippets v0.1
  2. --Allows you to run snippets by MineRobber9000
  3.  
  4. if not http then error("HTTP required.") end
  5.  
  6. local function getsetting(sSetting)
  7. local setting = settings.get(sSetting)
  8. if sSetting == "ccsnippet.autorun" and setting==nil then setting = true end
  9. return setting
  10. end
  11.  
  12. local jsonPath = "/"
  13. local jsonLocation = fs.combine(jsonPath,"json")
  14. if not fs.exists(jsonLocation) then
  15. local w = http.get("http://pastebin.com/raw/4nRg9CHU")
  16. local file = fs.open(jsonLocation,"w")
  17. if not w then error("Unable to get json API: download it manually at https://pastebin.com/4nRg9CHU; place it at "..jsonLocation) end
  18. file.write(w.readAll())
  19. file.close()
  20. w.close()
  21. end
  22. os.loadAPI(jsonLocation)
  23.  
  24. local function exit()
  25. fs.delete(jsonLocation)
  26. _G.json = nil
  27. return
  28. end
  29.  
  30. local function usage()
  31. printError("Argument must be a snippet.")
  32. printError("Special snippets:")
  33. printError("list - list snippets")
  34. exit()
  35. end
  36.  
  37. local tArgs = {...}
  38. if #tArgs ~= 1 then usage() else
  39.  
  40. local web = http.get("http://tilde.town/~minerobber/ccsnippets/snippets.json")
  41. local obj = json.decode(web.readAll())
  42. web.close()
  43.  
  44. if tArgs[1]=="list" then
  45. local tSnippets = {}
  46. for k,v in pairs(obj) do table.insert(tSnippets,k) end
  47. print("Snippets:")
  48. if #tSnippets == 0 then print("None.") exit() end
  49. write(tSnippets[1])
  50. if #tSnippets > 1 then
  51. for i=2,#tSnippets do
  52. write(", "..tSnippets[i])
  53. end
  54. end
  55. print()
  56. exit()
  57. else
  58. for k,v in pairs(obj) do
  59. if tArgs[1]==k then
  60. if getsetting("ccsnippet.autorun") then
  61. local h = fs.open("/.tempsnippet","w")
  62. h.write(v)
  63. h.close()
  64. shell.run("/.tempsnippet")
  65. fs.delete("/.tempsnippet")
  66. else
  67. local h = fs.open(k..".lua","w")
  68. h.write(v)
  69. h.close()
  70. print("Downloaded "..k.."!")
  71. exit()
  72. end
  73. end
  74. end
  75. if not snippet == "list" then error("Invalid snippet "..tArgs[1].."!",0) end
  76. exit()
  77. end
  78. end
Add Comment
Please, Sign In to add comment