Advertisement
Shaka01

smart stockpile additem - automatic dependancy download

Feb 2nd, 2023 (edited)
761
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.95 KB | None | 0 0
  1. shaka = require("API")
  2. fromInventories = shaka.readFile("inventories")
  3. resetTimer = 0
  4. disableMouse = false
  5.  
  6. -- Connect peripherals
  7. local sides = {"left", "right", "top", "bottom", "front", "back"}
  8. for i, side in ipairs(sides) do
  9.   local device = peripheral.getType(side)
  10.   if device == "modem" then
  11.     rednet.open(side)
  12. elseif device == "monitor" then
  13.     monitor = peripheral.wrap(side)
  14.   elseif device then
  15.     local wrapper = peripheral.wrap(side)
  16.   end
  17. end
  18.  
  19. -- Load the wanted items from the file
  20. if fs.exists("wanteditems.lua") then
  21. wantedItems = dofile("wanteditems.lua")
  22. else
  23. wantedItems = {}
  24. local file = fs.open("wanteditems.lua", "w")
  25. file.write("return {}")
  26. file.close()
  27. end
  28.  
  29. monitor.clear()
  30. monitor.setTextScale(0.5)
  31. window = shaka.centerWindow(30, 10, colors.gray, colors.yellow, monitor)
  32. shaka.centerText("Currently in menu, idling.", 5, window)
  33.  
  34. -- Function to add an item to the wanted items table
  35. function addItem()
  36.   sleep(0.2)
  37.   resetScreen()
  38.   term.setTextColor(colors.yellow)
  39.   print("Add item to wanted items:")
  40.   term.setTextColor(colors.white)
  41.  
  42.   print("Enter computer ID:")
  43.   local id = tonumber(read())
  44.  
  45.   if not id or id < 1 then
  46.     resetScreen()
  47.     term.setTextColor(colors.red)
  48.     print("Invalid computer ID.")
  49.     term.setTextColor(colors.gray)
  50.     print("\n\nPress any key to try again..")
  51.     term.setTextColor(colors.white)
  52.     os.pullEvent("key")
  53.     return
  54.   end
  55.  
  56.   print("Enter item name:")
  57.   local itemName = read()
  58.  
  59.   if not itemName or itemName == "" then
  60.     resetScreen()
  61.     term.setTextColor(colors.red)
  62.     print("Item name must not be empty.")
  63.     term.setTextColor(colors.gray)
  64.     print("\n\nPress any key to try again..")
  65.     term.setTextColor(colors.white)
  66.     os.pullEvent("key")
  67.     return
  68.   end
  69.  
  70.   print("Enter item count:")
  71.   local itemCount = tonumber(read())
  72.  
  73.   if not itemCount or itemCount < 1 then
  74.     resetScreen()
  75.     term.setTextColor(colors.red)
  76.     print("Invalid item count.")
  77.     term.setTextColor(colors.gray)
  78.     print("\n\nPress any key to try again..")
  79.     term.setTextColor(colors.white)
  80.     os.pullEvent("key")
  81.     return
  82.   end
  83.  
  84.   if not wantedItems[id] then
  85.     wantedItems[id] = {}
  86.   end
  87.     -- Check if item already exists for this computer ID
  88.   for _, v in ipairs(wantedItems[id]) do
  89.     if v[1] == itemName then
  90.       term.setTextColor(colors.red)
  91.       textutils.slowPrint("Item "..itemName.. " already exists for this ID.")
  92.       sleep(1)
  93.       return false
  94.     end
  95.   end
  96.  
  97.   table.insert(wantedItems[id], { itemName, itemCount })
  98.   saveWantedItems()
  99.   term.setTextColor(colors.green)
  100.   shaka.nextLine()
  101.   textutils.slowPrint("Added "..itemName)
  102.   sleep(1)
  103. end
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. -- Function to save the wanted items
  112. function saveWantedItems()
  113.   for id, items in pairs(wantedItems) do
  114.     for _, item in ipairs(items) do
  115.       if not id or not item[1] or not item[2] then
  116.         resetScreen()
  117.         term.setTextColor(colors.red)
  118.         print("Computer ID, item name, and item count must not be empty.")
  119.         term.setTextColor(colors.gray)
  120.         print("\n\nPress any key to try again..")
  121.         term.setTextColor(colors.white)
  122.         event, key = os.pullEvent("key")
  123.         os.reboot()
  124.       end
  125.     end
  126.   end
  127.   file = fs.open("wanteditems.lua", "w")
  128.   file.write("return {\n")
  129.   for id, items in pairs(wantedItems) do
  130.     file.write((" [%d] = {\n"):format(id))
  131.     for _, item in ipairs(items) do
  132.       file.write((" {%q, %d},\n"):format(item[1], item[2]))
  133.     end
  134.     file.write(" },\n")
  135.   end
  136.   file.write("}\n")
  137.   file.close()
  138. end
  139.  
  140.  
  141. function viewWantedItems()
  142.     local sortedIDs = {}
  143.     for id in pairs(wantedItems) do
  144.         table.insert(sortedIDs, id)
  145.     end
  146.     table.sort(sortedIDs)
  147.  
  148.     for _, id in ipairs(sortedIDs) do
  149.         local items = wantedItems[id]
  150.         local screensize, width = term.getSize()
  151.         screensize = width - 5
  152.         local pages = math.ceil(#items / screensize)
  153.         for page = 1, pages do
  154.             term.clear()
  155.             term.setCursorPos(1,1)
  156.             term.setTextColor(colors.yellow)
  157.             print("Wanted items:")
  158.             term.setTextColor(colors.lightGray)
  159.             print(("Computer ID: %d"):format(id))
  160.             term.setTextColor(colors.white)
  161.             for i = (page - 1) * screensize + 1, math.min(page * screensize, #items) do
  162.                 local item = items[i]
  163.                 print(("%d. %d x %s"):format(i, item[2], item[1]))
  164.             end
  165.             term.setTextColor(colors.gray)
  166.             print("Enter item number to edit (space to skip):")
  167.             term.setTextColor(colors.white)
  168.             local event, key = os.pullEvent("key")
  169.             if key ~= keys.space then
  170.                 local itemNum = tonumber(read())
  171.                 if itemNum == nil then
  172.                     term.setTextColor(colors.red)
  173.                     shaka.nextLine()
  174.                     textutils.slowPrint("Invalid input..")
  175.                     sleep(1)                   
  176.                     os.reboot()
  177.                 end
  178.  
  179.                 if itemNum > 0 and itemNum <= #items then
  180.                     local item = items[itemNum]
  181.                     print("Enter new item count or 0 to delete:")
  182.                     local newCount = tonumber(read())
  183.                     if newCount == 0 then
  184.                         table.remove(items, itemNum)
  185.                           term.setTextColor(colors.red)
  186.                           shaka.nextLine()
  187.                           textutils.slowPrint("Removed " ..item[1])
  188.                           sleep(1)
  189.                           saveWantedItems()
  190.                           return
  191.                     else
  192.                           items[itemNum][2] = newCount
  193.                           term.setTextColor(colors.green)
  194.                           shaka.nextLine()
  195.                           textutils.slowPrint("Changed "..item[1].. " to "..newCount)
  196.                           sleep(1)
  197.                           saveWantedItems()
  198.                           return
  199.                     end
  200.                 else
  201.                     term.setTextColor(colors.red)
  202.                     shaka.nextLine()
  203.                     textutils.slowPrint("Invalid input..")
  204.                     sleep(1)                   
  205.                     os.reboot()            
  206.                 end
  207.             end
  208.         end
  209.     end
  210. end
  211.  
  212. function displayLoadingBar(monitor, percent)
  213.   monitor.setTextColor(colors.gray)
  214.   monitor.setBackgroundColor(colors.black)
  215.   local width = monitor.getSize()
  216.   local barWidth = math.min(math.floor(width * percent / 100), width - 2)
  217.  
  218.         if percent >= 80 then
  219.             monitor.setTextColor(colors.lime)
  220.         elseif percent >= 70 then
  221.             monitor.setTextColor(colors.green)
  222.         elseif percent >= 40 then
  223.             monitor.setTextColor(colors.yellow)
  224.         elseif percent >= 15 then
  225.             monitor.setTextColor(colors.orange)
  226.         else
  227.             monitor.setTextColor(colors.red)
  228.         end
  229.  
  230.   monitor.setCursorPos(1,8)
  231.   monitor.write("[")
  232.   for i=1,barWidth do
  233.     monitor.write("=")
  234.   end
  235.   for i=barWidth+1,width-2 do
  236.     monitor.write(" ")
  237.   end
  238.   monitor.write("]")
  239.   monitor.setCursorPos(1, 9)
  240.   monitor.clearLine()
  241.   monitor.write(tostring(math.floor(percent)) .. "%")
  242. end
  243.  
  244. function defragmentInventories()
  245.     for _, invName in ipairs(fromInventories) do
  246.         term.clear()
  247.         term.setCursorPos(1,1)
  248.         local invWrap = peripheral.wrap(invName)
  249.         local inventory = invWrap.list()
  250.         shaka.changeColors(colors.green, colors.black)
  251.         term.clearLine()
  252.         print("Defragging inventory " .._.. " of " ..#fromInventories)
  253.         monitor.setCursorPos(1,1)
  254.         monitor.clear()
  255.         -- monitor.setBackgroundColor(colors.green)
  256.         shaka.changeColors(colors.green, colors.black, monitor)
  257.         monitor.clearLine()
  258.         monitor.write("Defragging inventory " .._.. " of " ..#fromInventories)
  259.         for slot, item in pairs(inventory) do
  260.             local percentage = (slot/#inventory) * 100
  261.             displayLoadingBar(term, percentage)
  262.             displayLoadingBar(monitor, percentage)
  263.             prettyInv = shaka.prettyName(invName)
  264.             prettyName = shaka.prettyName(item.name)
  265.             for i = 1, #fromInventories do
  266.                 prettyTarget = shaka.prettyName(fromInventories[i])
  267.                 ---monitor
  268.                 monitor.setCursorPos(1, 3)
  269.                 shaka.changeColors(colors.lightBlue, colors.black, monitor)
  270.                 monitor.clearLine()
  271.                 monitor.write("Source inv: "..prettyInv)
  272.                 shaka.nextLine(monitor)
  273.                 shaka.changeColors(colors.yellow, colors.black, monitor)
  274.                 monitor.clearLine()
  275.                 monitor.write("Moving item: "..prettyName)
  276.                 monitor.setCursorPos(1, 5)
  277.                 shaka.changeColors(colors.orange, colors.black, monitor)
  278.                 monitor.clearLine()
  279.                 monitor.write("Target inv: "..prettyTarget)
  280.                 --terminal
  281.                 term.setCursorPos(1, 3)
  282.                 shaka.changeColors(colors.lightBlue, colors.black)
  283.                 term.clearLine()
  284.                 term.write("Source inv: "..prettyInv)
  285.                 shaka.nextLine()
  286.                 shaka.changeColors(colors.yellow, colors.black)
  287.                 term.clearLine()
  288.                 term.write("Moving item: "..prettyName)
  289.                 term.setCursorPos(1, 5)
  290.                 shaka.changeColors(colors.orange, colors.black)
  291.                 term.clearLine()
  292.                 term.write("Target inv: "..prettyTarget)
  293.                 moved = invWrap.pushItems(fromInventories[i], slot)
  294.                 if moved ~= 0 then
  295.                     break
  296.                 end
  297.             end
  298.         end
  299.         shaka.changeColors(colors.black, colors.white)
  300.         shaka.changeColors(colors.black, colors.white, monitor)
  301.     end
  302. end
  303.  
  304. function manualSortTable(t)
  305.   local selected = 1
  306.   local quit = false
  307.  
  308.   while not quit do
  309.     -- clear screen and print current state of table
  310.     term.clear()
  311.     term.setCursorPos(1,1)
  312.     shaka.changeColors(colors.gray, colors.white)
  313.     term.clearLine()
  314.     print("Choose inventory order: Deselect[Enter], Save['S']")
  315.     shaka.changeColors(colors.black, colors.white)
  316.     term.setCursorPos(1, 3)
  317.     for i, v in ipairs(t) do
  318.       if i == selected then
  319.         term.setBackgroundColor(colors.yellow)
  320.         term.setTextColor(colors.black)
  321.         print(i.."> " .. v)
  322.         term.setBackgroundColor(colors.black)
  323.         term.setTextColor(colors.white)
  324.       else
  325.         print(i.."  " .. v)
  326.       end
  327.     end
  328.  
  329.     -- wait for user input
  330.     local event, key = os.pullEvent("key")
  331.     if key == keys.enter then
  332.       if selected ~= nil then
  333.         while selected do
  334.           -- redraw the table with the current selection
  335.           term.clear()
  336.           term.setCursorPos(1, 1)
  337.             shaka.changeColors(colors.gray, colors.white)
  338.             term.clearLine()
  339.             print("Choose inventory order: Select[Enter], Save['S']")
  340.             shaka.changeColors(colors.black, colors.white)
  341.             term.setCursorPos(1, 3)
  342.           for i, v in ipairs(t) do
  343.             if i == selected then
  344.                 term.setBackgroundColor(colors.black)
  345.                 term.setTextColor(colors.yellow)
  346.               print(i.."> " .. v)
  347.               term.setTextColor(colors.white)
  348.             else
  349.               print(i.."  " .. v)
  350.             end
  351.           end
  352.           -- wait for user input to move the selection
  353.           local event2, key2 = os.pullEvent("key")
  354.           if key2 == keys.up then
  355.             if selected > 1 then
  356.               selected = selected - 1
  357.             end
  358.           elseif key2 == keys.down then
  359.             if selected < #t then
  360.               selected = selected + 1
  361.             end
  362.           elseif key2 == keys.enter then
  363.             -- break out of the selection loop
  364.             break
  365.           elseif key2 == keys.s then
  366.             return
  367.           end
  368.         end
  369.       end
  370.     elseif key == keys.up then
  371.       -- move selection up
  372.       if selected and selected > 1 then
  373.         t[selected], t[selected - 1] = t[selected - 1], t[selected]
  374.         selected = selected - 1
  375.       end
  376.     elseif key == keys.down then
  377.       -- move selection down
  378.       if selected and selected < #t then
  379.         t[selected], t[selected + 1] = t[selected + 1], t[selected]
  380.         selected = selected + 1
  381.       end
  382.     elseif key == keys.s then
  383.       -- quit the function
  384.       quit = true
  385.     end
  386.   end
  387. end
  388.  
  389.  
  390. -- User interface to add an item
  391. function menu(eventName)
  392.     shaka.changeColors(colors.black, colors.white)
  393.     term.clear()
  394.     term.setTextColor(colors.white)
  395.     local numberedOptions = 1
  396.     for i = 5, 13, 2 do
  397.         if i == 5 then numberedOptions = 1
  398.         elseif i == 7 then numberedOptions = 2
  399.         elseif i == 9 then numberedOptions = 3
  400.         elseif i == 11 then numberedOptions = 4
  401.         elseif i == 13 then numberedOptions = 5
  402.         end
  403.         term.setCursorPos(1, i)
  404.         term.setBackgroundColor(colors.lightGray)
  405.         term.clearLine()
  406.         term.write(numberedOptions..".")
  407.     end
  408.     term.setTextColor(colors.black)
  409.     shaka.centerText("Manually add new item.", 5)
  410.     shaka.centerText("Computer ID overview.", 7)
  411.     shaka.centerText("Search for a specific item.", 9)
  412.     shaka.centerText("Return to main menu.", 11)
  413.     shaka.centerText("Defragment inventories.", 13)
  414.    
  415.     shaka.changeColors(colors.black, colors.gray)
  416.     term.setCursorPos(1,1)
  417.     print("\nClick your choice or enter number.")
  418.     event, button, x, choice = os.pullEvent(eventName)
  419.  
  420.     if event == "key" then
  421.         if button == keys.one then choice = "add"
  422.         elseif button == keys.two then choice = "view"
  423.         elseif button == keys.three then choice = "search"
  424.         elseif button == keys.four then choice = "reboot"
  425.         elseif button == keys.five then choice = "defrag"
  426.         end
  427.     end
  428.    
  429.     if event == "mouse_click" then
  430.         if choice == 5 then choice = "add"
  431.         elseif choice == 7 then choice = "view"
  432.         elseif choice == 9 then choice = "search"
  433.         elseif choice == 11 then choice = "reboot"
  434.         elseif choice == 13 then choice = "defrag"
  435.         end
  436.     end
  437.  
  438.     if event == "mouse_click" or event == "key" then
  439.         if choice == "add" then
  440.             addItem()
  441.         elseif choice == "view" then
  442.             viewWantedItems()
  443.         elseif choice == "search" then
  444.             searchItem()
  445.         elseif choice == "reboot" then
  446.             os.reboot()
  447.         elseif choice == "defrag" then
  448.             shaka.clearScreen()
  449.             shaka.changeColors(colors.gray, colors.white)
  450.             term.clear()
  451.             shaka.centerText("Please choose priority of inventories.", 5)
  452.             shaka.centerText("Items will first try to go to 1, then 2, etc..", 7)
  453.             shaka.centerText("Press any key to continue..", 10)
  454.             os.pullEvent("key")
  455.             term.setBackgroundColor(colors.black)
  456.             manualSortTable(fromInventories)
  457.             defragmentInventories()
  458.             shaka.changeColors(colors.black,colors.green)
  459.             print("\nDone!")
  460.             shaka.writeFile("inventories", fromInventories)
  461.             sleep(1)
  462.             os.reboot()
  463.         else
  464.             if choice == nil then choice = button end
  465.           print("Invalid selection("..choice..").")
  466.           sleep(1)
  467.         end
  468.     end
  469. end
  470.  
  471. function resetScreen()
  472. term.clear()
  473. term.setCursorPos(1,1)
  474. end
  475.  
  476. function searchItem()
  477.     resetScreen()
  478.     term.setTextColor(colors.yellow)
  479.     print("Enter item name or item count:\n")
  480.     term.setTextColor(colors.lime)
  481.     sleep(0.1)
  482.     local searchTerm = read()
  483.     if searchTerm == "" then
  484.         term.setTextColor(colors.red)
  485.         print("Cancelling.")
  486.         sleep(1)
  487.         return
  488.     end
  489.     found = {}
  490.     if tonumber(searchTerm) ~= nil then -- search by item count
  491.         for i, wantedItem in pairs(wantedItems) do
  492.             for k, v in pairs(wantedItem) do
  493.                 if v[2] == tonumber(searchTerm) then
  494.                     table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
  495.                 end
  496.             end
  497.         end
  498.     else -- search by item name
  499.         for i, wantedItem in pairs(wantedItems) do
  500.             for k, v in pairs(wantedItem) do
  501.                 if string.find(string.lower(v[1]), string.lower(searchTerm)) then
  502.                     table.insert(found, {computerID = i, itemName = v[1], itemCount = v[2]})
  503.                 end
  504.             end
  505.         end
  506.     end
  507.     term.setTextColor(colors.white)
  508.     if #found == 0 then
  509.         term.setTextColor(colors.red)
  510.         print("Item not in stockpile list.")
  511.         sleep(1)
  512.     elseif #found == 1 then
  513.         local item = found[1]
  514.         resetScreen()
  515.         term.setTextColor(colors.white)
  516.         print(("ID%d: %s x %d"):format(item.computerID, item.itemName, item.itemCount))
  517.         term.setTextColor(colors.lightGray)
  518.         print("\nEnter new item count or 0 to delete:\n")
  519.         term.setTextColor(colors.white)
  520.         local newCount = tonumber(read())
  521.         if newCount == 0 then
  522.             for i, wantedItem in pairs(wantedItems) do
  523.                 if i == item.computerID then
  524.                     for k, v in pairs(wantedItem) do
  525.                         if v[1] == item.itemName then
  526.                             table.remove(wantedItem, k)
  527.                             term.setTextColor(colors.red)
  528.                             shaka.nextLine()
  529.                             textutils.slowPrint("Removed "..item.itemName.. ".")
  530.                             sleep(1)
  531.                             break
  532.                         end
  533.                     end
  534.                 end
  535.             end
  536.         elseif newCount ~= nil then
  537.             for i, wantedItem in pairs(wantedItems) do
  538.                 if i == item.computerID then
  539.                     for k, v in pairs(wantedItem) do
  540.                         if v[1] == item.itemName then
  541.                             wantedItem[k][2] = newCount
  542.                             term.setTextColor(colors.green)
  543.                             shaka.nextLine()
  544.                             textutils.slowPrint("Changed "..item.itemName.. " to "..newCount)
  545.                             sleep(1)
  546.                             break
  547.                         end
  548.                     end
  549.                 end
  550.             end
  551.         else
  552.         term.setTextColor(colors.red)
  553.         shaka.nextLine()
  554.         textutils.slowPrint("Invalid input..") 
  555.         sleep(1)
  556.         end
  557.         saveWantedItems()
  558.     else
  559.         resetScreen()
  560.         term.setTextColor(colors.yellow)
  561.         print("Multiple results found. Please choose one:\n")
  562.         term.setTextColor(colors.white)
  563.  
  564.         local pageSize = 15 -- number of items to display on each page
  565.         local startIndex = 1 -- index of the first item to display
  566.         local endIndex = math.min(#found, pageSize) -- index of the last item to display
  567.  
  568.         local function displayItems()
  569.           term.clear()
  570.           term.setCursorPos(1, 1)
  571.           term.setTextColor(colors.yellow)
  572.           term.setBackgroundColor(colors.gray)
  573.           term.clearLine()
  574.           print("Scroll and enter item #")
  575.           term.setTextColor(colors.white)
  576.           term.setBackgroundColor(colors.black)
  577.           term.setCursorPos(1, 3)
  578.          
  579.          
  580.           for i = startIndex, endIndex do
  581.             local item = found[i]
  582.             local itemText = ("%d.ID%d: %s x %d"):format(i, item.computerID, item.itemName, item.itemCount)
  583.             term.setTextColor(colors.yellow)
  584.             io.write(i .. ".")
  585.             term.setTextColor(colors.gray)
  586.             io.write("ID" .. item.computerID .. ": ")
  587.             term.setTextColor(colors.white)
  588.             io.write(item.itemName .. " ")
  589.             term.setTextColor(colors.lightGray)
  590.             print("x " .. item.itemCount)
  591.             term.setTextColor(colors.white)
  592.           end
  593.         end
  594.  
  595.         displayItems()
  596.  
  597.         while true do
  598.           local event, param1, param2 = os.pullEvent()
  599.           if event == "mouse_scroll" then
  600.             -- Update the start and end indexes based on the scroll amount
  601.             startIndex = startIndex + param1
  602.             startIndex = math.max(startIndex, 1)
  603.             endIndex = math.min(startIndex + pageSize - 1, #found)
  604.             startIndex = math.max(startIndex, 1)
  605.             displayItems()
  606.           elseif event == "key" then
  607.           if param1 == keys.down then
  608.             startIndex = startIndex + 1
  609.             startIndex = math.max(startIndex, 1)
  610.             endIndex = math.min(startIndex + pageSize - 1, #found)
  611.             startIndex = math.max(startIndex, 1)
  612.             displayItems()
  613.         elseif param1 == keys.up then
  614.             startIndex = startIndex - 1
  615.             startIndex = math.max(startIndex, 1)
  616.             endIndex = math.min(startIndex + pageSize - 1, #found)
  617.             startIndex = math.max(startIndex, 1)
  618.             displayItems()
  619.          
  620.           else
  621.             choice = tonumber(read())
  622.             if choice == nil then
  623.               term.setTextColor(colors.red)
  624.               print("Invalid input. Please try again.")
  625.               sleep(1)
  626.               return
  627.             end
  628.             break
  629.             end
  630.           end
  631.         end
  632. if choice >= 1 and choice <= #found then
  633.   local item = found[choice]
  634.   resetScreen()
  635.   term.setTextColor(colors.white)
  636.   print(("ID%d: %s x %d"):format(item.computerID, item.itemName, item.itemCount))
  637.   term.setTextColor(colors.lightGray)
  638.   print("\nEnter new item count or 0 to delete:\n")
  639.   term.setTextColor(colors.white)
  640.   local newCount = tonumber(read())
  641.  
  642.   if newCount ~= 0 and newCount ~= nil then
  643.     -- If newCount is a number, update the item count
  644.     for i, wantedItem in pairs(wantedItems) do
  645.       if i == item.computerID then
  646.         for k, v in pairs(wantedItem) do
  647.           if v[1] == item.itemName then
  648.             wantedItem[k][2] = newCount
  649.             term.setTextColor(colors.green)
  650.             shaka.nextLine()
  651.             textutils.slowPrint("Changed "..item.itemName.. " to "..newCount)
  652.             sleep(1)
  653.             break
  654.           end
  655.         end
  656.       end
  657.     end
  658.   elseif newCount == 0 then
  659.     -- If newCount is "0", delete the item
  660.     for i, wantedItem in pairs(wantedItems) do
  661.       if i == item.computerID then
  662.         for k, v in pairs(wantedItem) do
  663.           if v[1] == item.itemName then
  664.             table.remove(wantedItem, k)
  665.             term.setTextColor(colors.red)
  666.             shaka.nextLine()
  667.             textutils.slowPrint("Removed "..item.itemName.. ".")
  668.             sleep(1)
  669.             break
  670.           end
  671.         end
  672.       end
  673.     end
  674.   else
  675.     -- If newCount is not a number or "0", display an error message and return
  676.     term.setTextColor(colors.red)
  677.     textutils.slowPrint("Invalid input.")
  678.     sleep(1)
  679.     return
  680.   end
  681.  
  682.   -- Save the updated wantedItems table to file
  683.   saveWantedItems()
  684.   end
  685.  end
  686. end
  687.  
  688. while true do
  689. parallel.waitForAny(menu)
  690. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement