Advertisement
JereTheJuggler

Untitled

Dec 13th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local recipesConfig = shell.resolve("").."/empowererRecipes.config"
  4. local empowerer = nil
  5. local chest = nil
  6. local chestAddress = nil
  7. local displayStands = {}
  8.  
  9. local recipes = {}
  10.  
  11. local logging = false
  12.  
  13. local importantItems = {}
  14.  
  15. function init()
  16.     for _,name in ipairs(peripheral.getNames()) do
  17.         if name ~= "top" and name ~= "left" and name ~= "right" and name ~= "bottom" and name ~= "front" and name ~= "back" then
  18.             local t = peripheral.getType(name)
  19.             if t == "actuallyadditions:empowerer" then
  20.                 empowerer = peripheral.wrap(name)
  21.             elseif t == "actuallyadditions:displaystand" then
  22.                 table.insert(displayStands,peripheral.wrap(name))
  23.             elseif t == "minecraft:chest" then
  24.                 chest = peripheral.wrap(name)
  25.                 chestAddress = name
  26.             end
  27.         end
  28.     end
  29.     if empowerer == nil or
  30.        chest == nil or
  31.        #displayStands ~= 4 then
  32.         term.setTextColor(colors.red)
  33.         print("Error setting up peripherals. Required peripherals are:")
  34.         print("1 x Empowerer")
  35.         print("1 x Chest")
  36.         print("4 x Display Stand")
  37.         term.setTextColor(colors.white)
  38.         return false
  39.     end
  40.     if not fs.exists(recipesConfig) then
  41.         term.setTextColor(colors.red)
  42.         print("Error getting recipes config. File not found:")
  43.         print(recipesConfig)
  44.         term.setTextColor(colors.white)
  45.         return false
  46.     end
  47.     local file = fs.open(recipesConfig,"r")
  48.     local contents = file.readAll()
  49.     file.close()
  50.     recipes = textutils.unserialize(contents)
  51.     if recipes == nil then
  52.         term.setTextColor(colors.red)
  53.         print("Error getting recipes config. Parsing file failed")
  54.         term.setTextColor(colors.white)
  55.         return false
  56.     end
  57.     if #recipes == 0 then
  58.         term.setTextColor(colors.red)
  59.         print("Error getting recipes config. 0 recipes defined")
  60.         term.setTextColor(colors.white)
  61.         return false
  62.     end
  63.     for i,data in ipairs(recipes) do
  64.         if data.result == nil or
  65.            data.items == nil or
  66.            type(data.items) ~= "table" or
  67.            #data.items ~= 5 then
  68.             term.setTextColor(colors.red)
  69.             print("Error getting recipes config")
  70.             print("Improper recipe def: item "..i)
  71.             print("Proper recipe syntax:")
  72.             term.setTextColor(colors.yellow)
  73.             print("{")
  74.             term.setTextColor(colors.cyan)
  75.             term.write("    result")
  76.             term.setTextColor(colors.yellow)
  77.             term.write(" = ")
  78.             term.setTextColor(colors.white)
  79.             term.write("result item")
  80.             term.setTextColor(colors.yellow)
  81.             print(",")
  82.             term.setTextColor(colors.cyan)
  83.             term.write("    items")
  84.             term.setTextColor(colors.yellow)
  85.             print(" = {")
  86.             term.setTextColor(colors.white)
  87.             term.write("        empowerer item")
  88.             term.setTextColor(colors.yellow)
  89.             print(",")
  90.             for n=1,4 do
  91.                 term.setTextColor(colors.white)
  92.                 term.write("        display stand "..n.." item")
  93.                 term.setTextColor(colors.yellow)
  94.                 if n < 4 then
  95.                     print(",")
  96.                 else
  97.                     print("")
  98.                 end
  99.             end
  100.             print("    }")
  101.             print("}")
  102.             term.setTextColor(colors.red)
  103.             print("All items should be referenced by their display name")
  104.             term.setTextColor(colors.white)
  105.             return false
  106.         end
  107.         for j,name in ipairs(data.items) do
  108.             if importantItems[name] == nil then
  109.                 importantItems[name] = {
  110.                     {
  111.                         recipeIndex=i,
  112.                         itemIndex=j
  113.                     }
  114.                 }
  115.             else
  116.                 table.insert(importantItems[name],{
  117.                     recipeIndex=i,
  118.                     itemIndex=j
  119.                 })
  120.             end
  121.         end
  122.     end
  123.     return true
  124. end
  125.  
  126. function run()
  127.     local currentRecipe = nil
  128.     --main loop
  129.     while true do
  130.         --seek valid recipe
  131.         currentRecipe = nil
  132.         while currentRecipe == nil do
  133.             if logging then
  134.                 print("Clearing found items")
  135.             end
  136.             for i=1,#recipes do
  137.                 recipes[i].foundItems = {-1,-1,-1,-1,-1}
  138.             end
  139.             if logging then
  140.                 print("Searching Chest")
  141.             end
  142.             for i=1,chest.size() do
  143.                 local meta = chest.getItemMeta(i)
  144.                 if meta ~= nil and importantItems[meta.displayName] ~= nil then
  145.                     if logging then
  146.                         print("Found important item: "..meta.displayName)
  147.                     end
  148.                     for _,data in ipairs(importantItems[meta.displayName]) do
  149.                         recipes[data.recipeIndex].foundItems[data.itemIndex]=i
  150.                     end
  151.                 end
  152.             end
  153.             if logging then
  154.                 print("Checking for valid recipes")
  155.             end
  156.             for i=1,#recipes do
  157.                 local valid = true
  158.                 for j=1,5 do
  159.                     if recipes[i].foundItems[j] == -1 then
  160.                         valid = false
  161.                         break
  162.                     end
  163.                 end
  164.                 if valid then
  165.                     if logging then
  166.                         print("Found valid recipe")
  167.                     end
  168.                     currentRecipe = recipes[i]
  169.                     break
  170.                 end
  171.             end
  172.         end
  173.         if logging then
  174.             print("Moving Items")
  175.         end
  176.         --move items
  177.         for i=1,5 do
  178.             local perip = nil
  179.             if i == 1 then
  180.                 perip = empowerer
  181.             else
  182.                 perip = displayStands[i-1]
  183.             end
  184.             perip.pullItems(chestAddress,currentRecipe.foundItems[i])
  185.         end
  186.         --wait for recipe to finish
  187.         if logging then
  188.             print("Waiting for empowering to finish")
  189.         end
  190.         while empowerer.getItemMeta(1).displayName ~= currentRecipe.result do
  191.             sleep(2)
  192.         end
  193.         if logging then
  194.             print("Moving result back to chest")
  195.         end
  196.         empowerer.pushItems(chestAddress,1)
  197.     end
  198. end
  199.  
  200. function waitForKey()
  201.     sleep(1)
  202.     while true do
  203.         local _,key = os.pullEvent("key")
  204.         if key ~= keys.d then
  205.             break
  206.         end
  207.         logging = not logging
  208.     end
  209. end
  210.  
  211. if init() then
  212.     term.clear()
  213.     term.setCursorPos(1,1)
  214.     print("Press any key to terminate")
  215.     parallel.waitForAny(run,waitForKey)
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement