Advertisement
Shaka01

bigStorageMove manageItems dependancy

Feb 2nd, 2023 (edited)
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.75 KB | None | 0 0
  1. ---please enter name of big storage inventory
  2. targetName = "minecraft:chest_0~"
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. if targetName == "minecraft:chest_0~" then
  36.     shell.run("edit","addItem")
  37. end
  38.  
  39. if fs.exists("wanteditems.lua") then
  40.   wantedItems = dofile("wanteditems.lua")
  41. else
  42.   wantedItems = {}
  43.   local file = fs.open("wanteditems.lua", "w")
  44.   file.write("return {}")
  45.   file.close()
  46. end
  47.  
  48. local function saveWantedItems()
  49. local file = fs.open("wanteditems.lua", "w")
  50. file.write("return {\n")
  51. for name, count in pairs(wantedItems) do
  52. file.write((" [%q] = %d,\n"):format(name, count))
  53. end
  54. file.write("}\n")
  55. file.close()
  56. end
  57.  
  58. local function addWantedItem()
  59. term.clear()
  60. term.setCursorPos(1,1)
  61. print("Enter the full item name:")
  62. local name = read()
  63. if name == "" then
  64. print("Field can't be empty.")
  65. sleep(1)
  66. return
  67. end
  68. print("Enter the desired maximum count for this item:")
  69. local count = tonumber(read())
  70. if count == nil then
  71. print("Field can't be empty.")
  72. sleep(1)
  73. return
  74. end
  75. wantedItems[name] = count
  76. saveWantedItems()
  77. print(("%d x %s added to wanted items"):format(count, name))
  78. local event, key, isHeld = os.pullEvent("key")
  79. end
  80.  
  81. local function removeWantedItem()
  82. term.clear()
  83. term.setCursorPos(1,1)
  84. print("Enter the name of the item to remove:")
  85. local name = read()
  86. if wantedItems[name] then
  87. wantedItems[name] = nil
  88. saveWantedItems()
  89. print(("%s removed from wanted items"):format(name))
  90. else
  91. print("Item not found in wanted items")
  92. end
  93. local event, key, isHeld = os.pullEvent("key")
  94. end
  95.  
  96.  
  97. local function viewWantedItems()
  98. term.clear()
  99. term.setCursorPos(1,1)
  100. print("Wanted items:")
  101.  
  102. local items = {}
  103. for name, count in pairs(wantedItems) do
  104. table.insert(items, {name = name, count = count})
  105. end
  106.  
  107. local selected = 1
  108. while true do
  109. term.clear()
  110. term.setCursorPos(1,1)
  111. for i, item in ipairs(items) do
  112. local prefix = i == selected and "> " or " "
  113. print(prefix .. item.count .. " x " .. item.name)
  114. end
  115. local event, key = os.pullEvent("key")
  116. if key == keys.up and selected > 1 then
  117.   selected = selected - 1
  118. elseif key == keys.down and selected < #items then
  119.   selected = selected + 1
  120. elseif key == keys.enter then
  121.   term.clear()
  122.   term.setCursorPos(1,1)
  123.   print("1. Delete entry")
  124.   print("2. Change entry")
  125.   print("3. Back")
  126.   print("Enter your choice:")
  127.   local subchoice = tonumber(read())
  128.   if subchoice == 1 then
  129.     wantedItems[items[selected].name] = nil
  130.     saveWantedItems()
  131.     print(("%s removed from wanted items"):format(items[selected].name))
  132.     sleep(1)
  133.     table.remove(items, selected)
  134.     if selected > #items then
  135.       selected = #items
  136.     end
  137.   elseif subchoice == 2 then
  138.     print("Enter the new desired maximum count for this item:")
  139.     local newCount = tonumber(read())
  140.     if newCount == "" then return end
  141.     wantedItems[items[selected].name] = newCount
  142.     saveWantedItems()
  143.     items[selected].count = newCount
  144.     print(("%d x %s changed in wanted items"):format(newCount, items[selected].name))
  145.     sleep(1)
  146.   elseif subchoice == 3 then
  147.     break
  148.   else
  149.     print("Invalid choice")
  150.     sleep(1)
  151.   end
  152. elseif key == keys.backspace or key == keys.escape then
  153.   break
  154. end
  155. end
  156. end
  157.  
  158. function searchItems(table, key, value)
  159.   term.clear()
  160.   term.setCursorPos(1,1)
  161.   term.setTextColor(colors.yellow)
  162.   print("Enter the item name (partial matches are fine):")
  163.   term.setTextColor(colors.white)
  164.   local itemName = read(nil, history)
  165.   if itemName == "" then
  166.   term.setTextColor(colors.red)
  167.     print("\nNo item name entered\n")
  168.     print("Press any key to continue...")
  169.     term.setTextColor(colors.white)
  170.     os.pullEvent("key")
  171.     return
  172.   end
  173.   local matchingKeys = {}
  174.   for k, v in pairs(table) do
  175.     if string.match(k, itemName) then
  176.       matchingKeys[#matchingKeys+1] = k
  177.     end
  178.   end
  179.   if #matchingKeys == 0 then
  180.   term.setTextColor(colors.red)
  181.     print("\nNo matching items found")
  182.     term.setTextColor(colors.green)
  183.     print("\nPress any key to continue...")
  184.     term.setTextColor(colors.white)
  185.     os.pullEvent("key")
  186.     return
  187.   elseif #matchingKeys == 1 then
  188.     key = matchingKeys[1]
  189.   else
  190.     term.clear()
  191.     term.setCursorPos(1,1)
  192.     term.setTextColor(colors.yellow)
  193.     print("Multiple matching items found:")
  194.     term.setTextColor(colors.white)
  195.     for i = 1, #matchingKeys do
  196.       local currentVal = table[matchingKeys[i]]
  197.       if currentVal ~= nil then
  198.         print(i .. ": " .. matchingKeys[i] .. " (current value: " .. currentVal .. ")")
  199.       else
  200.         print(i .. ": " .. matchingKeys[i])
  201.       end
  202.     end
  203.     term.setTextColor(colors.green)
  204.     print("\nPlease click the item you want.")
  205.     term.setTextColor(colors.white)
  206.     local selection
  207.         local a, b = term.getCursorPos()
  208.         local k, l = term.getSize()
  209.       term.setCursorPos(k -1 , 1)
  210.       term.setTextColor(colors.red)
  211.       term.setBackgroundColor(colors.gray)
  212.       -- term.setCursorPos(k , 1)
  213.       print(" X")
  214.       term.setBackgroundColor(colors.black)
  215.       term.setTextColor(colors.white)
  216.       term.setCursorPos(a, b)
  217.     while true do
  218.       local event, button, x, y = os.pullEvent()
  219.       if event == "mouse_click" then
  220.         if y == 1 and x == term.getSize() then
  221.           os.reboot()
  222.         elseif y >= 2 and y <= #matchingKeys + 1 then
  223.           selection = y - 1
  224.           break
  225.         end
  226.       elseif event == "key" then
  227.         if button == keys.one then selection = 1
  228.         elseif button == keys.two then selection = 2
  229.         elseif button == keys.three then selection = 3
  230.         elseif button == keys.four then selection = 4
  231.         end
  232.       end
  233.     end
  234.     key = matchingKeys[selection]
  235.   end
  236.   term.clear()
  237.   term.setCursorPos(1,1)
  238.   term.setTextColor(colors.yellow)
  239.   local currentVal = table[key]
  240.   print("Enter the new amount for " .. key)
  241.   term.setTextColor(colors.lightGray)
  242.     print("Or leave blank to delete.")
  243.     term.setTextColor(colors.green)
  244.   print("\nCurrent amount: " .. (currentVal or "").."\n")
  245.   term.setTextColor(colors.white)
  246.   local newValue = read()
  247.   if newValue == "" then
  248.     table[key] = nil
  249.     term.setTextColor(colors.red)
  250.     print("Deleted " .. key)
  251.     term.setTextColor(colors.white)
  252.   else
  253.     table[key] = newValue
  254.     term.setTextColor(colors.lime)
  255.     print("\nSuccess!")
  256.     term.setTextColor(colors.green)
  257.     print("Updated " .. key .. " to " .. newValue)
  258.     term.setTextColor(colors.white)
  259.   end
  260.   saveWantedItems(table)
  261.   print("Press any key to continue...")
  262.   os.pullEvent("key")
  263. end
  264.  
  265.  
  266. function scanAndSendInventories()
  267.     local inventory = peripheral.wrap(targetName)
  268.     term.clear()
  269.     term.setCursorPos(1,1)
  270. term.setTextColor(colors.green)
  271. print("Success!")
  272. term.setTextColor(colors.white)
  273.     term.setCursorPos(1,3)
  274.     for slot, item in pairs(inventory.list()) do
  275.       if not wantedItems[item.name] then
  276.         wantedItems[item.name] = item.count
  277.         print("Added " ..item.name)
  278.         else
  279.         --print("Already in list: " ..item.name)
  280.       end
  281.     end
  282. local file = fs.open("wanteditems.lua", "w")
  283. file.write("return {\n")
  284. for name, count in pairs(wantedItems) do
  285. file.write((" [%q] = %d,\n"):format(name, count))
  286. end
  287. file.write("}\n")
  288. file.close()
  289. term.setTextColor(colors.lightGray)
  290. print("\nPress any key to continue..")
  291. term.setTextColor(colors.white)
  292. event, key = os.pullEvent("key")
  293. end
  294.  
  295.  
  296.  
  297.  
  298.  
  299. while true do
  300. term.clear()
  301. term.setCursorPos(1,1)
  302. print("1. Add new item")
  303. print("2. View and remove items")
  304. print("3. Search for items")
  305. print("4. Quit")
  306. print("5. Update Items")
  307. print("Enter your choice:")
  308. local choice = tonumber(read())
  309. if choice == 1 then
  310. addWantedItem()
  311. elseif choice == 2 then
  312. viewWantedItems()
  313. elseif choice == 3 then
  314. searchItems(wantedItems, key, value)
  315. elseif choice == 4 then
  316. os.reboot()
  317. elseif choice == 5 then
  318. scanAndSendInventories()
  319. else
  320. print("Invalid choice")
  321. end
  322. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement