Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Modify blockLocation to change where the blocks are situated based on the computer
- local blockLocation = {storage = "right", altar = "back"}
- local args = {...}
- local STATES = {crafting = "craft", orb = "orb", empty = "empty"}
- local chest = peripheral.wrap(blockLocation.storage)
- local altar = peripheral.wrap(blockLocation.altar)
- altar["state"] = STATES.empty
- altar["full"] = 0
- local bloodOrb = {name = "bloodmagic:blood_orb"}
- local craftInstructions = {keepCrafting = true, step = 0, currentItemAmount = 0, amountToCraft = 1, baseItem = "", outcomeItem = ""}
- local Recipes = {}
- --********************************************--
- -- CRAFTING FUNCTIONS --
- --********************************************--
- function getFirstEmptySlot(storage)
- for i = 1,storage.size(),1 do
- if storage.list()[i] == nil then
- return i
- end
- end
- return 0
- end
- function craft()
- local storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
- local storageOutcomeInfo = getItemInfoFromStorage(craftInstructions["outcomeItem"], chest)
- if altar["state"] == STATES.crafting then
- if getItemInAltar() == craftInstructions["outcomeItem"] then
- local foundSpace = false
- for name, data in pairs(storageOutcomeInfo["positions"]) do
- if moveItemTo(1, data, altar, blockLocation.storage) ~= 0 and next(altar.list()) == nil then
- foundSpace = true
- break
- end
- end
- if foundSpace == false then
- moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
- end
- craftInstructions["step"] = craftInstructions["step"] + 1
- craftInstructions["currentItemAmount"] = craftInstructions["currentItemAmount"] + 1
- term.clear()
- write("\nTo close the program, press Q\n")
- write("\nCrafted Items: " .. craftInstructions["currentItemAmount"])
- if craftInstructions["amountToCraft"] ~= -1 then
- write("/" .. craftInstructions["amountToCraft"] .. "\n")
- else
- write(" Crafted\n" .. getItemInfoFromStorage(craftInstructions["baseItem"], chest).nbrOfItems .. " left in Storage\n")
- end
- if craftInstructions["step"] == bloodOrb["interval"] then
- moveBloodOrb()
- altar["state"] = STATES.orb
- craftInstructions["step"] = 0
- else
- storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
- if storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["amountToCraft"] == -1 then
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- elseif storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["currentItemAmount"] < craftInstructions["amountToCraft"] then
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- else
- moveBloodOrb()
- craftInstructions["keepCrafting"] = false
- end
- end
- elseif getItemInAltar() == "" then
- storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
- if storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["amountToCraft"] == -1 then
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- elseif storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["currentItemAmount"] < craftInstructions["amountToCraft"] then
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- else
- moveBloodOrb()
- craftInstructions["keepCrafting"] = false
- end
- end
- elseif altar["state"] == STATES.orb then
- if altar.getTanks()[1]["amount"]/ altar.getTanks()[1]["capacity"] > 0.90 and altar["full"] == 4 then
- moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- altar["state"] = STATES.crafting
- altar["full"] = 0
- elseif altar.getTanks()[1]["amount"]/ altar.getTanks()[1]["capacity"] > 0.90 and altar["full"] ~= 4 then
- altar["full"] = altar["full"] + 1
- end
- elseif altar["state"] == STATES.empty then
- moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
- altar["state"] = STATES.crafting
- altar["full"] = 0
- else
- write("\nwth...")
- end
- sleep(0.5)
- end
- function moveBloodOrb()
- if getItemInfoFromStorage(bloodOrb["name"], altar).nbrOfItems > 0 then
- write("moving orb to storage")
- moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
- elseif getItemInfoFromStorage(bloodOrb["name"], chest).nbrOfItems > 0 then
- write("moving orb to altar")
- moveItemTo(getItemInfoFromStorage(bloodOrb["name"], chest).positions[1], 1, chest, blockLocation.altar)
- end
- end
- function findOrbInStorage(storage)
- local items = storage.list()
- local positions = {}
- local nbrOfItems = 0
- for name, data in pairs(items) do
- if data["name"] == "bloodmagic:blood_orb" then
- return name
- end
- end
- return nil
- end
- function getItemInAltar()
- items = altar.list()
- local item = next(altar.list())
- if items[item] ~= nil then
- return items[item].name .. ":" .. items[item].damage
- end
- return ""
- end
- --*
- -- function that will return positions of the item in said storage with how many it contains
- -- item : string of item ex: "minecraft:stone:0" or "bloodmagic:blood_orb"
- -- storage : peripheral in which we look for the item
- --*
- function getItemInfoFromStorage(item, storage)
- local size = storage.size()
- local itemList = storage.list()
- local positions = {}
- local nbrOfItems = 0
- local modName, itemName, itemDamage = item:match("([^,]+):([^,]+):([^,]+)")
- if modName == nil and itemName == nil then
- modName, itemName = item:match("([^,]+):([^,]+)")
- end
- if modName == nil and itemName == nil then
- return {positions = positions, nbrOfItems = nbrOfItems}
- end
- if itemDamage == nil then
- itemDamage = "0"
- end
- for i = 1,size,1 do
- if itemList[i] ~= nil then
- if itemList[i]["name"] == modName .. ":" .. itemName and tostring(itemList[i]["damage"]) == itemDamage then
- table.insert(positions, i)
- nbrOfItems = nbrOfItems + itemList[i]["count"]
- end
- end
- end
- return {positions = positions, nbrOfItems = nbrOfItems}
- end
- --*
- -- function that will move an item from one inventory to another
- -- positionFrom : position of the item to move in starting inventory
- -- positionTo : position of the item to move in starting inventory
- -- storage : starting inventory peripheral in which we look for the item
- -- direction : location of end inventory
- --*
- function moveItemTo(positionFrom, positionTo, storage, direction)
- return storage.pushItems(direction, positionFrom, positionTo)
- end
- function initializeCrafting()
- if args[3] ~= nil and tonumber(args[3]) > -1 then
- craftInstructions["amountToCraft"] = tonumber(args[3])
- else
- craftInstructions["amountToCraft"] = -1
- end
- craftInstructions["baseItem"] = Recipes[args[2]]["base"]
- craftInstructions["outcomeItem"] = Recipes[args[2]]["outcome"]
- if (verifyCraftingCommand() == false) then
- return false
- end
- if next(altar.list()) == nil then
- altar["state"] = STATES.empty
- elseif findOrbInStorage(altar) ~= nil then
- altar["state"] = STATES.orb
- elseif next(altar.list()) ~= nil then
- moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
- if (verifyCraftingCommand() == false) then
- return false
- end
- else
- local foundSpace = false
- for name, data in pairs(storageOutcomeInfo["positions"]) do
- if moveItemTo(1, data, altar, blockLocation.storage) ~= 0 and next(altar.list()) == nil then
- foundSpace = true
- break
- end
- end
- if foundSpace == false then
- moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
- end
- end
- return true
- end
- function verifyCraftingCommand()
- if tostring(args[3]) == "0" then
- write("Done crafting 0 items.............\n")
- return false
- elseif getFirstEmptySlot(chest) == 0 then
- write("You need an empty inventory spot to craft\n")
- return false
- elseif craftInstructions["amountToCraft"] ~= -1 then
- if getItemInfoFromStorage(craftInstructions["baseItem"], chest).nbrOfItems < tonumber(args[3]) then
- write("Not enough items in the inventory spot to craft\n")
- return false
- end
- end
- return true
- end
- function exitProgramOnKey()
- while true do
- local eventList = { os.pullEvent() }
- if eventList[1] == "key" then
- if eventList[2] == 16 then
- craftInstructions["keepCrafting"] = false
- return
- end
- end
- end
- end
- function activateCrafting()
- bloodOrb["interval"] = 16;
- if (initializeCrafting() == false) then
- return
- end
- write("To close the program, press Q\n")
- while craftInstructions["keepCrafting"] == true do
- parallel.waitForAny(craft,exitProgramOnKey)
- end
- write("\nProgram Terminated\n")
- end
- --- start check
- -- items to craft?
- -- place to put item?
- -- version of computercraft too old
- --
- --********************************************--
- -- RECIPES FUNCTIONS --
- --********************************************--
- function listRecipes()
- for name, data in pairs(Recipes) do
- write(name .. " - base: " .. data["base"] .. ", outcome: " .. data["outcome"] .. "\n")
- end
- end
- function addRecipe()
- write("Enter the name of the craft: ")
- local name = read()
- write("Enter the base of the craft: ")
- local base = read()
- write("Enter the outcome of the craft: ")
- local outcome = read()
- if setContains(Recipes, name) then
- write("Recipe name already exists, replace? (y/n)")
- local replacing = read()
- if (string.sub(replacing,1,1) == "y") then
- Recipes[name] = {base = base, outcome = outcome}
- saveRecipeToFile()
- else
- write("Ending action..\n")
- end
- else
- Recipes[name] = {base = base, outcome = outcome}
- saveRecipeToFile()
- end
- end
- function modifyRecipe(name)
- if setContains(Recipes, name) then
- valid = false
- while (valid == false)
- do
- write("Name: " .. name .. " - Base: " .. Recipes[name]["base"] .. " - Outcome: " .. Recipes[name]["outcome"] .. "\n")
- write("What would you like to change?\n")
- term.setTextColor(colors.green)
- write ("name/base/outcome\n")
- term.setTextColor(colors.white)
- local elementToChange = read()
- if elementToChange == "name" or elementToChange == "base" or elementToChange == "outcome" then
- write("Enter the new value of " .. elementToChange .. ":\n")
- local valueToChange = read()
- local fromElement = ""
- if elementToChange == "name" then
- fromElement = name
- else
- fromElement = Recipes[name][elementToChange]
- end
- writeColors({{"Are you sure you want to change the "}, {elementToChange, "green"}, {" from "}, {fromElement, "blue"}, {" to "}, {valueToChange, "lightBlue"}, {"?(y/n)"} })
- local confirmReplacement = read()
- if (string.sub(confirmReplacement,1,1) == "y") then
- local modifiedRecipe = {}
- local newName = name
- if elementToChange == "name" then
- modifiedRecipe[valueToChange] = {base = Recipes[name]["base"], outcome = Recipes[name]["outcome"]}
- newName = valueToChange
- elseif elementToChange == "base" then
- modifiedRecipe[name] = {base = valueToChange, outcome = Recipes[name]["outcome"]}
- elseif elementToChange == "outcome" then
- modifiedRecipe[name] = {base = Recipes[name]["base"], outcome = valueToChange}
- end
- if elementToChange == "name" and setContains(Recipes, newName) then
- writeColors({{"Recipe named "}, {newName, "green"}, {" already exists, replace?(y/n)"}})
- local replacing = read()
- if (string.sub(replacing,1,1) == "y") then
- Recipes[name] = nil
- Recipes[newName] = modifiedRecipe[newName]
- saveRecipeToFile()
- valid = true
- end
- else
- Recipes[newName] = modifiedRecipe[newName]
- Recipes[name] = nil
- saveRecipeToFile()
- valid = true
- end
- else
- write("\n")
- end
- else
- write("not a valid option\n")
- end
- end
- else
- write("There are no recipes with the name.\n")
- end
- end
- function deleteRecipe(name)
- writeColors({{"Are you sure you want to delete the "}, {name, "green"}, {"?(y/n)"} })
- local confirmDeleting = read()
- if (string.sub(confirmDeleting,1,1) == "y") then
- Recipes[name] = nil
- writeColors({{name, "green"},{" has been deleted\n"}})
- saveRecipeToFile()
- end
- end
- function saveRecipeToFile()
- file = io.open("bloodmagic-recipes.txt","w")
- for name, data in pairs(Recipes) do
- file:write(name .. ";" .. data["base"] .. ";" .. data["outcome"] .. "\n")
- end
- file:flush()
- file:close()
- end
- function readRecipeFile()
- file = io.open("bloodmagic-recipes.txt", "r")
- if file then
- for line in file:lines() do
- if (line:match("([^,]+);([^,]+);([^,]+)")) then
- local name, base, outcome = line:match("([^,]+);([^,]+);([^,]+)")
- Recipes[name] = {base = base, outcome = outcome}
- end
- end
- else
- file = io.open("bloodmagic-recipes.txt","w")
- file:close()
- end
- end
- function setContains(set, key)
- return set[key] ~= nil
- end
- function writeColors(input)
- for name, data in pairs(input) do
- if data[2] == nil then
- data[2] = "white"
- end
- term.setTextColor(colors[data[2]])
- write(data[1])
- term.setTextColor(colors.white)
- end
- end
- --********************************************--
- -- HELP FUNCTIONS --
- --********************************************--
- function help(sectionName)
- if sectionName == "all" then
- writeColors({{"This program allows you to automate anything in the blood altar from blood magic, you can add, modify, delete recipes, craft a number of items or until they run out.\n"}})
- writeColors({{" "},{"help", "lightBlue"}, {" - get some help. can be done on all"}, {"\n "}, {"functions\n"}})
- writeColors({{" "},{"craft", "lightBlue"}, {" - This option allows to craft items"}, {"\n "}, {"automatically. For more info, type: "}, {"\n "}, {shell.getRunningProgram() .. " craft help\n", "lime"}})
- writeColors({{" "},{"recipes", "lightBlue"}, {" - This option allows to add, modify,"}, {"\n "}, {"delete or list recipes for more info, type:"}, {"\n "}, {shell.getRunningProgram() .. " recipes help\n", "lime"}})
- elseif sectionName == "craft" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- elseif sectionName == "recipes" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- elseif sectionName == "add" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- elseif sectionName == "modify" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- elseif sectionName == "delete" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- elseif sectionName == "list" then
- writeColors({{" "}, {"The option craft allows you to craft items automatically"}})
- end
- end
- --********************************************--
- -- RUNNING STARTUP --
- --********************************************--
- readRecipeFile()
- if args[1] == "recipes" then
- if args[2] == "add" then
- addRecipe()
- elseif args[2] == "modify" then
- if args[3] ~= nil then
- modifyRecipe(args[3])
- else
- write({{"Missing 3rd argument, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
- end
- elseif args[2] == "delete" then
- if args[3] ~= nil then
- deleteRecipe(args[3])
- else
- writeColors({{"Missing 3rd argument, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
- end
- elseif args[2] == "list" then
- write("---- Available Recipes ----\n")
- term.setTextColor(colors.blue)
- listRecipes()
- term.setTextColor(colors.white)
- write("---------------------------\n")
- elseif args[2] == "help" then
- help("recipes")
- else
- writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
- end
- elseif args[1] == "craft" then
- if args[2] ~= nil and Recipes[args[2]] ~= nil then
- activateCrafting()
- elseif args[2] == "help" then
- help("craft")
- else
- writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
- end
- elseif args[1] == "help" then
- help("all")
- else
- writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement