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", "craftacloud")
- 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")
- 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
- error("No default API ?")
- end
- settings.close("settings")
- end
- termSize = term.getSize()
- sleep(1)
- function stringRepeat(txt, times)
- s = ""
- for i=1,times do
- s = s..txt
- end
- return s
- end
- function cycleThrough(a,b)
- if not b then
- b = a
- a = 1
- end
- if not last then
- last = a-1
- end
- if last < b then
- last = last+1
- else
- last = a
- end
- return last
- end
- 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 waitingScreen()
- _,y = term.getCursorPos()
- term.setCursorPos(1,y)
- term.write(stringRepeat(".", cycleThrough(1,termSize)).." ")
- end
- function search(txt)
- txt = tostring(txt)
- matches = {}
- j = 0
- for i=1,#inv do
- size = inv[i].size
- id = inv[i].id
- dmg = inv[i].dmg
- assert(size)
- assert(id)
- if string.find(string.lower(id), string.lower(txt)) then
- matches[#matches+1] = {}
- matches[#matches].id = id
- matches[#matches].size = size
- matches[#matches].dmg = dmg
- end
- end
- if #matches > 0 then
- return matches
- else
- return false
- end
- end
- function undone()
- local g = cx4.Get(0)
- if string.find(g, "done") or string.find(g, "fail") then
- return true
- else
- return false
- end
- end
- function temppost(var)
- f = fs.open("f", "w")
- f.write(var)
- f.close()
- shell.run("temppost", "f")
- fs.delete("f")
- end
- function refresh()
- print("Refreshing inventory...")
- while not inv do
- result, inv = pcall(cx4.Get, 1)
- if result == false then
- return false, "Corrupted inventory data"
- end
- save = fs.open("last_inventory", "w")
- save.write(inv)
- save.close()
- result, inv = pcall(textutils.unserialize, inv)
- if result == false then
- return false
- end
- end
- if type(inv) == "table" then
- print("Done !")
- return true
- else
- return false
- end
- end
- function loadLastInventory()
- if fs.exists("last_inventory") then
- save = fs.open("last_inventory", "r")
- inv = save.readAll()
- save.close()
- inv = textutils.unserialize(inv)
- if type(inv) == "table" then
- return true
- end
- end
- return refresh()
- end
- print("Initializing inventory...")
- if not loadLastInventory() then
- inv = {}
- end
- term.clear()
- term.setCursorPos(1,1)
- if type(inv) == "table" then
- if #inv > 0 then
- print("Inventory initialized")
- else
- print("Unable to initalize the inventory.")
- end
- else
- error("Inventory couldn't be initialized")
- end
- while true do
- print("Commands: search, refresh ")
- term.write(">")
- commands = io.read()
- if not commands then break end
- commands = string.split(commands, " ")
- if string.lower(commands[1]) == "search" and commands[2] then
- matches = search(commands[2])
- if type(matches) == "table" then
- -- print(tostring(#matches).." matches.")
- for i=1, #matches do
- textutils.pagedPrint(" "..i..":["..matches[i].size.."]"..matches[i].id..":"..matches[i].dmg)
- end
- end
- print("End of list !")
- if matches then
- while true do
- print("request <ID> <Amount>")
- term.write(">request ")
- request = io.read()
- if not request then break end
- request = "request "..request
- words = string.split(request, " ")
- words[2] = tonumber(words[2])
- words[3] = tonumber(words[3])
- if type(words[2]) == "number" and type(words[3]) == "number" then
- if words[2] <= #matches and words[2] > 0 then
- cx4.Set(0, "request "..matches[words[2]].id.." "..words[3].." "..matches[words[2]].dmg, false)
- while undone() do
- waitingScreen()
- sleep()
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Done !")
- break
- else
- print("Wrong input")
- end
- end
- end
- else
- print("No items found for that query !")
- end
- elseif string.lower(commands[1]) == "refresh" then
- cx4.Set(0, "refresh")
- while undone() do
- sleep()
- end
- term.clear()
- term.setCursorPos(1,1)
- print("Done !")
- refresh()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement