Skip to content

Instantly share code, notes, and snippets.

@k3rni
Created April 16, 2013 22:43
Show Gist options
  • Save k3rni/5400291 to your computer and use it in GitHub Desktop.
Save k3rni/5400291 to your computer and use it in GitHub Desktop.
Access MCTechTree from inside Computercraft. Needs HTTP API enabled.
args={...}
url="http://mctechtree.herokuapp.com/solve.lua"
name=table.concat(args, ' ')
-- TODO: split on commas, pass multiple items
local request = http.post(url, "items[]="..name)
if request == nil then
error("Error making request")
end
data=request.readAll();
loadstring(data)()
local raw = result.raw
local crafts = result.crafts
function printRaw(items)
local text = "Required resources:\n"
for i,row in ipairs(items) do
local obj = row[1]
local count = row[2]
local stackinfo = row[3]
if stackinfo then
text = text .. count .. " " .. obj .. " (" .. stackinfo .. ")\n"
else
text = text .. count .. " " .. obj .. "\n"
end
end
return text
end
function printCrafts(crafts)
local sc
local rm
local text = "Crafting:\n"
for i,row in ipairs(crafts) do
if row.num then text = text .. row.num .. ".\n" end
if row.count > 1 then sc = (row.count .. " ") else sc = "" end
if row.machine then rm = ("using " .. row.machine .. " craft") else rm = "craft" end
text = text .. rm .. " " .. sc .. row.result .. " from " .. row.ingredients .. "\n"
end
return text
end
textutils.pagedPrint(printRaw(raw) .. printCrafts(crafts), 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment