Advertisement
Shaka01

craftTurtle_mainProgram/startup NEW

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