153 lines
4.5 KiB
Lua
153 lines
4.5 KiB
Lua
local me = peripheral.wrap("back")
|
|
local EXPORT_SIDE = "south"
|
|
|
|
local args = {...}
|
|
|
|
local function starts_with(str, start)
|
|
return str:sub(1, #start) == start
|
|
end
|
|
|
|
local function ends_with(str, ending)
|
|
return ending == "" or str:sub(-#ending) == ending
|
|
end
|
|
|
|
local function waitForEnter()
|
|
print("Press [Enter] to continue.")
|
|
io.read()
|
|
end
|
|
|
|
local function isAEOnline()
|
|
local usage, err = me.getEnergyUsage()
|
|
return usage ~= nil and usage > 0
|
|
end
|
|
|
|
local function fetchItemLists(url)
|
|
local request = http.get(url)
|
|
if request == nil then
|
|
print("HTTP request failed. Please contact the administrator.")
|
|
return nil
|
|
end
|
|
if request.getResponseCode() ~= 200 then
|
|
print("HTTP error code " .. request.getResponseCode() .. " Your link is likely incorrect.")
|
|
return nil
|
|
end
|
|
local itemListText = request.readAll()
|
|
request.close()
|
|
local itemLists, msg = textutils.unserializeJSON(itemListText)
|
|
if not itemLists then
|
|
print("Failed to parse item-list JSON: " .. msg)
|
|
return nil
|
|
end
|
|
return itemLists
|
|
end
|
|
|
|
local function findItem(items, name)
|
|
for _, item in pairs(items) do
|
|
if item.name == name then
|
|
return item
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function attemptItemExport(name, count, side)
|
|
local func = function()
|
|
return me.exportItem(
|
|
{
|
|
name=name,
|
|
count=count
|
|
},
|
|
side
|
|
)
|
|
end
|
|
local success, result = pcall(func)
|
|
if success then
|
|
return result
|
|
else
|
|
return nil, result
|
|
end
|
|
end
|
|
|
|
local function exportItem(name, count)
|
|
print("Exporting " .. count .. "x " .. name)
|
|
local totalTransferred = 0
|
|
while totalTransferred < count do
|
|
local amountToTransfer = count - totalTransferred
|
|
local items = me.listItems()
|
|
local item = findItem(items, name)
|
|
if item ~= nil and item.amount > 0 then
|
|
local count, err = attemptItemExport(name, amountToTransfer, EXPORT_SIDE)
|
|
if count ~= nil then
|
|
print(" Transferred " .. count .. " items.")
|
|
totalTransferred = totalTransferred + count
|
|
if count == 0 then
|
|
print(" Couldn't transfer any items. Please ensure there's space available for exporting.")
|
|
waitForEnter()
|
|
end
|
|
else
|
|
print(" Transfer failed: " .. err)
|
|
waitForEnter()
|
|
end
|
|
-- elseif craftableItem ~= nil and not startedCraftingRecently then
|
|
-- print(" No more items in the system; would you like to attempt to auto-craft " .. amountToTransfer .. " more?[yes/no]")
|
|
-- local response = io.read()
|
|
-- if response == "yes" then
|
|
-- me.craftItem({name=name, count=amountToTransfer})
|
|
-- startedCraftingRecently = true
|
|
-- while me.isItemCrafting({name=name}) do
|
|
-- print(" Crafting in progress...")
|
|
-- os.sleep(0.5)
|
|
-- end
|
|
-- end
|
|
else
|
|
print(" Item doesn't exist in the system. Please add " .. amountToTransfer .. " of this item to the system, or type\"skip\" to skip this item.")
|
|
local response = io.read()
|
|
if response == "skip" then
|
|
-- "skip" by pretending the items were exported.
|
|
totalTransferred = totalTransferred + amountToTransfer
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function exportSchematic(schematic)
|
|
print("Exporting schematic\n \"" .. schematic.__NAME__ .. "\"")
|
|
for i = 1, schematic.__COUNT__ do
|
|
print("Iteration " .. i .. "/" .. schematic.__COUNT__ .. ":")
|
|
for name, count in pairs(schematic) do
|
|
if not (starts_with(name, "__") and ends_with(name, "__")) then
|
|
exportItem(name, count)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
local function exportItemList(itemList)
|
|
print("Exporting items for schematic:\n\t" .. itemList.__NAME__)
|
|
for name, count in pairs(itemList) do
|
|
exportItem(name, count)
|
|
end
|
|
print("Done!")
|
|
end
|
|
|
|
-- MAIN SCRIPT
|
|
if not isAEOnline() then
|
|
print("Could not connect to the AE system.")
|
|
return
|
|
end
|
|
-- Check URL
|
|
if #args < 1 or not http.checkURL(args[1]) then
|
|
print("Missing or invalid item-list URL.")
|
|
return
|
|
end
|
|
|
|
local itemLists = fetchItemLists(args[1])
|
|
if itemLists == nil then
|
|
print("Failed to retrieve item-list. Exiting.")
|
|
return
|
|
end
|
|
|
|
for idx, itemList in pairs(itemLists) do
|
|
exportSchematic(itemList)
|
|
end
|