Advertisement
Wyvern67

AEC: Computer

Aug 10th, 2015
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.03 KB | None | 0 0
  1. shell.run("pastebin", "get", "jzpwtcvf", "dl")
  2. shell.run("dl", "ini")
  3. assert(os.loadAPI("ini"), "Couldn't download the INI api")
  4.  
  5. settings = ini.open("settings")
  6.  
  7. api = nil
  8. while api == nil do
  9.     api = settings.read("API", "Name")
  10.     if api == "cx4" then
  11.         cx4ID = settings.read("CloudChest", "ID")
  12.         if cx4ID == false then
  13.             print("A CloudChest ID is required !")
  14.             print("If you don't have one, simply go to https://boomtree.com/cx4 and get one.")
  15.             print("(You'll only have to do this once)")
  16.             term.write(">CloudChest ID: ")
  17.             cx4ID = io.read()
  18.             settings.write("CloudChest", "ID", cx4ID)
  19.         end
  20.         shell.run("dl", "cx4")
  21.         assert(os.loadAPI("cx4"), "Couldn't download the CloudChest api")
  22.         print(cx4ID)
  23.         cx4.Chest(cx4ID)
  24.     elseif api == "craftacloud" then
  25.         packetMaxSize = settings.read("Craftacloud", "packetMaxSize", 4000)
  26.         salt = settings.read("Craftacloud", "ID")
  27.         if not salt then
  28.             print("A unique Craftacloud ID is required !")
  29.             print("What is your Minecraft name ?")
  30.             salt = tostring(io.read())
  31.             assert(salt, "Non-valid Minecraft name")
  32.             settings.write("Craftacloud", "ID", salt)
  33.         end
  34.  
  35.         cx4 = {}
  36.         function stringSplitInParts(txt, count)
  37.             length = string.len(txt)
  38.             parts = math.floor(length/count)
  39.             lastPartLength = length%count
  40.            
  41.             composition = {}
  42.             for i=0,parts-1 do
  43.                 table.insert(composition, string.sub(txt, (count*i)+1, count*(i+1)))
  44.             end
  45.             if lastPartLength ~= 0 then
  46.                 table.insert(composition, string.sub(txt, count*parts+1, length+1))
  47.             end
  48.             return composition
  49.         end
  50.        
  51.         function sSend(channel, data, doVerifications)
  52.             if doVerifications == nil then
  53.                 doVerifications = true
  54.             end
  55.             encoded = textutils.urlEncode(data)
  56.             url = "http://craft-a-cloud.mooo.com/upload.php?name="..textutils.urlEncode(salt..channel).."&data="..encoded
  57.             -- print(salt..channel..":"..string.len(encoded))
  58.             http.request(url)
  59.             local i=0
  60.             if doVerifications then
  61.                 while sGet(channel) ~= data do
  62.                         if i >= 20 then
  63.                             print("Couldn't update "..salt..channel)
  64.                             return false
  65.                         end
  66.                         sleep(0.25)
  67.                         i = i+1
  68.                 end
  69.             end
  70.             return true
  71.         end
  72.        
  73.         function Set(channel, data)
  74.             data = tostring(data)
  75.             if not channel then
  76.                 return false
  77.             end
  78.             composite = stringSplitInParts(data, packetMaxSize)
  79.             if #composite == 1 then
  80.                 return sSend(channel, data)
  81.             else
  82.                 if not sSend(channel, "composite "..#composite) then
  83.                     return false
  84.                 end
  85.                
  86.                 for i=1, #composite do
  87.                     print("composite: "..string.len(composite[i]))
  88.                     ret = sSend(channel+i, composite[i], false)
  89.                     if not ret then
  90.                         return false
  91.                     end
  92.                 end
  93.             end
  94.             return true
  95.         end
  96.         cx4.Set = Set
  97.        
  98.         function sGet(channel)
  99.             url = "http://craft-a-cloud.mooo.com/download.php?name="..textutils.urlEncode(salt..channel)
  100.             response = ""
  101.             http.request(url)
  102.             event, returnurl, response = os.pullEvent("http_success")
  103.             response = response.readAll()
  104.             return response
  105.         end
  106.            
  107.         function Get(channel)
  108.             response = sGet(channel)
  109.             if string.sub(response, 1, 10) == "composite " then
  110.                 parts = tonumber(string.sub(response, 11))
  111.                 if not parts then
  112.                     return false
  113.                 end
  114.                
  115.                 composite = {}
  116.                 for i=1, parts do
  117.                     composite[i] = sGet(channel+i)
  118.                 end
  119.                 result, response = pcall(table.concat, composite)
  120.                 if result == true then
  121.                     return response
  122.                 else
  123.                     return false
  124.                 end
  125.             else
  126.                 return response
  127.             end
  128.         end
  129.         cx4.Get = Get
  130.     else
  131.         print("Using the default API (craftacloud)")
  132.         settings.write("API", "Name", "craftacloud")
  133.         api = "craftacloud"
  134.     end
  135.     settings.close("settings")
  136. end
  137.  
  138. ae = peripheral.find("tileinterface")
  139. ender = peripheral.find("ender_chest")
  140. if ender then
  141.     lastSlot = ender.getInventorySize()
  142. end
  143.  
  144. dir = "up"
  145.  
  146. function split(str,splitter)
  147.     if not splitter then return end
  148.     if not str then return end
  149.     words = {}
  150.     i=0
  151.     for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
  152.         i=i+1
  153.         words[i] = part
  154.     end
  155.     return words
  156. end
  157. string.split = split
  158.  
  159. function get(id, amount, dmg)
  160.     if not dmg then
  161.         dmg = 0
  162.     end
  163.     if not amount then
  164.         return false
  165.     end
  166.     if not id then
  167.         return false
  168.     end
  169.     fp = {}
  170.     fp.id = id
  171.     fp.dmg = dmg
  172.     local ret = pcall(ae.exportItem, fp, dir, amount)
  173.     if ender then
  174.         if turtle.getItemCount(1) > 0 then
  175.             return turtle.drop()
  176.         end
  177.     else
  178.         return ret
  179.     end
  180. end
  181.  
  182. function stringSplitInParts(txt, count)
  183.     length = string.len(txt)
  184.     parts = math.floor(length/count)
  185.     lastPartLength = length%count
  186.    
  187.     composition = {}
  188.     for i=0,parts-1 do
  189.         table.insert(composition, string.sub(txt, (count*i)+1, count*(i+1)))
  190.     end
  191.     if lastPartLength ~= 0 then
  192.         table.insert(composition, string.sub(txt, count*parts+1, length+1))
  193.     end
  194.     return composition
  195. end
  196.  
  197. function submitInventory()
  198.     rawInv = ae.getAvailableItems()
  199.    
  200.     -- now to treat the data
  201.     inv = {}
  202.     for i=1, #rawInv do
  203.         inv[i] = {}
  204.         inv[i].size = rawInv[i].size
  205.         inv[i].id = rawInv[i].fingerprint.id
  206.         inv[i].dmg = rawInv[i].fingerprint.dmg
  207.     end
  208.    
  209.     print("Updating online inventory...")
  210.     cx4.Set(1, textutils.serialize(inv))
  211.     cx4.Set(0, "done")
  212.     print("Done !")
  213. end
  214.  
  215. function suckSlot(chest, slot, qty)
  216.     chest.swapStacks(1,slot)
  217.     if not qty then
  218.         local ret = turtle.suck()
  219.     else
  220.         local ret = turtle.suck(qty)
  221.     end
  222.     chest.swapStacks(1,slot)
  223.     return ret
  224. end
  225. turtle.suckSlot = suckSlot
  226.  
  227.  
  228.  
  229.  
  230.  
  231. while true do
  232.     commands = nil
  233.     while commands == nil or commands == "done" do
  234.         if ender then
  235.             slot = ender.getStackInSlot(lastSlot)
  236.             if type(slot) == "table" then
  237.                 suckSlot(ender, lastSlot)
  238.                 turtle.dropDown()
  239.             end
  240.         end
  241.         commands = cx4.Get(0)
  242.     end
  243.    
  244.     commands = string.split(commands, " ")
  245.     if commands[1] == "request" or commands[1] == "get" then
  246.         if not commands[4] then
  247.             commands[4] = 0
  248.         end
  249.         print(commands[2])
  250.         ret = get(commands[2], commands[3], commands[4])
  251.         if not ret then
  252.             cx4.Set(0, "fail")
  253.         else
  254.             cx4.Set(0, "done")
  255.         end
  256.     elseif commands[1] == "refresh" then
  257.         submitInventory()
  258.     end
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement