Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shell.run("pastebin", "get", "jzpwtcvf", "dl")
- shell.run("dl", "ini")
- assert(os.loadAPI("ini"), "Couldn't download the INI api")
- settings = ini.open("settings")
- api = nil
- while api == nil do
- api = settings.read("API", "Name")
- if api == "cx4" then
- cx4ID = settings.read("CloudChest", "ID")
- if cx4ID == false then
- print("A CloudChest ID is required !")
- print("If you don't have one, simply go to https://boomtree.com/cx4 and get one.")
- print("(You'll only have to do this once)")
- term.write(">CloudChest ID: ")
- cx4ID = io.read()
- settings.write("CloudChest", "ID", cx4ID)
- end
- shell.run("dl", "cx4")
- assert(os.loadAPI("cx4"), "Couldn't download the CloudChest api")
- print(cx4ID)
- cx4.Chest(cx4ID)
- elseif api == "craftacloud" then
- packetMaxSize = settings.read("Craftacloud", "packetMaxSize", 4000)
- salt = settings.read("Craftacloud", "ID")
- if not salt then
- print("A unique Craftacloud ID is required !")
- print("What is your Minecraft name ?")
- salt = tostring(io.read())
- assert(salt, "Non-valid Minecraft name")
- settings.write("Craftacloud", "ID", salt)
- end
- cx4 = {}
- function stringSplitInParts(txt, count)
- length = string.len(txt)
- parts = math.floor(length/count)
- lastPartLength = length%count
- composition = {}
- for i=0,parts-1 do
- table.insert(composition, string.sub(txt, (count*i)+1, count*(i+1)))
- end
- if lastPartLength ~= 0 then
- table.insert(composition, string.sub(txt, count*parts+1, length+1))
- end
- return composition
- end
- function sSend(channel, data, doVerifications)
- if doVerifications == nil then
- doVerifications = true
- end
- encoded = textutils.urlEncode(data)
- url = "http://craft-a-cloud.mooo.com/upload.php?name="..textutils.urlEncode(salt..channel).."&data="..encoded
- -- print(salt..channel..":"..string.len(encoded))
- http.request(url)
- local i=0
- if doVerifications then
- while sGet(channel) ~= data do
- if i >= 20 then
- print("Couldn't update "..salt..channel)
- return false
- end
- sleep(0.25)
- i = i+1
- end
- end
- return true
- end
- function Set(channel, data)
- data = tostring(data)
- if not channel then
- return false
- end
- composite = stringSplitInParts(data, packetMaxSize)
- if #composite == 1 then
- return sSend(channel, data)
- else
- if not sSend(channel, "composite "..#composite) then
- return false
- end
- for i=1, #composite do
- print("composite: "..string.len(composite[i]))
- ret = sSend(channel+i, composite[i], false)
- if not ret then
- return false
- end
- end
- end
- return true
- end
- cx4.Set = Set
- function sGet(channel)
- url = "http://craft-a-cloud.mooo.com/download.php?name="..textutils.urlEncode(salt..channel)
- response = ""
- http.request(url)
- event, returnurl, response = os.pullEvent("http_success")
- response = response.readAll()
- return response
- end
- function Get(channel)
- response = sGet(channel)
- if string.sub(response, 1, 10) == "composite " then
- parts = tonumber(string.sub(response, 11))
- if not parts then
- return false
- end
- composite = {}
- for i=1, parts do
- composite[i] = sGet(channel+i)
- end
- result, response = pcall(table.concat, composite)
- if result == true then
- return response
- else
- return false
- end
- else
- return response
- end
- end
- cx4.Get = Get
- else
- print("Using the default API (craftacloud)")
- settings.write("API", "Name", "craftacloud")
- api = "craftacloud"
- end
- settings.close("settings")
- end
- ae = peripheral.find("tileinterface")
- ender = peripheral.find("ender_chest")
- if ender then
- lastSlot = ender.getInventorySize()
- end
- dir = "up"
- function split(str,splitter)
- if not splitter then return end
- if not str then return end
- words = {}
- i=0
- for part in string.gmatch(str, "[^%"..splitter.."]+") do -- get each part
- i=i+1
- words[i] = part
- end
- return words
- end
- string.split = split
- function get(id, amount, dmg)
- if not dmg then
- dmg = 0
- end
- if not amount then
- return false
- end
- if not id then
- return false
- end
- fp = {}
- fp.id = id
- fp.dmg = dmg
- local ret = pcall(ae.exportItem, fp, dir, amount)
- if ender then
- if turtle.getItemCount(1) > 0 then
- return turtle.drop()
- end
- else
- return ret
- end
- end
- function stringSplitInParts(txt, count)
- length = string.len(txt)
- parts = math.floor(length/count)
- lastPartLength = length%count
- composition = {}
- for i=0,parts-1 do
- table.insert(composition, string.sub(txt, (count*i)+1, count*(i+1)))
- end
- if lastPartLength ~= 0 then
- table.insert(composition, string.sub(txt, count*parts+1, length+1))
- end
- return composition
- end
- function submitInventory()
- rawInv = ae.getAvailableItems()
- -- now to treat the data
- inv = {}
- for i=1, #rawInv do
- inv[i] = {}
- inv[i].size = rawInv[i].size
- inv[i].id = rawInv[i].fingerprint.id
- inv[i].dmg = rawInv[i].fingerprint.dmg
- end
- print("Updating online inventory...")
- cx4.Set(1, textutils.serialize(inv))
- cx4.Set(0, "done")
- print("Done !")
- end
- function suckSlot(chest, slot, qty)
- chest.swapStacks(1,slot)
- if not qty then
- local ret = turtle.suck()
- else
- local ret = turtle.suck(qty)
- end
- chest.swapStacks(1,slot)
- return ret
- end
- turtle.suckSlot = suckSlot
- while true do
- commands = nil
- while commands == nil or commands == "done" do
- if ender then
- slot = ender.getStackInSlot(lastSlot)
- if type(slot) == "table" then
- suckSlot(ender, lastSlot)
- turtle.dropDown()
- end
- end
- commands = cx4.Get(0)
- end
- commands = string.split(commands, " ")
- if commands[1] == "request" or commands[1] == "get" then
- if not commands[4] then
- commands[4] = 0
- end
- print(commands[2])
- ret = get(commands[2], commands[3], commands[4])
- if not ret then
- cx4.Set(0, "fail")
- else
- cx4.Set(0, "done")
- end
- elseif commands[1] == "refresh" then
- submitInventory()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement