Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local recipesConfig = shell.resolve("").."/empowererRecipes.config"
- local empowerer = nil
- local chest = nil
- local chestAddress = nil
- local displayStands = {}
- local recipes = {}
- local logging = false
- local importantItems = {}
- function init()
- for _,name in ipairs(peripheral.getNames()) do
- if name ~= "top" and name ~= "left" and name ~= "right" and name ~= "bottom" and name ~= "front" and name ~= "back" then
- local t = peripheral.getType(name)
- if t == "actuallyadditions:empowerer" then
- empowerer = peripheral.wrap(name)
- elseif t == "actuallyadditions:displaystand" then
- table.insert(displayStands,peripheral.wrap(name))
- elseif t == "minecraft:chest" then
- chest = peripheral.wrap(name)
- chestAddress = name
- end
- end
- end
- if empowerer == nil or
- chest == nil or
- #displayStands ~= 4 then
- term.setTextColor(colors.red)
- print("Error setting up peripherals. Required peripherals are:")
- print("1 x Empowerer")
- print("1 x Chest")
- print("4 x Display Stand")
- term.setTextColor(colors.white)
- return false
- end
- if not fs.exists(recipesConfig) then
- term.setTextColor(colors.red)
- print("Error getting recipes config. File not found:")
- print(recipesConfig)
- term.setTextColor(colors.white)
- return false
- end
- local file = fs.open(recipesConfig,"r")
- local contents = file.readAll()
- file.close()
- recipes = textutils.unserialize(contents)
- if recipes == nil then
- term.setTextColor(colors.red)
- print("Error getting recipes config. Parsing file failed")
- term.setTextColor(colors.white)
- return false
- end
- if #recipes == 0 then
- term.setTextColor(colors.red)
- print("Error getting recipes config. 0 recipes defined")
- term.setTextColor(colors.white)
- return false
- end
- for i,data in ipairs(recipes) do
- if data.result == nil or
- data.items == nil or
- type(data.items) ~= "table" or
- #data.items ~= 5 then
- term.setTextColor(colors.red)
- print("Error getting recipes config")
- print("Improper recipe def: item "..i)
- print("Proper recipe syntax:")
- term.setTextColor(colors.yellow)
- print("{")
- term.setTextColor(colors.cyan)
- term.write(" result")
- term.setTextColor(colors.yellow)
- term.write(" = ")
- term.setTextColor(colors.white)
- term.write("result item")
- term.setTextColor(colors.yellow)
- print(",")
- term.setTextColor(colors.cyan)
- term.write(" items")
- term.setTextColor(colors.yellow)
- print(" = {")
- term.setTextColor(colors.white)
- term.write(" empowerer item")
- term.setTextColor(colors.yellow)
- print(",")
- for n=1,4 do
- term.setTextColor(colors.white)
- term.write(" display stand "..n.." item")
- term.setTextColor(colors.yellow)
- if n < 4 then
- print(",")
- else
- print("")
- end
- end
- print(" }")
- print("}")
- term.setTextColor(colors.red)
- print("All items should be referenced by their display name")
- term.setTextColor(colors.white)
- return false
- end
- for j,name in ipairs(data.items) do
- if importantItems[name] == nil then
- importantItems[name] = {
- {
- recipeIndex=i,
- itemIndex=j
- }
- }
- else
- table.insert(importantItems[name],{
- recipeIndex=i,
- itemIndex=j
- })
- end
- end
- end
- return true
- end
- function run()
- local currentRecipe = nil
- --main loop
- while true do
- --seek valid recipe
- currentRecipe = nil
- while currentRecipe == nil do
- if logging then
- print("Clearing found items")
- end
- for i=1,#recipes do
- recipes[i].foundItems = {-1,-1,-1,-1,-1}
- end
- if logging then
- print("Searching Chest")
- end
- for i=1,chest.size() do
- local meta = chest.getItemMeta(i)
- if meta ~= nil and importantItems[meta.displayName] ~= nil then
- if logging then
- print("Found important item: "..meta.displayName)
- end
- for _,data in ipairs(importantItems[meta.displayName]) do
- recipes[data.recipeIndex].foundItems[data.itemIndex]=i
- end
- end
- end
- if logging then
- print("Checking for valid recipes")
- end
- for i=1,#recipes do
- local valid = true
- for j=1,5 do
- if recipes[i].foundItems[j] == -1 then
- valid = false
- break
- end
- end
- if valid then
- if logging then
- print("Found valid recipe")
- end
- currentRecipe = recipes[i]
- break
- end
- end
- end
- if logging then
- print("Moving Items")
- end
- --move items
- for i=1,5 do
- local perip = nil
- if i == 1 then
- perip = empowerer
- else
- perip = displayStands[i-1]
- end
- perip.pullItems(chestAddress,currentRecipe.foundItems[i])
- end
- --wait for recipe to finish
- if logging then
- print("Waiting for empowering to finish")
- end
- while empowerer.getItemMeta(1).displayName ~= currentRecipe.result do
- sleep(2)
- end
- if logging then
- print("Moving result back to chest")
- end
- empowerer.pushItems(chestAddress,1)
- end
- end
- function waitForKey()
- sleep(1)
- while true do
- local _,key = os.pullEvent("key")
- if key ~= keys.d then
- break
- end
- logging = not logging
- end
- end
- if init() then
- term.clear()
- term.setCursorPos(1,1)
- print("Press any key to terminate")
- parallel.waitForAny(run,waitForKey)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement