Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local p = peripheral.wrap("bottom")
- local c = peripheral.wrap("top")
- local function loadRecipes() -- return contents of recipes as table
- local data = nil
- if fs.exists("recipes") then
- file = fs.open("recipes", "r")
- data = textutils.unserialize(file.readAll())
- file.close()
- end
- return data
- end
- local function learnRecipe() --returns disp_name (string), recipeLearned (bool), recipeExists (bool)
- local product = p.getStackInSlot(1)
- local recipeExists, recipeLearned = false
- if product then
- local recipe = {product = product.display_name, schematic = {}, ingredient = {}}
- for i=2, 82 do
- local slot = p.getStackInSlot(i)
- if slot then
- local name = slot.display_name
- recipe.schematic[i] = name
- if recipe.ingredient[name] then
- recipe.ingredient[name] = recipe.ingredient[name] + slot.qty
- else
- recipe.ingredient[name] = slot.qty
- end
- end
- end --ends for schematic record loop
- local savedRecipes = {}
- local writeRecipe = false
- if fs.exists("recipes") then
- savedRecipes = loadRecipes()
- if savedRecipes[recipe.product] then
- --print("Recipe for "..recipe.product.. " already exists.")
- recipeExists = true
- else writeRecipe = true
- end
- else writeRecipe = true
- end
- if writeRecipe then
- savedRecipes[product.display_name] = recipe
- local file = fs.open("recipes", "w")
- file.write(textutils.serialize(savedRecipes))
- file.close()
- recipeLearned = true
- end
- end
- return product.displayname, recipeLearned, recipeExists
- end
- local function chestQuery() -- returns table | table.slotNum == display_name, table.qty.display_name == total qty
- local chestSize = c.getInventorySize()
- local chest = {slotNum = {}, qty = {}}
- for i=1, chestSize do
- local slot = c.getStackInSlot(i)
- if slot then
- local name = slot.display_name
- chest.slotNum[i] = name
- if chest.qty[name] then
- chest.qty[name] = chest.qty[name] + slot.qty
- else
- chest.qty[name] = slot.qty
- end
- end
- end
- return(chest)
- end
- local function checkStock(item) -- returns boolean 'craftable', table missingIngredients
- local recipes = loadRecipes()
- local stock = chestQuery()
- local craftable = false
- for k,v in pairs(recipes) do
- if k == item then
- craftable = true
- end
- end
- local missingIngredients = recipes[item].ingredient
- local missingSum = 0
- if craftable then
- for k,v in pairs(stock.qty) do
- missingIngredients[k] = missingIngredients[k] - v
- missingSum = missingSum + missingIngredients[k]
- end
- end
- if missingSum <= 0 then
- craftable = true
- else
- craftable = false
- end
- return craftable, missingIngredients
- end
- local function craft(item)
- if checkStock(item) then
- local recipes = loadRecipes()
- local schematic = recipes[item].schematic
- local chest = chestQuery()
- for k,v in pairs(schematic) do
- for k1,v1 in pairs(chest.slotNum) do
- if k==k1 then
- c.pushItem("down",1,1)
- p.pullItemIntoSlot("up", k)
- end
- end
- end
- end
- end
- if tArgs[1] == "learn" then
- local x,y,z = learnRecipe()
- if y then
- print("Sucessfully learnt.")
- elseif z then
- print("Recipe already known.")
- else
- print("Recipe cannot be learnt. (Invalid product?)")
- end
- elseif tArgs[1] == "recipes" then
- if fs.exists("recipes") then
- local file = fs.open("recipes","r")
- local recipes = textutils.unserialize(file.readAll())
- file.close()
- for k,v in pairs(recipes) do
- print(k)
- end
- else
- print("No recipes exist.")
- end
- elseif tArgs[1] == "craft" then
- if #tArgs >= 2 then
- local itemName = tArgs[2]
- if #tArgs >= 3 then
- for i=3,#tArgs do
- itemName = itemName.. " ".. tArgs[i]
- end
- end
- craft(itemName)
- end
- elseif tArgs[1] == "chestQuery" then
- print(textutils.serialize(chestQuery()))
- elseif tArgs[1] == "checkStock" then
- if #tArgs >= 2 then
- local itemName = tArgs[2]
- if #tArgs >= 3 then
- for i=3,#tArgs do
- itemName = itemName.. " ".. tArgs[i]
- end
- end
- local x, y = checkStock(itemName)
- print(x)
- print(textutils.serialize(y))
- end
- end
Add Comment
Please, Sign In to add comment