bruegge

MCS:CODE:CobblestoneFarm

Mar 6th, 2018
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.47 KB | None | 0 0
  1. function Pastebin(user, password)
  2.     local Pastebin = {}
  3.     Pastebin.api_user_key = 0
  4.     Pastebin.API_version = 0.8
  5.      
  6.     -- Internal function that parses the pastebin XML response.
  7.     function Pastebin:parse(response)
  8.         local objs = {}
  9.         for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
  10.             local obj = {}
  11.             for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
  12.                 obj[key] = value
  13.             end
  14.             objs[#objs+1] = obj
  15.         end
  16.         return objs
  17.     end
  18.      
  19.     -- Used to be a custom function, but I realized CC-Lua had one already.
  20.     Pastebin.url_encode = textutils.urlEncode
  21.      
  22.     -- My Pastebin API key.
  23.     Pastebin.api_dev_key = "c09548865c8dd0c2bf57d0d060b5e96a"
  24.      
  25.     -- Allowable inputs for paste creation options.
  26.     Pastebin.paste_formats = {["4cs"]=true,["6502acme"]=true,["6502kickass"]=true,["6502tasm"]=true,abap=true,actionscript=true,actionscript3=true,ada=true,algol68=true,apache=true,applescript=true,apt_sources=true,arm=true,asm=true,asp=true,asymptote=true,autoconf=true,autohotkey=true,autoit=true,avisynth=true,awk=true,bascomavr=true,bash=true,basic4gl=true,bibtex=true,blitzbasic=true,bnf=true,boo=true,bf=true,c=true,c_mac=true,cil=true,csharp=true,cpp=true,["cpp-qt"]=true,c_loadrunner=true,caddcl=true,cadlisp=true,cfdg=true,chaiscript=true,clojure=true,klonec=true,klonecpp=true,cmake=true,cobol=true,coffeescript=true,cfm=true,css=true,cuesheet=true,d=true,dcl=true,dcpu16=true,dcs=true,delphi=true,oxygene=true,diff=true,div=true,dos=true,dot=true,e=true,ecmascript=true,eiffel=true,email=true,epc=true,erlang=true,fsharp=true,falcon=true,fo=true,f1=true,fortran=true,freebasic=true,freeswitch=true,gambas=true,gml=true,gdb=true,genero=true,genie=true,gettext=true,go=true,groovy=true,gwbasic=true,haskell=true,haxe=true,hicest=true,hq9plus=true,html4strict=true,html5=true,icon=true,idl=true,ini=true,inno=true,intercal=true,io=true,j=true,java=true,java5=true,javascript=true,jquery=true,kixtart=true,latex=true,ldif=true,lb=true,lsl2=true,lisp=true,llvm=true,locobasic=true,logtalk=true,lolcode=true,lotusformulas=true,lotusscript=true,lscript=true,lua=true,m68k=true,magiksf=true,make=true,mapbasic=true,matlab=true,mirc=true,mmix=true,modula2=true,modula3=true,["68000devpac"]=true,mpasm=true,mxml=true,mysql=true,nagios=true,newlisp=true,text=true,nsis=true,oberon2=true,objeck=true,objc=true,["ocaml-brief"]=true,ocaml=true,octave=true,pf=true,glsl=true,oobas=true,oracle11=true,oracle8=true,oz=true,parasail=true,parigp=true,pascal=true,pawn=true,pcre=true,per=true,perl=true,perl6=true,php=true,["php-brief"]=true,pic16=true,pike=true,pixelbender=true,plsql=true,postgresql=true,povray=true,powershell=true,powerbuilder=true,proftpd=true,progress=true,prolog=true,properties=true,providex=true,purebasic=true,pycon=true,python=true,pys60=true,q=true,qbasic=true,rsplus=true,rails=true,rebol=true,reg=true,rexx=true,robots=true,rpmspec=true,ruby=true,gnuplot=true,sas=true,scala=true,scheme=true,scilab=true,sdlbasic=true,smalltalk=true,smarty=true,spark=true,sparql=true,sql=true,stonescript=true,systemverilog=true,tsql=true,tcl=true,teraterm=true,thinbasic=true,typoscript=true,unicon=true,uscript=true,ups=true,urbi=true,vala=true,vbnet=true,vedit=true,verilog=true,vhdl=true,vim=true,visualprolog=true,vb=true,visualfoxpro=true,whitespace=true,whois=true,winbatch=true,xbasic=true,xml=true,xorg_conf=true,xpp=true,yaml=true,z80=true,zxbasic=true}
  27.     Pastebin.paste_private_options = {[0]=true,[1]=true,[2]="Private"}
  28.     Pastebin.paste_expire_date_options = {["N"]=true,["10M"]=true,["1H"]=true,["1D"]=true,["1W"]=true,["2W"]=true,["1M"]=true}
  29.      
  30.     -- Creates a new paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
  31.     -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
  32.     --
  33.     -- Returns paste URL or error code.
  34.     function Pastebin:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  35.       if not api_paste_expire_date then
  36.         api_paste_expire_date = "N"
  37.       end
  38.       assert(type(api_paste_code) == "string", "Only strings can be uploaded as pastes! Use `tostring()` if necessary!")
  39.       local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_option=paste", "&api_paste_code=", self.url_encode(api_paste_code)}
  40.       if self.api_user_key then if type(self.api_user_key) == "string" then local len = #urlstr urlstr[len+1] = "&api_user_key=" urlstr[len+2] = self.url_encode(self.api_user_key) else io.stderr:write("Invalid user key!\n") end end
  41.       if api_paste_name then if type(api_paste_name) == "string" then local len = #urlstr urlstr[len+1] = "&api_paste_name=" urlstr[len+2] = self.url_encode(api_paste_name) else io.stderr:write("Only strings can be used to name pastes! Use `tostring()` if necessary!") end end
  42.       if api_paste_format then if self.paste_formats[api_paste_format] then local len = #urlstr urlstr[len+1] = "&api_paste_format=" urlstr[len+2] = self.url_encode(api_paste_format) else io.stderr:write("Invalid paste format!") end end
  43.       if api_paste_private then local paste_private_long = self.paste_private_options[api_paste_private] if paste_private_long then if paste_private_long == "Private" and not self.api_user_key then io.stderr:write("User key necessary for private pastes!") else local len = #urlstr urlstr[len+1] = "&api_paste_private=" urlstr[len+2] = self.url_encode(api_paste_private) end else io.stderr:write("Invalid privacy level") end end
  44.       if api_paste_expire_date then if self.paste_expire_date_options[api_paste_expire_date] then local len = #urlstr urlstr[len+1] = "&api_paste_expire_date=" urlstr[len+2] = api_paste_expire_date else io.stderr:write("Invalid paste expiration date!") end end
  45.      
  46.       return http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  47.     end
  48.      
  49.     -- Authenticates with Pastebin.
  50.     -- Example: local agente382 = pastebin.login("AgentE382", "thisisnotmypassword")
  51.     --
  52.     -- Returns user key for use with other API functions.
  53.     function Pastebin:loginToPastebin(api_user_name, api_user_password)
  54.         assert(type(api_user_name) == "string" and type(api_user_password) == "string", "Both arguments are required and must be strings!")
  55.         return http.post("http://pastebin.com/api/api_login.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_name="..self.url_encode(api_user_name).."&api_user_password="..self.url_encode(api_user_password)).readAll()
  56.     end
  57.      
  58.     -- Deletes a paste. Uses user key from login(). api_paste_key must be a string containing the end of the paste's URL.
  59.     -- Example: local response = pastebin.delete("Rxe673BJ")
  60.     --
  61.     -- Returns "Paste Removed" or error message.
  62.     function Pastebin:delete(api_paste_key)
  63.         assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  64.         return http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=delete").readAll()
  65.     end
  66.      
  67.     -- Fetches a paste from Pastebin.
  68.     -- Example: local rc4 = pastebin.get("Rxe673BJ")
  69.     --
  70.     -- Returns requested paste as a string or an error message.
  71.     function Pastebin:get(api_paste_key)
  72.         assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
  73.         local response = http.get("http://pastebin.com/raw.php?i="..self.url_encode(api_paste_key))
  74.         return response and response.readAll()
  75.     end
  76.      
  77.     -- Fetches a private paste from Pastebin. Paste must belong to user logged in with api_user_key
  78.     -- Example: local test = pastebin.getprivate("fhLGZm3i")
  79.     --
  80.     -- Returns requested paste as a string or an error message.
  81.     function Pastebin:getprivate(api_paste_key)
  82.         assert(type(self.api_user_key) == "string" and type(api_paste_key) == "string", "Both arguments required and must be strings!")
  83.         return http.post("http://pastebin.com/api/api_raw.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_paste_key="..self.url_encode(api_paste_key).."&api_option=show_paste").readAll()
  84.     end
  85.      
  86.     -- Lists data about all of a user's pastes. Uses user key from login(). Note that this makes it impossible to get data about random people's pastes.
  87.     -- Example: local allpastes = pastebin.list(1000)
  88.     --
  89.     -- Returns a list of data about all pastes associated with a user's account, using the table format described at the end of the document.
  90.     function Pastebin:list(api_results_limit)
  91.         assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
  92.         local urlstr = {"api_dev_key=", self.url_encode(self.api_dev_key), "&api_user_key=", self.url_encode(self.api_user_key), "&api_option=list"}
  93.         if api_results_limit then if type(api_results_limit) == "number" then if api_results_limit > 1000 then api_results_limit = 1000 elseif api_results_limit < 1 then api_results_limit = 1 end local len = #urlstr urlstr[len+1] = "&api_results_limit=" urlstr[len+2] = self.url_encode(api_results_limit) else io.stderr:write("Results limit must be a number!\n") end end
  94.         local response = http.post("http://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
  95.         if string.find(response,"<") then
  96.             return self:parse(response)
  97.         else
  98.             return response
  99.         end
  100.     end
  101.      
  102.     -- Lists data about the 18 currently trending pastes.
  103.     -- Example: local trendingpastes = pastebin.trending()
  104.     --
  105.     -- Returns a list of data about the 18 currently trending pastes, using the table format described at the end of this document.
  106.     function Pastebin:trending()
  107.         local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_option=trends").readAll()
  108.         if string.find(response,"<") then
  109.             return self:parse(response)
  110.         else
  111.             return response
  112.         end
  113.     end
  114.      
  115.     -- Lists all of a user's settings. Uses user key from login().
  116.     -- Example: local settings = pastebin.settings()
  117.     --
  118.     -- Returns a list of the user's settings, using the table format described at the end of this document.
  119.     function Pastebin:settings()
  120.         assert(type(self.api_user_key) == "string", "Enter a valid user key as a string!")
  121.         local response = http.post("http://pastebin.com/api/api_post.php", "api_dev_key="..self.url_encode(self.api_dev_key).."&api_user_key="..self.url_encode(self.api_user_key).."&api_option=userdetails").readAll()
  122.         if string.find(response,"<") then
  123.             return unpack(self:parse(response))
  124.         else
  125.             return response
  126.         end
  127.     end
  128.      
  129.     -- Edit a paste. First argument required. All others optional. Pass explicit nils to pass over an argument.
  130.     -- Example: local paste = pastebin.create("This is my awesome paste text!", "Super cool name", nil, nil, "N")
  131.     --
  132.     -- Returns paste URL or error code.
  133.     function Pastebin:edit(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  134.         local list = self:list(1000)
  135.         for a, b in pairs(list) do
  136.             if b and b["title"] == api_paste_name then
  137.                 self:delete(b["key"])
  138.                 local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
  139.                 return message
  140.             end
  141.         end
  142.         local message = self:create(api_paste_code, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)                    
  143.         return message
  144.     end
  145.    
  146.     local function passwordInput(isFirstTime)
  147.         if isFirstTime == true then    
  148.             term.clear()
  149.             print("+-----------------------------------------------+")
  150.             print("| You are using this software the first time on |")
  151.             print("| this computer. This software needs your       |")
  152.             print("| Pastebin username and password                |")
  153.             print("+-----------------------------------------------+")
  154.             write("username:")
  155.             local user = io.read()
  156.             term.clear()
  157.             print("+-----------------------------------------------+")
  158.             print("| You are using this software the first time on |")
  159.             print("| this computer. This software needs your       |")
  160.             print("| Pastebin username and password                |")
  161.             print("+-----------------------------------------------+")
  162.             write("password:")
  163.             local password = io.read()
  164.             term.clear()
  165.             return user, password
  166.         else
  167.             term.clear()
  168.             print("+-------------------------------+")
  169.             print("| Username or password is wrong |")
  170.             print("|           Try again           |")
  171.             print("+-------------------------------+")
  172.             write("username:")
  173.             local user = io.read()
  174.             term.clear()
  175.             print("+-------------------------------+")
  176.             print("| Username or password is wrong |")
  177.             print("|           Try again           |")
  178.             print("+-------------------------------+")
  179.             write("password:")
  180.             local password = io.read()
  181.             term.clear()
  182.             return user, password
  183.         end
  184.     end
  185.    
  186.     local function readUserDataFromFile()
  187.         if fs.exists("MCS_data.txt") then
  188.             local file = fs.open("MCS_data.txt", "r")
  189.             local user = file.readLine()
  190.             local password = file.readLine()
  191.             file.close()
  192.             return user, password
  193.         end
  194.         return false
  195.     end
  196.    
  197.     local function writeUserDataToFile(user, password)
  198.         local file = fs.open("MCS_data.txt", "w")
  199.         file.writeLine(user)
  200.         file.writeLine(password)
  201.         file.close()
  202.     end
  203.    
  204.     function Pastebin:login(user, password)
  205.         local isFirstTime = true
  206.         if not user or not password then
  207.             user, password = readUserDataFromFile()
  208.         end
  209.         while true do
  210.             if user and password then
  211.                 self.api_user_key = self:loginToPastebin(user, password)
  212.                 if self.api_user_key ~= "Bad API request, invalid login" then
  213.                     writeUserDataToFile(user, password)
  214.                     term.clear()
  215.                     return
  216.                 end
  217.             end
  218.             user, password = passwordInput(isFirstTime)
  219.             isFirstTime = false
  220.         end
  221.     end
  222.    
  223.     Pastebin:login(user, password)
  224.     return Pastebin
  225. end
  226.  
  227. function Settings()
  228.     local settings = {}
  229.     settings.settings = {}
  230.     settings.settings.isActive = false
  231.     settings.settings.updateTime = 30
  232.     settings.settings.minimumNumber = 8000
  233.     settings.settings.maximumNumber = 10000
  234.     settings.settings.enableMinMaxNumber = false
  235.    
  236.     function settings:loadSettings()
  237.         if fs.exists("MCS_settings.txt") then
  238.             local file = fs.open("MCS_settings.txt", "r")
  239.             local settings = file.readAll()
  240.             file.close()
  241.             if settings ~= nil then
  242.                 local settingsTable = textutils.unserialize(settings)
  243.                 self:set(settingsTable)
  244.                 return true
  245.             end
  246.         end
  247.         return false
  248.     end
  249.    
  250.     function settings:writeSettings()
  251.         local file = fs.open("MCS_settings.txt", "w")
  252.         file.write(textutils.serialize(self.settings))
  253.         file.close()
  254.     end
  255.    
  256.     function settings:init()
  257.         if self:loadSettings() == false then
  258.             self.settings.isActive = true
  259.             self:writeSettings()
  260.         end
  261.     end
  262.    
  263.     function settings:set(settingsTable)
  264.         if settingsTable then
  265.             if settingsTable.isActive then
  266.                 self.settings.isActive = settingsTable.isActive
  267.             end
  268.             if settingsTable.updateTime then
  269.                 self.settings.updateTime = settingsTable.updateTime
  270.             end
  271.             if settingsTable.maximumNumber then
  272.                 self.settings.maximumNumber = settingsTable.maximumNumber
  273.             end
  274.             if settingsTable.minimumNumber then
  275.                 self.settings.minimumNumber = settingsTable.minimumNumber
  276.             end
  277.             if settingsTable.enableMinMaxNumber ~= nil then
  278.                 self.settings.enableMinMaxNumber = settingsTable.enableMinMaxNumber
  279.             end
  280.             self:writeSettings()
  281.         end
  282.     end
  283.    
  284.     function settings:setActive(isActive)
  285.         self.settings.isActive = isActive
  286.         self:writeSettings()
  287.     end
  288.    
  289.     function settings:getIsActive()
  290.         return self.settings.isActive
  291.     end
  292.    
  293.     function settings:getSettings()
  294.         return self.settings
  295.     end
  296.    
  297.     function settings:getUpdateTime()
  298.         return self.settings.updateTime
  299.     end
  300.    
  301.     function settings:getMaximumNumber()
  302.         return self.settings.maximumNumber
  303.     end
  304.    
  305.     function settings:getMinimumNumber()
  306.         return self.settings.minimumNumber
  307.     end
  308.    
  309.     function settings:getEnableMinMaxNumber()
  310.         return self.settings.enableMinMaxNumber
  311.     end
  312.    
  313.     function settings:print()
  314.         term.clear()
  315.         print("isActive: ".. tostring(self:getIsActive()))
  316.         print("enableMinMaxNumber: ".. tostring(self:getEnableMinMaxNumber()))
  317.         print("updateTime: ".. tostring(self:getUpdateTime()))
  318.         if self:getEnableMinMaxNumber() then
  319.             print("minimumNumber: ".. tostring(self:getMinimumNumber()))
  320.             print("maximumNumber: ".. tostring(self:getMaximumNumber()))      
  321.         end    
  322.     end
  323.    
  324.     settings:init()
  325.     return settings
  326. end
  327.  
  328. settings = Settings()
  329.  
  330. function updateToPastebin()
  331.     local pastebin = Pastebin()
  332.     local pasteNameOut = "MCS:Out:CobblestoneFarm:"..os.getComputerID()
  333.     local pasteNameIn = "MCS:In:CobblestoneFarm:"..os.getComputerID()
  334.     local pasteNameMeSystem = "MCS:Out:ME-System:"
  335.     local statusOld = not settings:getIsActive()
  336.     local status = {}
  337.     local cobblestoneItemCount = 0
  338.     local settingsData = {}
  339.     while true do
  340.         settings:print()
  341.         status.isActive = settings:getIsActive()
  342.         if status.isActive ~= statusOld then
  343.             statusOld = status.isActive
  344.             settingsData = settings:getSettings()
  345.             if settingsData then
  346.                 local information = textutils.serialize(settingsData)
  347.                 if information then    
  348.                     local message = pastebin:edit(information, pasteNameOut, nil, nil, "N")
  349.                     print(message)
  350.                 end    
  351.             end
  352.         end
  353.         sleep(settings:getUpdateTime())
  354.         local list = pastebin:list(1000)
  355.         for a, b in pairs(list) do
  356.             if b["title"] == pasteNameIn then
  357.                 local statusString = pastebin:get(b["key"])
  358.                 pastebin:delete(b["key"])
  359.                 if statusString then
  360.                     status = textutils.unserialize(statusString)
  361.                     if status then
  362.                         settings:set(status)
  363.                     end                
  364.                 end
  365.             end
  366.             if settings:getEnableMinMaxNumber() then
  367.                 local result = string.sub(b["title"], 1, string.len(pasteNameMeSystem))
  368.                 if result == pasteNameMeSystem then
  369.                     local items = pastebin:get(b["key"])
  370.                     if items then
  371.                         local itemList = textutils.unserialize(items)
  372.                         for a, b in pairs(itemList) do
  373.                             if b["itemId"] == "minecraft:cobblestone" then
  374.                                 cobblestoneItemCount = b["count"]
  375.                                 if cobblestoneItemCount > settings:getMaximumNumber() then
  376.                                     settings:setActive(false)
  377.                                 end
  378.                                 if cobblestoneItemCount < settings:getMinimumNumber() then
  379.                                     settings:setActive(true)
  380.                                 end                        
  381.                             end
  382.                         end
  383.                     end
  384.                 end
  385.             end
  386.         end    
  387.     end
  388. end
  389.  
  390. function work()
  391.     turtle.select(1)
  392.     local slot = 1
  393.     while true do
  394.         local isActive = settings:getIsActive()
  395.         if isActive == true then
  396.             turtle.dig()
  397.             turtle.digDown()
  398.             local freeSpaceInSlot = turtle.getItemSpace(slot)
  399.             if turtle.getItemSpace(slot) == 0 then
  400.                 slot = slot + 1
  401.                 turtle.select(slot)
  402.                 if slot > 4 then
  403.                     print("empty everything")
  404.                     for emptySlot = 16, 1, -1 do
  405.                         turtle.select(emptySlot)
  406.                         turtle.dropUp()
  407.                     end
  408.                     slot = 1
  409.                     turtle.select(slot)
  410.                 end
  411.             end
  412.         else
  413.             sleep(1)
  414.         end
  415.     end
  416. end
  417.  
  418. parallel.waitForAny(updateToPastebin, work)
Add Comment
Please, Sign In to add comment