Advertisement
Shaka01

craftTurtle_mainProgram/startup

Feb 11th, 2023 (edited)
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.41 KB | None | 0 0
  1. --crafts items per request
  2. --requests are made by the stockpile brain
  3.  
  4. --change this to your turtle's name
  5. local target = "turtle_#"  
  6. --save with ctrl s
  7. --exit with ctrl e
  8. -----------------------------------
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. if target == "turtle_#" then
  22.     term.clear()
  23.     term.setCursorPos(1,1)
  24.     print("Please add name of turtle!")
  25.     sleep(2)
  26.     shell.run("edit", "startup")
  27.     os.reboot()
  28. end
  29.  
  30.  
  31. --initialize
  32. local craftingRecipes = {}
  33. local file = fs.open("craftingRecipes", "r")
  34. local recipeNames = {}
  35. mainItem = true
  36. if fs.exists("scrollItems") == false then
  37. shell.run("pastebin", "get", "s1F8FVrJ", "scrollItems")
  38. end
  39. if fs.exists("recipeRecorder") == false then
  40. shell.run("pastebin", "get", "JvsrKViR", "recipeRecorder")
  41. end
  42. if fs.exists("API") == false then
  43. shell.run("pastebin", "get", "EzkfU5ZM", "API")
  44. end
  45. shaka = require("API")
  46. avSlots = {5, 6, 7, 9, 10, 11, 13, 14, 15, 16}
  47.  
  48.  
  49. ---load recipes
  50. if file then
  51.   craftingRecipes = textutils.unserialize(file.readAll())
  52.   file.close()
  53. end
  54.  
  55. for k, v in pairs(craftingRecipes) do
  56.   table.insert(recipeNames, k)
  57. end
  58.  
  59. -- Connect peripherals
  60. local sides = {"left", "right", "top", "bottom", "front", "back"}
  61. for i, side in ipairs(sides) do
  62.     local device = peripheral.getType(side)
  63.     if device == "modem" then
  64.     modem = peripheral.wrap(side)
  65.     if modem.isWireless() then
  66.         wirelessModem = peripheral.wrap(side)
  67.         rednet.open(side)
  68.     else
  69.         wiredModem = peripheral.wrap(side)
  70.         rednet.close(side)
  71.     end
  72.     elseif device == "monitor" then
  73.         monitor = peripheral.wrap(side)
  74.     end
  75. end
  76.  
  77.  
  78. --initialize inventories
  79. targetInv = peripheral.wrap(target)
  80.  
  81. function tableContains(t, item)
  82.   for _, value in pairs(t) do
  83.     if value == item then
  84.       return true
  85.     end
  86.   end
  87.   return false
  88. end
  89.  
  90. --get computer id of brain
  91. if not fs.exists("brainID") then
  92.     rednet.broadcast("wassap", "giefIDplz")
  93.     local sender, msg, protocol = rednet.receive("disBeID", 1)
  94.     if msg then
  95.         shaka.writeFile("brainID", sender)
  96.         -- os.reboot()
  97.     else
  98.         term.clear()
  99.         term.setTextColor(colors.orange)
  100.         shaka.centerText("No Brain available!", 5)
  101.         term.setTextColor(colors.yellow)
  102.         shaka.centerText("Are you sure it is turned on?", 7)
  103.         os.pullEvent("key")
  104.         os.reboot()
  105.     end
  106. end
  107.  
  108. brain = shaka.readFile("brainID")
  109.  
  110.  
  111. function getInventories()
  112.   fromInventories = {}
  113.   if fs.exists("inventories") then
  114.     fromInventories = shaka.readFile("inventories")
  115.   end
  116.   local inventories = wiredModem.getNamesRemote()
  117.   for _, inventoryName in ipairs(inventories) do
  118.     if shaka.stringFind(inventoryName, "turtle") == false and inventoryName ~= nil and shaka.stringFind(inventoryName, "computer") == false then
  119.       if not tableContains(fromInventories, inventoryName) then
  120.         table.insert(fromInventories, inventoryName)
  121.       end
  122.     end
  123.   end
  124.   shaka.writeFile("inventories", fromInventories)
  125. end
  126.  
  127.  
  128. getInventories()
  129.  
  130.  
  131. local function scanInventory(itemName) -- returns the total amount of a certain item in all inventories
  132.     local items = {}
  133.     local tasks = {}
  134.     local tasks2 = {}
  135.     local invCounts = {}
  136.     local filteredInventories = {}
  137.    
  138.     for i = 1, #fromInventories do
  139.         tasks[#tasks+1] = function()
  140.             local invWrap = peripheral.wrap(fromInventories[i])
  141.             if invWrap == nil then
  142.                 return -- skip this inventory and move on to the next one
  143.             else
  144.                 table.insert(filteredInventories, fromInventories[i])
  145.             end
  146.            
  147.             local invCount = 0
  148.             for k, v in pairs(invWrap.list()) do
  149.                 tasks2[#tasks2+1] = function()
  150.                     if v.name == itemName then
  151.                         invCount = invCount + v.count
  152.                     end
  153.                 end
  154.             end
  155.            
  156.             parallel.waitForAll(unpack(tasks2))
  157.             invCounts[fromInventories[i]] = invCount
  158.             tasks2 = {} -- clear tasks2 after each inventory
  159.         end
  160.     end
  161.    
  162.     parallel.waitForAll(unpack(tasks))
  163.    
  164.     shaka.writeFile("inventories", filteredInventories)
  165.     fromInventories = shaka.readFile("inventories")
  166.    
  167.     for i = 1, #fromInventories do
  168.         if invCounts[fromInventories[i]] ~= nil then
  169.             items[itemName] = (items[itemName] or 0) + invCounts[fromInventories[i]]
  170.         end
  171.     end
  172.    
  173.     return items[itemName] or 0
  174. end
  175.  
  176.  
  177.  
  178. function findAndMoveItems(itemToSearch, toInventory, toInventorySlot, count)
  179.   local searchSuccess = false
  180.   local searchers = {}
  181.   local itemsMoved = 0  -- keep track of how many items have been moved so far
  182.   for i = 1, #fromInventories do
  183.     searchers[i] = function()
  184.       local invWrap = peripheral.wrap(fromInventories[i])
  185.       for slot, item in pairs(invWrap.list()) do
  186.         if item.name == itemToSearch then
  187.           local availableCount = item.count
  188.           if availableCount >= count - itemsMoved then
  189.             local movedItem = invWrap.pushItems(toInventory, slot, count - itemsMoved, toInventorySlot)
  190.             if movedItem > 0 then
  191.               itemsMoved = itemsMoved + movedItem  -- update itemsMoved
  192.               if itemsMoved == count then
  193.                 searchSuccess = true
  194.                 break
  195.               end
  196.             end
  197.           end
  198.         end
  199.       end
  200.     end
  201.   end
  202.  
  203.   for _, searcher in ipairs(searchers) do
  204.     parallel.waitForAny(searcher)
  205.     if searchSuccess then
  206.       break
  207.     end
  208.   end
  209. end
  210.  
  211.  
  212.  
  213.  
  214. function requestAndSortItems(craftingRequest)
  215.   local recipeName = craftingRequest
  216.   local recipe = craftingRecipes[recipeName]
  217.   local keyCount = recipe.keyCount
  218.   if makeSureItemsAvailable(recipeName) == false then
  219.     return false
  220.   end
  221.   local tasks = {}
  222.   for ingredient, ingredientData in pairs(recipe) do
  223.     if ingredient ~= "keyCount" then
  224.       for j, data in ipairs(ingredientData) do
  225.         tasks[#tasks+1] = function()
  226.           findAndMoveItems(ingredient, target, data.slot, 1)
  227.         end
  228.       end
  229.     end
  230.   end
  231.   parallel.waitForAll(unpack(tasks))
  232.   return true
  233. end
  234.  
  235.  
  236. function split(s, delimiter)
  237.     result = {};
  238.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  239.         table.insert(result, match);
  240.     end
  241.     return result;
  242. end
  243.  
  244. function findAvailableDrop(slot)
  245.   local tasks = {}
  246.   for i = 1, #fromInventories do
  247.     local invWrap = peripheral.wrap(fromInventories[i])
  248.     tasks[#tasks+1] = function()
  249.       invWrap.pullItems(target, slot)
  250.     end
  251.   end
  252.   parallel.waitForAll(unpack(tasks))
  253. end
  254.  
  255.  
  256. function dropInventory()
  257.   local tasks = {}
  258.   for i = 1, 16 do
  259.     if turtle.getItemCount(i) > 0 then
  260.       tasks[#tasks+1] = function()
  261.         repeat
  262.           findAvailableDrop(i)
  263.         until turtle.getItemCount(i) == 0
  264.       end
  265.     end
  266.   end
  267.   parallel.waitForAll(unpack(tasks))
  268. end
  269.  
  270.  
  271.  
  272. function broadcastItems(craftingRecipes) --- informs brain about all available crafting recipes
  273.   for itemName, itemData in pairs(craftingRecipes) do
  274.      rednet.send(brain, "saveItem " ..itemName, "stockpileupdate")
  275.   end
  276.     term.clear()
  277.     term.setTextColor(colors.green)
  278.     shaka.centerText("All recipes transmitted.", 5)
  279.     sleep(2)
  280.     os.reboot()
  281. end
  282.  
  283. function getRecipeIngredients(recipeName, craftingRecipes)
  284.   local ingredients = {}
  285.   local recipe = craftingRecipes[recipeName]
  286.  
  287.   for ingredientName, ingredientData in pairs(recipe) do
  288.     if ingredientName ~= "keyCount" then
  289.       for i, ingredient in ipairs(ingredientData) do
  290.         ingredients[ingredientName] = (ingredients[ingredientName] or 0) + ingredient.count
  291.       end
  292.     end
  293.   end
  294.   return ingredients
  295. end
  296.  
  297. function isItemAvailableAsRecipe(recipes, itemName)
  298.   for recipeName, recipe in pairs(recipes) do
  299.     if recipeName == itemName then
  300.       return true
  301.     end
  302.   end
  303.   return false
  304. end
  305.  
  306. function getKeyCount(itemName)
  307.  local recipe = craftingRecipes[itemName]
  308. if recipe ~= nil then
  309.   for ingredientName, ingredientData in pairs(recipe) do
  310.         if ingredientName == "keyCount" then
  311.             return ingredientData
  312.         end
  313.     end
  314. else
  315.     monitor.setTextScale(0.5)
  316.     monitor.setBackgroundColor(colors.red)
  317.     monitor.setTextColor(colors.black)
  318.     monitor.clear()
  319.     monitor.setCursorPos(1,1)
  320.     monitor.write("Recipe for " ..itemName)
  321.     monitor.setCursorPos(1,2)
  322.     monitor.write("is not known to me.")
  323.     monitor.setCursorPos(1, 4)
  324.     monitor.write("Please teach me or stop requesting.")
  325.     shell.run("recipeRecorder")
  326. end
  327.  
  328. end
  329.  
  330.  
  331. function makeSureItemsAvailable(item)
  332.   local wantedItem = getRecipeIngredients(item, craftingRecipes)
  333.   for key, value in pairs(wantedItem) do
  334.     message = scanInventory(key)
  335.     if tonumber(message) < value then
  336.       return false
  337.     end
  338.   end
  339.   return true
  340. end
  341.  
  342. function askItemAvailability(item)
  343.     local wantedItem = getRecipeIngredients(item, craftingRecipes)
  344.       for key, value in pairs(wantedItem) do
  345.         canItemBeCrafted = isItemAvailableAsRecipe(craftingRecipes, key)
  346.         message = scanInventory(key)
  347.         if tonumber(message) >= value  then
  348.         --skip
  349.         elseif canItemBeCrafted == true then
  350.             numberOfMissing = value - tonumber(message)
  351.             local keyCount = getKeyCount(key)
  352.             nextLine()
  353.             monitor.setTextColor(colors.orange)
  354.             monitor.setTextColor(colors.green)
  355.             monitor.setCursorPos(1, 4)
  356.             monitor.clearLine()
  357.             monitor.write("Missing ingredients:")
  358.             nextLine()
  359.             monitor.clearLine()
  360.             screenKey = string.match(key, ":(.*)")
  361.             screenKey = string.gsub(screenKey, "_", " ")
  362.             monitor.write("Crafting " ..screenKey.. ".")
  363.             if mainItem == true then
  364.                 missingMainItem = math.ceil(numberOfMissing/keyCount)
  365.             end
  366.             start = 1
  367.             craftReps = math.ceil(missingMainItem/keyCount)
  368.             for i = start, craftReps do
  369.                 if requestAndSortItems(key) == false then
  370.                     mainItem = false
  371.                     askItemAvailability(key)
  372.                     break
  373.                 else
  374.                     mainItem = true
  375.                 end
  376.                 turtle.craft()
  377.                 dropInventory()
  378.             end
  379.         else
  380.             clearScreen()
  381.             term.clear()
  382.             term.setCursorPos(1,1)
  383.             term.setTextColor(colors.red)
  384.             monitor.setTextColor(colors.yellow)
  385.             monitor.write(key)
  386.             nextLine()
  387.             monitor.setTextColor(colors.red)
  388.             monitor.write("not available.")
  389.             nextLine()
  390.             nextLine()
  391.             monitor.write("Can't craft ")
  392.             nextLine()
  393.             monitor.setTextColor(colors.yellow)
  394.             monitor.write(item.. ".")
  395.             rednet.send(brain, item, "cantCraft")
  396.             print("Item\n"..key.."\nnot available to craft\n" ..item.. ".")
  397.             sleep(3)
  398.             return false
  399.         end
  400.     end
  401.     return true
  402. end
  403.  
  404. function nextLine()
  405.     x, y = monitor.getCursorPos()
  406.     monitor.setCursorPos(1, y + 1)
  407. end
  408.  
  409. function clearScreen()
  410.     monitor.setCursorPos(1, 1)
  411.     monitor.clear()
  412. end
  413.  
  414. function keyExists(table, key)
  415.   for k, v in pairs(table) do
  416.     if k == key then
  417.       return true
  418.     end
  419.   end
  420.   return false
  421. end
  422.  
  423.  
  424.  
  425. function finishIt(itemToCraft)
  426.     if keyExists(craftingRecipes, itemToCraft) == false then
  427.         clearScreen()
  428.         monitor.setTextColor(colors.red)
  429.         monitor.write("No recipe saved for")
  430.         nextLine()
  431.         monitor.write(itemToCraft)
  432.         event, key = os.pullEvent("key")
  433.         os.reboot()
  434.     end
  435.     monitor.setCursorPos(1,1)
  436.     monitor.clearLine()
  437.     monitor.setTextColor(colors.yellow)
  438.     monitor.write("Crafting ")
  439.     screenItem = string.match(itemToCraft, ":(.*)")
  440.     screenItem = string.gsub(screenItem, "_", " ")
  441.     monitor.setTextColor(colors.lightBlue)
  442.     local keyInfoz = getKeyCount(itemToCraft)
  443.     monitor.write(screenItem.. ": " ..craftTracker * keyInfoz)
  444.     local craftPercent = 100 - math.ceil((craftTracker / math.ceil(countToCraft/divideBy)) * 100)
  445.     displayLoadingBar(monitor, craftPercent)
  446.     if askItemAvailability(itemToCraft) == false then --<----
  447.         return false
  448.     end
  449.     if makeSureItemsAvailable(itemToCraft) == true then
  450.         monitor.setCursorPos(1, 4)
  451.         monitor.clearLine()
  452.         monitor.setTextColor(colors.green)
  453.         monitor.write("Crafting requested item.")
  454.         nextLine()
  455.         monitor.clearLine()
  456.         requestAndSortItems(itemToCraft)
  457.         if turtle.craft() then
  458.             local item = turtle.getItemDetail(1)
  459.             if item ~= nil and item.name == itemToCraft then
  460.                 finishedForReal = true
  461.                 craftTracker = craftTracker - 1
  462.             else
  463.                 finishedForReal = false
  464.             end
  465.         end
  466.         dropInventory()
  467.     else
  468.         finishIt(itemToCraft)
  469.     end
  470.     return true
  471. end
  472.  
  473. function displayLoadingBar(monitor, percent)
  474.   monitor.setTextColor(colors.gray)
  475.   local width = monitor.getSize()
  476.   local barWidth = math.min(math.floor(width * percent / 100), width - 2)
  477.  
  478.         if percent >= 80 then
  479.             monitor.setTextColor(colors.lime)
  480.         elseif percent >= 70 then
  481.             monitor.setTextColor(colors.green)
  482.         elseif percent >= 40 then
  483.             monitor.setTextColor(colors.yellow)
  484.         elseif percent >= 15 then
  485.             monitor.setTextColor(colors.orange)
  486.         else
  487.             monitor.setTextColor(colors.red)
  488.         end
  489.  
  490.   monitor.setCursorPos(1,8)
  491.   monitor.write("[")
  492.   for i=1,barWidth do
  493.     monitor.write("=")
  494.   end
  495.   for i=barWidth+1,width-2 do
  496.     monitor.write(" ")
  497.   end
  498.   monitor.write("]")
  499.   monitor.setCursorPos(1, 9)
  500.   monitor.write(tostring(math.floor(percent)) .. "%")
  501. end
  502.  
  503.  
  504. function deleteRecipe(name)
  505. term.clear()
  506. term.setCursorPos(1,1)
  507. term.setTextColor(colors.white)
  508.   for itemName, recipe in pairs(craftingRecipes) do
  509.     -- print(itemName)
  510.     if string.find(itemName, name) then
  511.     term.setTextColor(colors.red)
  512.       print("Final confirmation for:\n")
  513.       term.setTextColor(colors.white)
  514.       print(itemName .. ".\n")
  515.             term.setTextColor(colors.orange)
  516.       print("\nDelete? (y/n)\n")
  517.       local response = io.read()
  518.       if response == "y" then
  519.         craftingRecipes[itemName] = nil
  520.         file = fs.open("craftingRecipes", "w")
  521.         file.write(textutils.serialize(craftingRecipes))
  522.         file.close()
  523.         term.setTextColor(colors.red)
  524.         local id = os.getComputerID()
  525.         local msg = table.concat({id, itemName, "0"}, " ")
  526.         rednet.send(brain, msg, "tableChange")
  527.         local sender, message, protocol = rednet.receive("tableConfirm", 1)
  528.         if message then
  529.             print("\nDeleted on brain and turtle.")
  530.         else
  531.             print("\nRecipe deleted on turtle only.")
  532.         end
  533.         sleep(2)
  534.         os.reboot()
  535.       else
  536.         term.setTextColor(colors.green)
  537.         print("Deletion cancelled.")
  538.         sleep(1)
  539.         os.reboot()
  540.       end
  541.     end
  542.   end
  543.   print("No recipe found with the given name.")
  544. end
  545.  
  546.  
  547. function search_item(item, tableName)
  548. term.clear()
  549. term.setCursorPos(1,1)
  550. term.setTextColor(colors.lightGray)
  551.   local file = fs.open("craftingRecipes", "r")
  552.   if file then
  553.     craftingRecipes = textutils.unserialize(file.readAll())
  554.     file.close()
  555.   end
  556.  
  557.   local matches = {}
  558.   for key, value in pairs(tableName) do
  559.     if string.find(key, item) then
  560.       table.insert(matches, key)
  561.     end
  562.   end
  563.  
  564.   if #matches == 0 then
  565.     print("Item not found.")
  566.     sleep(1)
  567.     os.reboot()
  568.     return nil
  569.   elseif #matches == 1 then
  570.     deleteRecipe(matches[1])
  571.     return value
  572.   else
  573.     term.setTextColor(colors.white)
  574.     print("Multiple items match the search criteria:")
  575.     term.setTextColor(colors.lightGray)
  576.     for i, match in ipairs(matches) do
  577.       print(i .. ". " .. match)
  578.     end
  579.     term.setTextColor(colors.orange)
  580.     print("Which item would you like to delete? (Enter the number)")
  581.     local choice = tonumber(read())
  582.     if choice and matches[choice] then
  583.       deleteRecipe(matches[choice])
  584.           os.reboot()
  585.       return tableName[matches[choice]]
  586.     else
  587.     term.setTextColor(colors.red)
  588.       print("Invalid choice.")
  589.       sleep(1)
  590.       os.reboot()
  591.       return nil
  592.     end
  593.   end
  594. end
  595.  
  596. function craftstuff()
  597.     if protocol == "stockpile" then
  598.         monitor.setTextScale(0.5)
  599.         availableItems = {}
  600.         dropInventory()
  601.         if finishIt(itemToCraft) == false then
  602.             return false
  603.         end
  604.         dropInventory()
  605.     end
  606.     return true
  607. end
  608.  
  609. function enterKey()
  610.     term.clear()
  611.     term.setCursorPos(1,1)
  612.     term.setTextColor(colors.lightGray)
  613.     print("Enter name of item to search for:\n")
  614.     term.setTextColor(colors.lime)
  615.     searchedItem = read()
  616.     searchedItem = string.gsub(searchedItem, "%s", "")
  617.     if searchedItem ~= "" then
  618.         search_item(searchedItem, craftingRecipes)
  619.     else
  620.         term.setTextColor(colors.red)
  621.         print("Search can't be empty. Try again.")
  622.         sleep(1)
  623.         enterKey()
  624.     end
  625. end
  626.  
  627. function centerText(monitor, y, text)
  628.   local w, h = monitor.getSize()
  629.   local x = math.floor((w - string.len(text) + 2) / 2)
  630.   monitor.setCursorPos(x , y)
  631.   print(text)
  632. end
  633.  
  634. function manualSortTable(t)
  635.   local selected = 1
  636.   local quit = false
  637.  
  638.   while not quit do
  639.     -- clear screen and print current state of table
  640.     term.clear()
  641.     term.setCursorPos(1, 1)
  642.     shaka.changeColors(colors.gray, colors.white)
  643.     term.clearLine()
  644.     print("Deselect[Enter], Save['S'], Reset['R']")
  645.     shaka.changeColors(colors.black, colors.white)
  646.     term.setCursorPos(1, 3)
  647.     for i, v in ipairs(t) do
  648.       if i == selected then
  649.         term.setBackgroundColor(colors.yellow)
  650.         term.setTextColor(colors.black)
  651.         print(i.."> " .. v)
  652.         term.setBackgroundColor(colors.black)
  653.         term.setTextColor(colors.white)
  654.       else
  655.         print(i.."  " .. v)
  656.       end
  657.     end
  658.  
  659.     -- wait for user input
  660.     local event, key = os.pullEvent("key")
  661.     if key == keys.enter then
  662.       if selected ~= nil then
  663.         while selected do
  664.           -- redraw the table with the current selection
  665.           term.clear()
  666.           term.setCursorPos(1, 1)
  667.             shaka.changeColors(colors.gray, colors.white)
  668.             term.clearLine()
  669.             print("Select[Enter], Save['S'], Reset['R']")
  670.             shaka.changeColors(colors.black, colors.white)
  671.             term.setCursorPos(1, 3)
  672.           for i, v in ipairs(t) do
  673.             if i == selected then
  674.                 term.setBackgroundColor(colors.black)
  675.                 term.setTextColor(colors.yellow)
  676.               print(i.."> " .. v)
  677.               term.setTextColor(colors.white)
  678.             else
  679.               print(i.."  " .. v)
  680.             end
  681.           end
  682.           -- wait for user input to move the selection
  683.           local event2, key2 = os.pullEvent("key")
  684.           if key2 == keys.up then
  685.             if selected > 1 then
  686.               selected = selected - 1
  687.             end
  688.           elseif key2 == keys.down then
  689.             if selected < #t then
  690.               selected = selected + 1
  691.             end
  692.           elseif key2 == keys.enter then
  693.             -- break out of the selection loop
  694.             break
  695.           elseif key2 == keys.s then
  696.             return
  697.           elseif key2 == keys.r then
  698.             fs.delete("inventories")
  699.             getInventories()
  700.             manualSortTable(fromInventories)
  701.             return
  702.           end
  703.         end
  704.       end
  705.     elseif key == keys.up then
  706.       -- move selection up
  707.       if selected and selected > 1 then
  708.         t[selected], t[selected - 1] = t[selected - 1], t[selected]
  709.         selected = selected - 1
  710.       end
  711.     elseif key == keys.down then
  712.       -- move selection down
  713.       if selected and selected < #t then
  714.         t[selected], t[selected + 1] = t[selected + 1], t[selected]
  715.         selected = selected + 1
  716.       end
  717.     elseif key == keys.s then
  718.       -- quit the function
  719.       quit = true
  720.     elseif key == keys.r then
  721.         fs.delete("inventories")
  722.     getInventories()
  723.     manualSortTable(fromInventories)
  724.     return
  725.     end
  726.   end
  727. end
  728.  
  729.  
  730. function showMenuOptions()
  731. term.clear()
  732. term.setCursorPos(1,1)
  733. shaka.changeColors(colors.lightGray, colors.black)
  734. term.clearLine()
  735. print("'Enter' to search for known recipes.\n")
  736. shaka.changeColors(colors.black, colors.white)
  737. print("'C' to save a new crafting recipe.\n")
  738. term.setTextColor(colors.gray)
  739. print("'U' to send all known recipes to brain.\n")
  740. print("'O' to change the access order of connected inventories.")
  741. end
  742.  
  743. showMenuOptions()
  744.  
  745. while true do
  746.     monitor.setTextScale(1)
  747.     monitor.setBackgroundColor(colors.black)
  748.     clearScreen()
  749.     monitor.setTextColor(colors.gray)
  750.     monitor.write("Currently waiting")
  751.     nextLine()
  752.     monitor.write("for requests.")
  753.  
  754.     eventData = {os.pullEvent()}
  755.     event = eventData[1]
  756.     key = eventData[2] -- computer id
  757.     message = eventData[3]
  758.     protocol = eventData[4]
  759.     if event == "rednet_message" and key == brain then
  760.             if protocol == "stockpile" then
  761.             split(message, " ")
  762.             itemToCraft = tostring(result[1])
  763.             countToCraft = tonumber(result[2])
  764.             wantedTotalCount = tonumber(result[3])
  765.             divideBy = getKeyCount(itemToCraft)
  766.             craftTracker = math.ceil(countToCraft/divideBy)
  767.             term.clear()
  768.             term.setCursorPos(1, 1)
  769.             centerText(term, 5, "Menu currently not available.")
  770.             centerText(term, 6, "Busy doing stuff.")
  771.             for i = 1, math.ceil(countToCraft/divideBy) do
  772.                 if craftstuff() == false then
  773.                     break
  774.                 end
  775.                 if scanInventory(itemToCraft) >= wantedTotalCount then
  776.                     break
  777.                 end
  778.             end
  779.             showMenuOptions()
  780.             end
  781.     elseif event == "key" then
  782.         monitor.setTextScale(1)
  783.         clearScreen()
  784.         monitor.setTextColor(colors.gray)
  785.         monitor.write("Idling.")
  786.         nextLine()
  787.         monitor.clearLine()
  788.         if key == keys.s then
  789.             shell.run("scrollItems")
  790.         elseif key == keys.c then
  791.             shell.run("recipeRecorder")
  792.         elseif key == keys.u then
  793.             broadcastItems(craftingRecipes)
  794.         elseif key == keys.enter then
  795.             enterKey()
  796.         elseif key == keys.o then
  797.             manualSortTable(fromInventories)
  798.             shaka.writeFile("inventories", fromInventories)
  799.             os.reboot()
  800.         end
  801.     elseif event == "mouse_click" then
  802.         if protocol == 1 then
  803.             enterKey()
  804.         elseif protocol == 100 then
  805.             shell.run("scrollItems")
  806.         elseif protocol == 3 then
  807.             shell.run("recipeRecorder")
  808.         elseif protocol == 5 then
  809.             broadcastItems(craftingRecipes)
  810.         elseif protocol == 7 or protocol == 8 then
  811.             manualSortTable(fromInventories)
  812.             shaka.writeFile("inventories", fromInventories)
  813.             os.reboot()    
  814.         end
  815.     end
  816. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement