Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Debug fix ~ flaghacker, all credit goes to GamerNebulae
- --Computed Magic v2.1.3: TT[kami] update, unified version
- --Special thanks to flaghacker for helping me with updating the code
- --Declaring variables: All local variables cannot be used in other programs!
- local tArgs = {...}
- local reqpipe
- local chest
- local jarData = {}
- local jarDataIndex
- local NBT = {}
- local amountManaBean = {}
- local beansToRequest = {}
- if tArgs[1] == "troubleshoot" then troubleshoot = true end
- --Functions:
- --Peripheral detection:
- local function detectPeripherals()
- local p = peripheral.getNames()
- for k,v in pairs(p) do
- if (string.find(v, "LogisticsPipes:Request")) then
- reqpipe = peripheral.wrap(v)
- if troubleshoot then print("Request Pipe found!") end
- elseif (string.find(v, "reinforced_chest") or string.find(v, "thaumiumchest")) then
- chest = peripheral.wrap(v)
- if troubleshoot then print("Buffer found! (BetterStorage mod)") end
- elseif (string.find(v, "iron") or string.find(v, "gold") or string.find(v, "diamond") or string.find(v, "copper") or string.find(v, "silver") or string.find(v, "crystal") or string.find(v, "obsidian")) then
- chest = peripheral.wrap(v)
- if troubleshoot then print("Buffer found! (Iron Chests mod)") end
- elseif (string.find(v, "hungry_chest")) then
- chest = peripheral.wrap(v)
- if troubleshoot then print("Buffer found! (ThaumCraft mod)") end
- elseif (string.find(v, "container_chest")) then
- chest = peripheral.wrap(v)
- if troubleshoot then print("Buffer found! (Vanilla chest)") end
- end
- end
- end
- --Retrieve data from the jars
- local function retrieveData()
- --Retrieving which aspects you have and how much is in those jars and getting information from storage system
- local p = peripheral.getNames()
- local items = reqpipe.getAvailableItems()
- jarData = {}
- for k,v in pairs(p) do
- if (string.find(v, "tt_aspectContainer") or string.find(v, "tilejar")) then
- local aspectJar
- local c = peripheral.call(v, "getAspects")
- if (type(c) == "table") then
- aspectJar = string.lower(c[1]["name"])
- else
- aspectJar = string.lower(c)
- end
- local aspectCount = peripheral.call(v, "getAspectCount", aspectJar)
- if (not jarData[aspectJar]) then
- jarData[aspectJar] = {}
- jarDataIndex = #jarData[aspectJar] + 1
- jarData[aspectJar][jarDataIndex] = aspectCount
- else
- jarDataIndex = #jarData[aspectJar] + 1
- jarData[aspectJar][jarDataIndex] = aspectCount
- end
- end
- end
- for q = 1, #items, 1 do
- local NBTCompound = reqpipe.getNBTTagCompound(items[q][1])
- if type(NBTCompound) == "nil" then error("Report to the thread as 'NBTCompound is nil'") end
- local handler = fs.open("logFile", "a")
- handler.write(textutils.serialize(NBTCompound))
- handler.close()
- error("Done logging, post the 'logFile' file to the thread")
- local aspectBean = NBTCompound["value"]["Aspects"]["value"][1]["value"]["key"]["value"]
- NBT[aspectBean] = items[q][1]
- amountManaBean[aspectBean] = items[q][2]
- end
- end
- local function request(beanType, amountType)
- reqpipe.makeRequest(NBT[beanType], amountType)
- end
- --Calculate the amount of mana beans you can request
- local function calculate()
- local chestSpaceNeeded = 0
- for k,v in pairs(jarData) do
- for q = 1, #jarData[k] do
- if (not beansToRequest[k]) then
- beansToRequest[k] = 64 - jarData[k][q]
- else
- beansToRequest[k] = beansToRequest[k] + (64 - jarData[k][q])
- end
- end
- chestSpaceNeeded = chestSpaceNeeded + math.ceil(beansToRequest[k] / 64)
- end
- if (chestSpaceNeeded <= chest.getInventorySize()) then
- for k,v in pairs(beansToRequest) do
- if (not NBT[k]) then
- if troubleshoot then print("You do not have any "..k.." mana beans!") end
- elseif (amountManaBean[k] < v) then
- request(k, amountManaBean[k] - 1)
- else
- request(k,v)
- end
- end
- else
- for k,v in pairs(beansToRequest) do
- local invSpaceFound = false
- while (not invSpaceFound) do
- for q = 1, chest.getInventorySize() do
- if (not chest.getStackInSlot(q)) then
- if (not NBT[k]) then
- if troubleshoot then print("You do not have any "..k.." mana beans!") end
- elseif (amountManaBean[k] < v) then
- request(k, amountManaBean[k] - 1)
- else
- request(k,v)
- end
- elseif (q == chest.getInventorySize() and not invSpaceFound) then
- sleep(10)
- end
- end
- end
- end
- end
- end
- local function run()
- while (true) do
- detectPeripherals()
- retrieveData()
- calculate()
- sleep(600)
- end
- end
- run()
Advertisement
Add Comment
Please, Sign In to add comment