myidnum = 1
fillerItem = "minecraft:wooden_sword"
quantitydelimiter = "*"
recipefile = "recipes"
bridgechestdirection = {"north", "east", "south", "west"}
function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function stripSecondColen(str)
return table.concat({str:match( "([^:]+:[^:]+):?(.*)" )}, " ")
end
function loadRecipes(filename)
file = fs.open(filename, "r")
data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function grabItem(modem, channel, item)
itemname = nil
quantity = nil
if item == nil or item == "" or item == "x" then
itemname = fillerItem
quantity = 1
else
table = split(item, quantitydelimiter)
if #table == 1 then
itemname = trim(table[1])
quantity = 1
elseif #table >= 2 then
itemname = trim(table[1])
quantity = tonumber(trim(table[2]))
else
itemname = fillerItem
quantity = 1
end
end
modem.transmit(channel, myidnum, "Get "..tostring(quantity).." "..itemname)
transformeditemname = stripSecondColen(itemname)
modem.transmit(channel, myidnum, "Converting itemname to ME bridge format")
retrieved = bridge.retrieve(transformeditemname, quantity, bridgechestdirection[myidnum])
quantity = quantity - retrieved
while quantity > 0 do
modem.transmit(channel, myidnum, "Only "..tostring(retrieved).." available...crafting more")
bridge.craft(transformeditemname, quantity)
crafting = true
while crafting do
local event, name, amount, bytes = os.pullEvent("craftingComplete")
if trim(name) == trim(transformeditemname) then
crafting = false
end
end
retrieved = bridge.retrieve(transformeditemname, quantity, bridgechestdirection[myidnum])
quantity = quantity - retrieved
end
end
function infuse(modem, channel, recipe)
redstone.setOutput("left", true)
modem.transmit(channel, myidnum, "Filler retriever locked.")
for i = 1, #recipe do
grabItem(modem, channel, recipe[i])
end
sleep(20)
redstone.setOutput("left", false)
sleep(180)
modem.transmit(channel, myidnum, "Whacking matrix...")
redstone.setOutput("front", true)
sleep(2)
redstone.setOutput("front", false)
modem.transmit(channel, myidnum, "Waiting for completion.")
sleep(300)
redstone.setOutput("back", true)
modem.transmit(channel, myidnum, "Grabbing resultant item.")
sleep(10)
redstone.setOutput("back", false)
modem.transmit(channel, myidnum, "Infusion complete.")
end
recipes = loadRecipes(recipefile)
modem = peripheral.wrap("top")
modem.open(myidnum)
bridge = peripheral.wrap("right")
escape = false
while escape == false do
listening = true
command = ""
replychannel = 0
while listening do
local event, side, senton, replyon, message, distance = os.pullEvent("modem_message")
if senton == myidnum and replyon == 5 then
listening = false
command = message
replychannel = replyon
end
end
commandtable = split(trim(command), " ")
prefix = trim(commandtable[1])
if prefix == "infuse" then
recipenum = tonumber(trim(commandtable[2]))
infuse(modem, replychannel, recipes[recipenum])
end
end